Programs Using System Calls EX - NO: 2a Fork System Call Date: AIM
Programs Using System Calls EX - NO: 2a Fork System Call Date: AIM
AIM
To write a UNIX C program to create a child process from parent process using fork()
system call.
ALGORITHM
PROGRAM 1
#include<stdio.h>
#include<unistd.h>
main()
{
fork();
printf("Hello World\n");
}
OUTPUT
#include<stdio.h>
#include<unistd.h>
main()
{
fork();
fork();
fork();
printf("Hello World\n");
}
OUTPUT
RESULT
Thus a UNIX C program to create a child process from parent process using fork()
system call is executed successfully.
EX.NO: 2b SIMULATION OF FORK, GETPID AND WAIT SYSTEM CALLS
DATE:
AIM
To write a UNIX C program simulate fork(),getpid() and wait() system call.
ALGORITHM
Step 1: Invoke a fork() system call to create a child process that is a duplicate of parent process.
Step 2: Retrieve the process id of parent and child process.
Step 3: If return value of the fork system call=0(i.e.,child process),generate the fibonacii series.
Step 4: If return value of the fork system call>0(i.e.,parent process),wait until the child
completes.
PROGRAM
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<conio.h>
main()
{
int a=-1,b=1,i,c=a+b,n,pid,cpid;
printf("\nenter no. of terms ");
scanf("%d",&n);
pid=getpid();
printf("\nparent process id is %d",pid);
pid=fork();
cpid=getpid();
printf("\nchild process:%d",cpid);
if(pid==0)
{
printf("\nchild is producing fibonacci series ");
for(i=0;i<n;i++)
{
c=a+b;
printf("\n%d",c);
a=b;
b=c;
}
printf("\nchild ends");
}
else
{
printf("\nparent is waiting for child to complete");
wait(NULL);
printf("\nparent ends");
}
}
OUTPUT
[it27@mm4 ~]$ cc fork.c
[it27@mm4 ~]$ a.out
RESULT
Thus a UNIX C program to simulate fork(),getpid() and wait() system call is executed
successfully.