You are on page 1of 3

CHANDIGARH UNIVERSITY UID: 16BCS2994

PRACTICAL: III
AIM:

Programs using the following system calls of Linux Operating


System: fork, getpid, getppid, exit, wait, and close.

Application of Linux System Calls:

1. getpid()
getpid() returns the process ID of the calling process. (This is often used by routines
that generate unique temporary filenames.)

2. getppid()
getppid() returns the process ID of the parent of the calling process. This will be
either the ID of the process that created this process using fork(), or, if that process
has already terminated, the ID of the process to which this process has been
reparented.

3. fork()
fork() creates a new process by duplicating the calling process. The new process is
referred to as the child process. The calling process is referred to as the parent
process. The child process and the parent process run in separate memory spaces. At
the time of fork() both memory spaces have the same content.

4. wait()
wait() blocks the calling process until one of its child processes exits or a signal is
received. wait() takes the address of an integer variable and returns the process ID of
the completed process.

5. exit()
exit() terminates the calling process "immediately". Any open file descriptors
belonging to the process are closed; any children of the process are inherited by
process 1.

14
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING
CHANDIGARH UNIVERSITY UID: 16BCS2994

Program Code:
#include <stdio.h>

#include<sys/types.h>

#include<unistd.h>

int main()

int a=fork();

if(a==0)

printf("\nHello i m child process my id is %d",getpid());

printf("\nMy parent id is %d",getppid());

else if(a>0)

wait();

printf("\nHello i m parent process my id is %d",getpid());

printf("\nMy child id is %d",a);

else

{printf("Error has occured");}

exit(0);

return 0;

15
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING
CHANDIGARH UNIVERSITY UID: 16BCS2994

Output:

16
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING