Khwaja Fareed University of Engineering and Information Technology
Rahim Yar Khan
OPERATING SYSTEMS LAB SPRING 2019
Lab Manual 07
Process Creation
fork, wait, exit, getpid and getppid Unix System Calls
Rida Fatima
rida.fatima@kfueit.edu.pk
1 COMPILE AND EXECUTE A C PROGRAM
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
And execute as ./a.out or ./exec_name
Operating Systems Lab Spring, 2019 KFUEIT, RYK
2 PROCESS CREATION
When is a new process created?
1. System startup
2. Submit a new batch job/Start program
3. Execution of a system call by process
4. A user request to create a process
On the creation of a process following actions are performed:
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.
3. Initialize process control block.
4. Set up appropriate linkage to the scheduling queue.
3 FORK SYSTEM CALL
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
1. The new process created by fork is called the child process.
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.
(a) The process will execute sequen-
tially until it arrives at fork system call.
This process will be named as parent
process and the one created us-ing
fork() is termed as child process.
(b) After fork system call both the child and the parent process continue
executing with the instruction that follows the call to fork.
Figure 1: Parent and Child Process
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.
Figure 2: Return Value of fork system call
4 EXAMPLES OF FORK()
1. Fork()’s Return Value
# 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>
void main ( void ) {
p i d _ t pid= f o r k ( ) ;
i f ( pid == 0 ) { / / T h i s c h e c k w i l l p a s s o n l y f o r c h i l d p r o c e s
p r i n t f ( " I am Child s p r o c e s s \n " ) ;
4
Operating Systems Lab Spring, 2019 KFUEIT, RYK
e l s e i f ( pid > 0 ) { / / THIS C h e c k w i l l p a s s o n l y f o r p a r e n t
p r i n t f ( " I am P a r e n t p r o c e s s \n " ) ;
}
e l s e i f ( pid < 0 ) { // if fork() fails
p r i n t f ( " E r r o r i n Fork " ) ;
}
}
2. Manipulating Local and Global Variables
# include < u n i s t d . h>
# include < s y s / t y p e s . h>
# include < e r r n o . h>
# include < s t d i o . h>
# include < s y s /wait . h>
# include < s t d l i b . h>
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();
i f ( c h i l d _ p i d >= 0 ) { / fork succeeded /
i f ( c h i l d _ p i d == 0 ) {
/fork()returns0forthechi ldprocess/pri
ntf("childprocess!\n");
// Increment the local and global
variables
l o c a l ++;
g l o b a l ++;
p r i n t f ( " c h i l d PID = %d , p a r e n t pid = %d\n " ,
g e t p i d ( ) , getppid ( ) ) ;
p r i n t f ( " \n c h i l d ’ s l o c a l = %d , c h i l d ’ s
g l o b a l = %d\n " , l o c a l , g l o b a l ) ;
}
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
exit(0); / parent exits /
}
}
else{ / failure /
perror("fork");
exit(0);
}
}
5 WAIT SYSTEM CALL
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)
Include <sys/wait.h> and <stdlib.h> for definition of wait().
The execution of wait() could have two possible situations
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>
void main ( void ) {
i n t status=0;
p i d _ t pid= f o r k ( ) ;
i f ( pid == 0 ) { / / T h i s c h e c k w i l l p a s so n l y f o r c h i l d process
p r i n t f ( " I am Child p r o c e s s with pid %d and i
am not
w a i t i n g \n " , g e t p i d ( ) ) ;
exit(status);
}
e l s e i f ( pid > 0 ) { / / THIS C h e c k w i l l p a s so n l y for parent
p r i n t f ( " I am P a r e n t process with pid %d and
i am w a i t i n g \n " , g e t p i d ( ) ) ;
p i d _ t e x i t e d C h i l d I d =wait (& s t a t u s ) ;
p r i n t f ( " I am P a r e n t p r o c e s s and t h e c h i l d with
pid %d
i s e x i t e d \n " , e x i t e d C h i l d I d ) ;
}
e l s e i f ( pid < 0 ) { // i f f or k( ) fails
6
Operating Systems Lab Spring, 2019 KFUEIT, RYK
p r i n t f ( " E r r o r i n Fork " ) ;
}
6 EXIT SYSTEM CALL
A computer process terminates its execution by making an exit system call.
#include < s t d l i b . h>
i n t main ( ) {
exit(0);
}
7 COMMAND LINE ARGUMENT TO C PROGRAM
void main ( i n t argc , char argv [ ] ) {
/ a r g cnumber o f a r g u m e n t s /
int i ;
for(i=0;i<argc;i++){
p r i n t f ( " The argument a t %d index i s %s\n " , i , argv [ i ] ) ;
}
}
To run:
./commandlineargument.out abc def 1 2