Lab Sheet5

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Amrita School of Computing, Amritapuri Campus.

19CSE213: Operating Systems


LAB SHEET 5
Process Creation using fork() System call
LAB EXERCISE

1. Write a program to create processes according to the tree structure given


below. All processes should print their Process id and Parent Process id
and the label given in the process.

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
printf("Process A: %d\n", getpid());
if (fork()) {
wait(NULL);
if (fork()) {
wait(NULL);
if (fork()) {
wait(NULL);
} else {
printf("Process D: %d %d\n", getpid(), getppid());
if (fork())
wait(NULL);
else {
printf("Process G: %d %d\n", getpid(), getppid());
if (fork()) {
wait(NULL);
if (fork())
wait(NULL);
else
printf("Process I: %d %d\n", getpid(), getppid());
} else
printf("Process H: %d %d\n", getpid(), getppid());
}
}
} else {
printf("Process C: %d %d\n", getpid(), getppid());
if (fork()) {
wait(NULL);
if (fork()) {
wait(NULL);
} else {
printf("Process F: %d %d\n", getpid(), getppid());
}
} else {
printf("Process E: %d %d\n", getpid(), getppid());
}
}
} else {
printf("Process B: %d %d\n", getpid(), getppid());
}

}
2. Execute the fork.c program given to you more than once. What is the
order in which the processes are being executed? Is it the same in every
execution?
Yes, first child is being executed and then the parent is being executed.
Here parent is waiting for the child to complete before creating another
child process using wait.

3. Modify the fork.c program using wait () or sleep () system call, so that
parent will wait until child completes its execution.
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#define MAX_COUNT 200

void ChildProcess(void); /* child process prototype */


void ParentProcess(void); /* parent process prototype */

int main(void)
{
pid_t pid;

pid = vfork();
if (pid == 0)
ChildProcess();
else {
wait(NULL);
ParentProcess();
}
}
void ChildProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf(" This line is from child, value = %d\n", i);
printf(" *** Child process is done ***\n");
}
void ParentProcess(void)
{
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf("This line is from parent, value = %d\n", i);
printf("*** Parent is done ***\n");
}

4. Write a program to find area and perimeter of circle and square. Create
separate processes for circle and square.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(){
float a,p,r,s;
if(!fork()){
printf("Enter the side of sqaure : ");
scanf("%f",&s);
a=s*s;
p=4*s;
printf("Area of sqaure is: %.3f\n",a);
printf("Perimeter of sqaure : %.3f\n",p);
}
else{
wait(NULL);
printf("Enter the radius of circle : ");
scanf("%f",&r);
a=3.14*r*r;
p=6.28*r;
printf("Area of circle is %.3f : \n",a);
printf("Perimeter of circle is : %.3f \n ",p);
}
}

5. Modify the above program as follows: Parent process should create two
children.
[User enters Value of variable ‘a’ only once]
The first child finds area and perimeter of a circle with radius ‘a’ The
Second child finds area and perimeter of square with side ‘a’.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(){
float s,r,a,p;
scanf("%f",&s);
r=s;
if(!fork()){
a=s*s;
p=4*s;
printf("Area of sqaure is: %.3f\n",a);
printf("Perimeter of sqaure : %.3f\n",p);
}
else if(!fork()){
a=3.14*r*r;
p=6.28*r;
printf("Area of circle is %.3f : \n",a);
printf("Perimeter of circle is : %.3f \n ",p);
}
}
6. Modify the previous program to make the parent process wait until the
completion of its children.
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
int s,r;
int sa,sp;
pid_t c1,c2;
float ca,cp;
printf("Enter side/radius: ");
scanf("%d",&s);
r=s;
if(!fork()){
sa=s*s;
sp = 4*s;
printf("Area of square is: %d\n",sa);
printf("Perimeter of square: %d\n",sp);
exit(0);
}
else if(!fork()){
ca=3.14*r;
cp=2*3.14*r;
printf("Area of circle is: %f\n",ca);
printf("Perimeter of circle: %f\n",cp);
exit(0);
}
while(wait(NULL)>0){}printf("Parent Process completed\n");
}
7. Modify question 5 to make the parent process wait until the completion of
its child process that finds area of the Square.
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int s,r;
int sa,sp;
pid_t c1,c2;
float ca,cp;
printf("Enter side/radius: ");
scanf("%d",&s);
r=s;
if(!fork()){
c1=getpid();
sa=s*s;
sp = 4*s;
printf("Area of square is: %d\n",sa);
printf("Perimeter of square: %d\n",sp);
exit(0);
}
else if(!fork()){
ca=3.14*r;
cp=2*3.14*r;
printf("Area of circle is: %f\n",ca);
printf("Perimeter of circle: %f\n",cp);
exit(0);
}
wait(c1);
printf("Parent Process completed\n");
}
8. Create a parent process having two children. The first child should
overwrite its address space with a process that prints “Happy new year”.
The second child should overwrite its address space with another process
that prints the sum of digits of a number entered by the user. [Hint: use
exec family of system calls]
Sample output: The output should come in the following order
Happy new year
Enter the number: 123
Sum of Digits: 6
Parent exiting …good bye.

8.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main() {
pid_t child1, child2;
int status;

child1 = fork();

if (child1 == 0) {

execl("/bin/echo", "echo", "Happy new year", NULL);


perror("Exec failed");
} else if (child1 > 0) {

child2 = fork();

if (child2 == 0) {
int number;
printf("Enter a number: ");
scanf("%d", &number);
int sum = 0;
while (number > 0) {
sum += number % 10;
number /= 10;
}
printf("Sum of digits: %d\n", sum);
} else if (child2 > 0) {
waitpid(child1, &status, 0);
waitpid(child2, &status, 0);
} else {
perror("Fork failed");
}
} else {
perror("Fork failed");
}

return 0;
}

You might also like