You are on page 1of 2

1.

Write programs using the following system calls of UNIX operating system:
fork, exec, getpid, exit

Aim :
To write a C program using the system calls - fork, exec, getpid, exit

Algorithm :

1. Start the program


2. Include header files
3. Create a process using system call
4. Check if result of fork is successful
5. if fork() returns a negative value, the creation of a child process was unsuccessful
6. if fork() returns a zero value, the new child process is created
7. Moreover, a process can use function getpid() to retrieve the process ID assigned to this process.
8. Stop the program

Result :

Thus the program using the system calls - fork, exec, getpid, exit is executed and verified

SYSTEM CALLS USING FORK,GETPID,EXIT

Source Code:-

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

www.Technicalsymposium.com
pid=fork();
if(pid==-1)
{
printf(“Error in fork”);
exit(0);
}
if(pid==0)
{
pid1=fork();
exit(0);
}
printf(“\n parent id: %d”,getppid());
printf(“\n Child id : %d”,getpid());
}

OUTPUT:-

Parent id : 3233

Child id : 3664

www.Technicalsymposium.com

You might also like