You are on page 1of 30

JAYPEE UNIVERSITY OF INFORMATION AND TECHNOLOGY

OPERATING SYSTEMS LAB

SUBMITTED TO:

MADE BY:

Dr.RAJNI MOHANA

DILPREET SAWHNEY
131257
E7

TABLE OF CONTENTS

S
No
.

Topic

Date

Signature

PROGRAMS

1.
#include <stdio.h>
#include <unistd.h>
int main(void) {
printf("Hello World!\n");
fork( );
printf("I am after forking\n");
printf("\tI am process %d.\n", getpid( ));
}

OUTPUT:
Hello World!
I am after forking
I am process 21678.
I am after forking
I am process 21677.

2.
#include <unistd.h>
int main(void)
{ intpid;
printf("Hello World!\n");
printf("I am the parent process and pid is : %d .\n",getpid());
printf("Here i am before use of forking\n");
pid = fork( );
printf("Here I am just after forking\n");
if (pid == 0)
printf("I am the child process and pid is :%d.\n",getpid());
else
printf("I am the parent process and pid is: %d .\n",getpid());

}
OUTPUT:
Hello World!
I am the parent process and pidis : 29351 .
Here i am before use of forking
Here I am just after forking
I am the child process and pidis :29352.
Here I am just after forking
I am the parent process and pid is: 29351 .

3.
#include <stdio.h>
#include <unistd.h>
int main(void)
{
printf("Here I am just before first forking statement\n");
fork( );
printf("Here I am just after first forking statement\n");
fork( );
printf("Here I am just after second forking statement\n");
fork( );
printf("Here I am just after third forking statement\n");
printf("

Hello World from process %d!\n", getpid( ));

OUTPUT:
Here I am just before first forking statement
Here I am just after first forking statement
Here I am just after second forking statement
Here I am just after third forking statement

Hello World from process 22120!


Here I am just after first forking statement
Here I am just after second forking statement
Here I am just after third forking statement
Hello World from process 22119!
Here I am just after second forking statement
Here I am just after third forking statement
Hello World from process 22122!
Here I am just after third forking statement
Hello World from process 22123!
Here I am just after second forking statement
Here I am just after third forking statement
Hello World from process 22118!
Here I am just after third forking statement
Hello World from process 22121!
Here I am just after third forking statement
Hello World from process 22124!
Here I am just after third forking statement
Hello World from process 22117!

4.
#include <stdio.h>
#include <sys/wait.h>
int main(void)
{
int pid;
int status;
printf("Hello World!\n");

pid = fork( );
if (pid == -1)
{
perror("bad fork");
exit(1);
}
if (pid == 0)
printf("

I am the child process.\n");

else
{
wait(&status); /* parent waits for child to finish */
printf("I am the parent process.\n");
}
}

OUTPUT:
Hello World!
I am the child process.
I am the parent process.
5.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

main ( )
{
int forkresult ;

printf ("%d: I am the parent. Remember my number!\n", getpid( ) ) ;


printf ("%d: I am now going to fork ... \n", getpid( ) ) ;

forkresult = fork ( ) ;

if (forkresult != 0)
{

/* the parent will execute this code */


printf ("%d: My child's pid is %d\n", getpid ( ), forkresult ) ;

}
else

/* forkresult == 0 */

/* the child will execute this code */


printf ("%d: Hi! I am the child.\n", getpid ( ) ) ;

printf ("%d: like father like son. \n", getpid ( ) ) ;


}

OUTPUT:
28715: I am the parent. Remember my number!
28715: I am now going to fork ...
28716: Hi! I am the child.
28716: like father like son.
28715: My child's pid is 28716
28715: like father like son.

6.

#include <stdio.h>
main ( )
{
int pid ;
printf ("I'am the original process with PID %d and PPID %d.\n",
getpid ( ), getppid ( ) ) ;

pid = fork ( ) ;
if ( pid != 0 )

/* Duplicate. Child and parent continue from here */


/* pid is non-zero, so I must be the parent */

{
printf ("I'am the parent process with PID %d and PPID %d.\n",
getpid ( ), getppid ( ) ) ;
printf ("My child's PID is %d\n", pid ) ;
}
else

/* pid is zero, so I must be the child */

{
sleep (4) ;

/* make sure that the parent terminates first */

printf ("I'am the child process with PID %d and PPID %d.\n",
getpid ( ), getppid ( ) ) ;
}
printf ("PID %d terminates.\n", getpid ( ) ) ;
}

OUTPUT:
I'am the original process with PID 5100 and PPID 5011.
I'am the parent process with PID 5100 and PPID 5011.
My child's PID is 5101
PID 5100 terminates.

/* Parent dies */

I'am the child process with PID 5101 and PPID 1.


/* Orphaned, whose parent process is init with pid 1 */
PID 5101 terminates.

7.
#include <stdio.h>
main ( )
{
int pid ;

pid = fork ( ) ;

if ( pid != 0 )
{
while (1)
sleep (100) ;
}
else
{
exit (42) ;
}
}

OUTPUT:
[rh440@linuxserver ~]$ ./a.out &
[1] 26974
[rh440@linuxserver ~]$ parent process is over
[rh440@linuxserver ~]$ ps

PID TTY

TIME CMD

26219 pts/9

00:00:00 bash

26974 pts/9

00:00:00 a.out

26975 pts/9

00:00:00 a.out <defunct>

26976 pts/9

00:00:00 ps

THREADS:
1.
#include <stdio.h>
#include <pthread.h>
int glob_data = 5 ;
void *kidfunc(void *p)
{
printf ("Kid here. Global data was %d.\n", glob_data) ;
glob_data = 15 ;

printf ("Kid Again. Global data was now %d.\n", glob_data) ;


}
main ( )
{
pthread_t kid ;
pthread_create (&kid, NULL, kidfunc, NULL) ;
printf ("Parent here. Global data = %d\n", glob_data) ;
glob_data = 10 ;
pthread_join (kid, NULL) ;
printf ("End of program. Global data = %d\n", glob_data) ;
}

OUTPUT:
Parent here.
Global data = 5 Kid here.
Global data was 10. Kid Again.
Global data was now 15.
End of program.
Global data = 15

2.
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
printf("\n%d: Hello World!\n", threadid);
pthread_exit(NULL);

}
int main( )
{
pthread_t threads [NUM_THREADS];
int rc, t;
for(t=0; t < NUM_THREADS; t++)
{
printf ("Creating thread %d\n", t);
rc = pthread_create (&threads[t], NULL, PrintHello, (void *) t );
if (rc)
{
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}

OUTPUT:
Creating thread 0
Creating thread 1
Creating thread 2
Creating thread 3
Creating thread 4
4: Hello World!
2: Hello World!
3: Hello World!

1: Hello World!
0: Hello World!

3.
#include<pthread.h>
#include<stdio.h>
int sum;
void *runner(void *param);
main(int argc, char *argv[])
{
pthread_t tid;
pthread_attr_t attr;
if(argc != 2)
{
fprintf(stderr,"usage: a.out <integer value>\n");
exit();
}
if(atoi(argv[1]) < 0)
{
fprintf(stderr, "%d must be >= 0 \n", atoi(argv[1]));
exit();
}
pthread_attr_init(&attr);
pthread_create(&tid,&attr,runner,argv[1]);
pthread_join(tid,NULL);
printf("sum = %d\n",sum);
}
void *runner(void *param)

{
int upper = atoi(param);
int i;
sum=0;
if(upper > 0)
{
for(i=1; i <= upper;i++)
sum += i;
}
pthread_exit(0);
}

OUTPUT:
10
sum = 55

4.
#include <stdio.h>
#include <pthread.h>
void *kidfunc(void *p)
{
printf ("Kid ID is ---> %d\n", getpid( ));
}
main ( )
{
pthread_t kid ;
pthread_create (&kid, NULL, kidfunc, NULL) ; printf ("Parent ID is ---> %d\n", getpid( )) ;
pthread_join (kid, NULL) ;
printf ("No more kid!\n") ;
}
OUTPUT:

Parent ID is ---> 23677


Kid ID is ---> 23677
No more kid!

5.
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
int this_is_global;
void thread_func( void *ptr );
int main( )
{
int local_main;
int pid, status;
pthread_t thread1, thread2;
printf("First, we create two threads to see better what context they share\n");
this_is_global=1000;
printf("Set this_is_global=%d\n",this_is_global);
pthread_create( &thread1, NULL, (void*)&thread_func, (void*) NULL);
pthread_create(&thread2, NULL, (void*)&thread_func, (void*) NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("After threads, this_is_global=%d\n",this_is_global);
printf("\n"); printf("Now that the threads are done, let's call fork..\n");
local_main=17;
this_is_global=17;
printf("Before fork(), local_main=%d, this_is_global=%d\n",local_main, this_is_g
lobal);

pid=fork();
if (pid == 0)
{
printf("In child, pid %d: &global: %X, &local: %X\n", getpid(), &this_is_global, &local_main);
local_main=13;
this_is_global=23;
printf("Child set local main=%d, this_is_global=%d\n",local_main, this_is_global);
exit(0);
}
else {
printf("In parent, pid %d: &global: %X, &local: %X\n", getpid(), &this_is_global,
&local_main);
wait(&status);

printf("In parent, local_main=%d, this_is_global=%d\n",local_main, this_is_global);


}
exit(0);
}
void thread_func(void *dummy)
{
int local_thread;
printf("Thread %d, pid %d, addresses: &global: %X, &local: %X\n", pthread_self(),getpid()
,&this_is_global, &local_thread);
this_is_global++;
printf("In Thread %d, incremented this_is_global=%d\n", pthread_self(), this_is_global);
pthread_exit(0); }

OUTPUT:
First, we create two threads to see better what context they share/nSet this_is_
global=1000
Thread -1226790032, pid 27792, addresses: &global: 8049D0C, &local: B6E0A37C
In Thread -1226790032, incremented this_is_global=1001
Thread -1216300176, pid 27792, addresses: &global: 8049D0C, &local: B780B37C
In Thread -1216300176, incremented this_is_global=1002
After threads, this_is_global=1002

Now that the threads are done, let's call fork..


Before fork(), local_main=17, this_is_global=17
In parent, pid 27792: &global: 8049D0C, &local: BFFBE3D8
In child, pid 27795: &global: 8049D0C, &local: BFFBE3D8
Child set local main=13, this_is_global=23
In parent, local_main=17, this_is_global=17

6.
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
int tot_items = 0 ;
struct kidrec { int data ; pthread_t id ; } ;
#define NKIDS 50
void *kidfunc(void *p)
{
int *ip = (int *)p ;
int tmp, n ;
tmp = tot_items ;
for (n = 50000; n--; )
tot_items = tmp + *ip ; }
main ( )
{
struct kidrec kids[NKIDS] ;
int m ;
for (m=0; m<NKIDS; ++m)
{
kids[m].data = m+1 ;
pthread_create (&kids[m].id, NULL, kidfunc, &kids[m].data) ;
}
for (m=0; m<NKIDS; ++m)
pthread_join (kids[m].id, NULL) ;
printf ("End of Program. Grand Total = %d\n", tot_items) ;
}

OUTPUT:
End of Program. Grand Total = 1243

7.
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 7
char *messages[NUM_THREADS];
void *PrintHello(void *threadid)
{
int *id_ptr, taskid;
sleep(1);
id_ptr = (int *) threadid;
taskid = *id_ptr;
printf("\n %s from thread %d \n\n", messages[taskid], taskid);
pthread_exit(NULL);
}
int main( )
{
pthread_t threads[NUM_THREADS];
int *taskids[NUM_THREADS];
int rc, t;
messages[0] = "English: Hello World!";
messages[1] = "French: Bonjour, le monde!";
messages[2] = "Spanish: Hola al mundo";
messages[3] = "Klingon: Nuq neH!";
messages[4] = "German: Guten Tag, Welt!";
messages[5] = "Russian: Zdravstvytye, mir!";

messages[6] = "Japan: Sekai e konnichiwa!";


messages[7] = "Latin: Orbis, te saluto!";
for(t=0;t<NUM_THREADS;t++)
{
taskids[t] = (int *) malloc(sizeof(int));
*taskids[t] = t;
printf("Creating thread %d\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *) taskids[t]);
if (rc)
{
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}

OUTPUT:
Creating thread 0
Creating thread 1
Creating thread 2
Creating thread 3
Creating thread 4
Creating thread 5
Creating thread 6

Japan: Sekai e konnichiwa! from thread 6


German: Guten Tag, Welt! from thread 4

Spanish: Hola al mundo from thread 2


Russian: Zdravstvytye, mir! from thread 5
Klingon: Nuq neH! from thread 3
French: Bonjour, le monde! from thread 1
English: Hello World! from thread 0

FCFS:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<time.h>
#include<stdlib.h>
intbuf[10],n;
int main()
{
srand(time(NULL));
pid_t p;
inti,count=0;
n=0;
for(i=0;i<10;i++)
{
buf[i]=0;
}
p=fork();
while(1)
{
if(p<0)
{
return 0;
}
else if(p==0)
{
if(n>0)
{
printf("process running");
buf[n-1]=0;
n--;
count++;
}
}
else{
if(n<10)
{
intans=rand()%10+1;

buf[n]=ans;
n++;
}
}
}
return 0;
}

OUTPUT:
produced 1
produced 2
produced 3
produced 4
produced 5
produced 6
produced 7
produced 8
produced 9

array full
consuming 1
consuming 2
consuming 3
consuming 4
consuming 5
consuming 6
consuming 7
consuming 8
consuming 9

Lab4
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
intpfd[2],i;
pid_tmypid;
if(pipe(pfd) < 0)
{
perror("Pipe Error");
}
if(!fork())
{
char data;
printf("Enter a Number\n");
scanf("%d", &data);
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
intpfd[2],i;
pid_tmypid;
if(pipe(pfd) < 0)
{
perror("Pipe Error");
}
if(!fork())
{
char data;
printf("Enter a Number\n");
scanf("%d", &data);
write(pfd[1], &data,1);
mypid = getpid();
printf("I am process %d\n", mypid);
printf("My parent is process %d\n", getppid());
printf("Child Exiting\n");
exit(0);
}
else
{
char data1;
read(pfd[0], &data1, 1);
printf("Received %d from child \n", data1);
printf("The odd numbers are \n");
for(i=1; i<=data1; i+=2)
{
printf("%5d", i);

sleep(2);
}
printf("\n Parent Exiting\n");
exit(0);
}
return(0);
}
OUTPUT:
Enter a Number
25
I am process 29424
My parent is process 29423
Child Exiting
Received 25 from child
The odd numbers are
25

Peterson:
#include<stdio.h>
#include<pthread.h>
#include<time.h>
#include<stdlib.h>
intbuf[10],i=-1,count=10;
intturn,flag[2];
void *producer(void *p)
{
srand(time(NULL));
do
{
flag[0]=1;
turn=1;
while(flag[i]=1 && turn==1)
#include<stdio.h>
#include<pthread.h>
#include<time.h>
#include<stdlib.h>
intbuf[10],i=-1,count=10;
intturn,flag[2];
void *producer(void *p)
{
srand(time(NULL));
do
{

flag[0]=1;
turn=1;
while(flag[i]=1 && turn==1)
if(i<9)
{
i++;
buf[i]=rand()%10+1;
printf("process created:%d\n",buf[i]);
count--;
sleep(1);
}
flag[0]=0;
}while(count>=0);
}
void *consumer(void *a)
{
do{
flag[0]=1;
turn=0;
while(flag[i]==1 && turn==0);
if(i>=0)
{
printf("process:%d is consumed\n",buf[i]);
i--;
count--;
sleep(1);
}
flag[0]=0;
}while(count>=0);
}
int main()
{
pthread_tp,q;
pthread_attr_tattr;
pthread_attr_init(&attr);
pthread_create(&p,NULL,producer,NULL);
pthread_create(&q,NULL,consumer,NULL);
pthread_exit(NULL);
return 0;
OUTPUT:
process created:1
process created:8
process:8 is consumed
process created:3
process:3 is consumed
process created:3
process:3 is consumed
process created:3
process:3 is consumed
process created:4
process:4 is consumed

SJF
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include<time.h>
intarr[5];
void *r(void *a)
{
inti=0;
srand(time(NULL));
while(i<5)
{
arr[i]=1+rand()%5;
printf("new process %d \n",arr[i]);
i++;
}
pthread_exit(NULL);
}
int lowest(int array[])
{
int temp=1000,i,j=0;
for(i=0;i<5;i++)
{
if(array[i]<temp && array[i]!=-1)
{
temp=array[i];
j=i;
}
}
array[j]=-1;
return temp;
}
void *f1(void *brst)
{
int temp=0,count=0,a=0,b=0;
while(count<5)
{
a=a+temp;
temp=lowest(arr);
b=b+temp;
count++;
printf("thread working= %d\n",temp);
sleep(temp);
}

OUTPUT:
new process 3
new process 1
new process 5
new process 3
new process 1
thread working= 1
thread working= 1
thread working= 3
thread working= 3
thread working= 5
calculating turnaround time: 2

Semaphore
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<time.h>
intmutex=1,buf[10],i=0;
void wait(mutex)
{
while(mutex<=0);
mutex--;
}
void signal(mutex)
{
mutex++;
}
void *producer()
{
while(1)
{
wait(mutex);
srand(time(NULL));
if(i<9)
{
buf[i]=rand()%10+1;
printf("process created\n",buf[i]);
i++;
}
signal(mutex);
}
}
void *consumer()
{
while(1)
{
wait(mutex);

if(i>0)
{
i--;
printf("process consumed\n",buf[i]);
signal(mutex);
}
}
}
void main()
{
pthread_tp,q;
pthread_attr_tattr;
//pthread_attr_init(attr);
pthread_create(&p,NULL,producer,NULL);
pthread_create(&q,NULL,consumer,NULL);
pthread_exit(NULL);
}

OUTPUT:
Waiting 1
Producing 0
Signaling 0
Waiting 1
Consuming 0
Signaling 1

Round robin
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<time.h>
intarr[5],i,count=0;
void *production()
{
srand(time(NULL));
for(i=0;i<5;i++)
{
arr[i]=1+rand()%10;
printf("process %d created for time %d\n",i,arr[i]);
}
pthread_exit(NULL);
}
void *consumer()
{
intqt=5;
while(1)
{
if(count==5)
{
break;
}
else
{
for(i=0;i<5;i++)
{
if(arr[i]<=0)
{
count=count++;
continue;
}
else
{
if(arr[i]<qt)
{
printf("process %d is executing for burst time %d\n",i,arr[i]);
arr[i]=0;
sleep(arr[i]);
}
else
{
printf("process %d is running for time %d\n",i,qt);
arr[i]=arr[i]-qt;
sleep(qt);
}
}
}
}

}
pthread_exit(NULL);
}
int main()i
{
inta,b;
pthread_tp,q;
pthread_attr_tattr;
pthread_attr_init(&attr);
a=pthread_create(&p,NULL,production,NULL);
b=pthread_create(&q,NULL,consumer,NULL);
pthread_exit(0);
return 0;
}

OUTPUT:
producing 0 with bt 4
producing 1 with bt 9
producing 2 with bt 6
producing 3 with bt 3
producing 4 with bt 7
executing 0 for 4 sec
executing 1 for 4 sec
executing 2 for 4 sec
executing 3 for 3 sec
executing 4 for 4 sec
executing 1 for 4 sec
executing 2 for 2 sec
executing 4 for 3 sec
executing 1 for 1 sec

You might also like