CSE 4049: Design of Operating Systems
ASSIGNMENT 1:
This assignment is designed to give you practice with the concepts of
Process management
▪ Trace different states of the process during execution
▪ To learn the use of different system calls such as (fork(),vfork(),wait(),
exit()) for process handling in Linux environment.
1. Write a C program to create a child process using fork() system call. The child process
will print the message “Child” with its process identifier and then continue in an
indefinite loop. The parent process will print the message “Parent” with its process
identifier and then continue in an indefinite loop.
a) Run the program and trace the state of both processes.
b) Terminate the child process. Then trace the state of processes.
c) Run the program and trace the state of both processes. Terminate the parent
process. Then trace the state of processes.
d) Modify the program so that the parent process after displaying the message will
wait for child process to complete its task. Again run the program and trace the
state of both processes.
e) Terminate the child process. Then trace the state of processes.
2. Trace the output of the following program segment :
a) int main( ) b) int main( )
{ {
if(fork()==0) if(vfork()==0)
printf("1"); printf("1");
else else
printf("2"); printf("2");
printf("3"); printf("3");
return 0; }
}
c) int main( ) d) int main( )
{ {
pid_t pid; pid_t pid;
int i=5; int i=5;
pid=fork(); pid=vfork();
i=i+1; i=i+1;
if(pid= =0) if(pid==0)
{ {
printf("Child: %d",i); printf("Child: %d",i);
} _exit(0);
else }
{ else
wait(NULL); {
printf("Parent: %d",i); printf("Parent: %d",i);
} }
return 0; return 0;
} }
e) int main( ) f) int main( )
{ {
pid_t pid; pid_t pid;
int i=5; int i=5;
pid=fork(); pid=vfork();
if(pid= =0) if(pid==0)
{ {
i=i+1; i=i+1;
printf("Child: %d",i); printf("Child: %d",i);
} _exit(0);
else }
{ else
wait(NULL); {
printf("Parent: %d",i); printf("Parent: %d",i);
} }
return 0; return 0;
} }
g) int main( ) h) int main( )
{ {
int i=5; int i=5;
if(fork( )==0) if(vfork( )==0)
{ {
printf("Child: %d",i); printf("Child: %d",i);
} _exit(0);
else }
{ else
printf("Parent: %d",i); {
} printf("Parent: %d",i);
return 0; }
} return 0;
}
i) int main( ) j) int main( )
{ {
if(fork( )==0) if(vfork( )==0)
{ {
printf("1"); printf("1");
} _exit(0);
else }
{ else
wait(NULL); {
printf("2"); printf("2");
printf("3"); printf("3");
} }
return 0; return 0;
} }
k) int main( ) l) int main( )
{ {
pid_t c1; pid_t c1;
int n=10; int n=10;
c1=fork( ); c1=vfork( );
if(c1==0) if(c1==0)
{ {
printf(" Child\n"); printf(" Child\n");
n=20; n=20;
printf("n=%d \n",n); printf("n=%d \n",n);
} _exit(0);
else }
{ else
wait(NULL); {
printf("Parent\n"); printf("Parent\n");
printf("n=%d \n",n); printf("n=%d \n",n);
} }
return 0; return 0;
} }
m) int main( ) n) int main( )
{ {
int i=5; pid_t pid;
fork(); int i=5;
i=i+1; pid=vfork();
fork(); if(pid==0)
fprintf ( stderr,"% d",i); {
return 0; printf("Child: %d",i);
} _exit(0);
}
else
{
i=i+1;
printf("Parent: %d",i);
}
return 0;
}
o) int main( ) p) int main( )
{ {
int i=5; int i=5;
if(fork()==0) if(vfork()==0)
i=i+1; {
else i=i+1;
i=i-1; _exit(0);
fprintf(stderr,"%d",i); }
return 0; else
} i=i-1;
fprintf(stderr,"%d",i);
return 0;
}
q) int main( ) r) int main( )
{ {
int j,i=5; int j,i=5;
for(j=1;j<3;j++) for(j=1;j<3;j++)
{ {
if(fork()==0) if(fork()!=0)
{ {
i=i+1; i=i-1;
break; break;
} }
else }
wait(NULL); fprintf(stderr,"%d",i);
} return 0;
fprintf(stderr,"%d",i); }
return 0;
}
s) int main( ) t) void fun1(){
{
if(fork() == 0) fork();
if(fork()) fork();
printf("1\n"); printf("1\n");
return 0; }
} int main() {
fun1();
printf("1\n");
return 0;
3. Trace the following program segment and determine how many processes are created.
Draw a graph that shows how the processes are related.
a. int main( ) b. int main( )
{ {
if(fork( )&&fork( )); if(fork( ) || fork( ));
printf(" 1”); printf(" 1”);
return 0; return 0;
} }
c. int main( ) d. int main( )
{ {
pid_t c1,c2; pid_t c1=1,c2=1;
c2=0; c1 = fork();
c1 = fork(); if (c1 != 0)
if (c1 == 0) c2 = fork();
c2 = fork(); if (c2== 0)
if (c2 > 0) fork();
fork(); printf(" 1”);
printf(" 1”); return 0;
return 0; }
}
e. int main( ) f. int main( )
{ {
if (fork() || fork()) if (fork() && (!fork()))
fork(); {
printf(" 1 "); if (fork() || fork())
return 0; {
} fork();
}
}
printf("2 ");
return 0;
}