Fork, Wait, Exit, Getpid and Getppid Unix System Calls: Lab Manual 07 Process Creation
Fork, Wait, Exit, Getpid and Getppid Unix System Calls: Lab Manual 07 Process Creation
Lab Manual 07
Process Creation
fork, wait, exit, getpid and getppid Unix System Calls
Rida Fatima
rida.fatima@kfueit.edu.pk
Write down the C code in a text editor of your choice and save the file using .c exten-
sion i.e. filename.c. Now through your terminal move to the directory where filename.c
resides.
gcc f i r s t p r o g r a m . c
It will compile the code and by default an executable named a.out is created.
gcc o firstprogram firstprogram .c
If your file is named firstprogram.c then type -o firstprogram as the parameter to gcc. It
would create an executable by the name firstprogram for the source code named
firstpro-gram.c .
To execute the program simply write the following:
. / a . out OR ./firstprogram
In case you have written the code in C++ saved using .cpp extension, compile the code
using g++ as:
g++ f i l e n a m e . cpp OR g++ o exec_name f i l e n a m e . cpp
2 PROCESS CREATION
1. System startup
1. Assign a unique process identifier. Every process has a unique process ID, a
non-negative integer. Because the process ID is the only well-known identifier of
a process that is always unique, it is used to guarantee uniqueness.
2. Allocate space for the process.
An existing process can create a new process by calling the fork function.
# include < u n i s t d . h>
p i d _ t f o r k ( void ) ;
// Returns: 0 in child, process ID o f child in parent,
1 on error
The definition of the pid_t is given in <sys/types> include file and <unistd.h> contain
the declaration of fork system call.
IMPORTANT P OINTS
2. This function is called once but returns twice. The only difference in the returns
is that the return value in the child is 0, whereas the return value in the parent is
the process ID of the new child.
3. Both the child and the parent continue executing with the instruction that follows
the call to fork.
4. The child is a copy of the parent. For example, the child gets a copy of the
parent’s data space, heap, and stack. Note that this is a copy for the child; the
parent and the child do not share these portions of memory.
2
Operating Systems Lab Spring, 2019 KFUEIT, RYK
5. In general, we never know whether the child starts executing before the parent, or
vice versa. The order depends on the scheduling algorithm used by the kernel.
(b) After fork system call both the child and the parent process continue
executing with the instruction that follows the call to fork.
3
Operating Systems Lab Spring, 2019 KFUEIT, RYK
(a) Fork system call returns child process ID (created using fork) in par-
ent process and zero in child process
(b) We can execute different portions of code in parent and child process
based on the return value of fork that is either zero or greater than zero.
4 EXAMPLES OF FORK()
4
Operating Systems Lab Spring, 2019 KFUEIT, RYK
int global=0;
i n t main ( )
{
i nt status;
p i d _ tc h i l d _ p i d ;
i nt loc al = 0;
/ now c r e a t e new process /
child_pid = fork();
e l s e {/ parent process /
printf("parent process!\n");
p r i n t f ( " p a r e n t PID = %d , c h i l d pid = %d\n " ,
g etpi d(), child_pid);
i n t w=wait (& s t a t u s ) ;
/ / The c h a n g e i n local and global
variable
//in child process should not refl ect
//here in parent process.
p r i n t f ( " \n P a r e n t ’ z l o c a l = %d , p a r e n t ’ s g l o b a
l = %d\n " , l o c a l , g l o b a l ) ;
printf("Parent s a y s bye ! \ n " ) ;
5
Operating Systems Lab Spring, 2019 KFUEIT, RYK
else{ / failure /
perror("fork");
exit(0);
}
}
This function blocks the calling process until one of its child processes exits. wait() takes
the address of an integer variable and returns the process ID of the completed process.
p i d _ t wait ( i n t status)
1. If there are at least one child processes running when the call to wait() is made,
the caller will be blocked until one of its child processes exits. At that moment,
the caller resumes its execution
2. If there is no child process running when the call to wait() is made, then this
wait() has no effect at all. That is, it is as if no wait() is there.
EXAMPLE
# include < s t d i o . h>
# include < u n i s t d . h>
# include < s y s / t y p e s . h>
# include < s t d l i b . h>
6
Operating Systems Lab Spring, 2019 KFUEIT, RYK
To run:
./commandlineargument.out abc def 1 2