Program to Implement System Calls Using Fork Function

0
2240

Understanding the functions of fork system call would help to know in detail about child process creation. The resulting child process is mostly identical to the process which calls the function.The child process has a unique process ID and it executes independently of the parent process. When you call p = fork() , a new process (the child process) is created based on the current process. Thus a separated address space is created for the child.

• If fork() system call returns a negative value, it implies that the creation of the process was unsuccessful.
• If a new child process is created, fork() system call returns zero.
• fork() system call returns a positive value and the process ID of the child process to the parent.

PROBLEM DEFINITION

Write a program to implement System calls using fork function.

ALGORITHM

Step 1: Start

Step 2: Declare p1 and p2

Step 3: Call the fork function for p1.If p1  is equal to -1 print error and return 0.

Step 4: Else print the parent and child by getppid() and getpid().

Step 5: Again call fork () for p2. Print the parent and child by getppid() and getpid().

Step 6: Stop

Fork system call program

#include<stdio.h>

#include<sys/wait.h>

int main()

{

int p1,p2;

p1=fork();

if(p1==-1)

{

printf(“Error”);

return 0;

}

else

{

printf(“parent is %d\n”,getppid());

printf(“child is %d\n”,getpid());

}

p2=fork();

printf(“parent is %d\n”,getppid());

printf(“child is %d\n”,getpid());

}

OUTPUT

Parentid     26678

Childid      28557

Parentid     28557

Childid      28558

Parentid     26678

Childid      28557

CONCLUSION

The program was executed successfully

PARENT-CHILD RELATION USING FORK

PROBLEM DEFINITION

Write a program to implement a given Parent-Child relation using fork

ALGORITHM

Step 1: Start

Step 2: Declare int p1,p2,p3 and  p4;

Step 3: Call the fork function for p1.If p1 is equal to -1 print error and return 0.

Step 4: If p1 is equal to 0 then call fork () for p2.If p2 =0 the print the parent and child by getppid() and getpid().

Step 5: Again call fork () for p3. If p3 is equal to -1 print error and return 0.

Step 6: If p3 =0 then print the parent and child by getppid() and getpid().

Step 7: Call fork () for p4. If p4=0 the print the parent and child by getppid() and getpid().return 0

Step 8: Stop

PROGRAM DEVELOPMENT

#include<sys/wait.h>

#include<stdio.h>

int main()

{

int p1,p2,p3,p4;

p1=fork();

if(p1==-1)

{

printf(“Error”);

return 0;

}

if(p1==0)

{

p2=fork();

if(p2==0)

{

printf(“Parent is %d\n”,getppid());

printf(“child is %d\n”,getpid());

}

p3=fork();

if(p3==-1)

{

printf(“Error”);

return 0;

}

if(p3==0)

{

printf(“Parent is %d\n”,getppid());

printf(“child is %d\n”,getpid());

p4=fork();

if(p4==0)

{

printf(“Parent is %d\n”,getppid());

printf(“child is %d\n”,getpid());

return 0;

}

}

}

return 0;

}

OUTPUT

Child is 5263

Parent is 5262

Child is 5263

Child is 5264

Parent is 5264

Child is 5265

Child is 5262

Parent is 5262

Child is 5266

Parent is 5266

Child is 5267

CONCLUSION

The program was executed successfully. 