You are on page 1of 5

NAME- KAUSTUBH PATHAK

ENROLLMENT NO - EN19CS301163
CLASS- CS-C
BATCH - C2
SUBJECT- OPERATING SYSTEM(OS)

Experiment 4

Write a program in c to understand the concept of process creation and


process termination.

Theory:

Till now we know that whenever we execute a program then a process is created and would be
terminated after the completion of the execution.
What if we need to create a process within the program and may be wanted to schedule a
different task for it. Can this be achieved?
Yes, obviously through process creation. Of course, after the job is done it would get terminated
automatically or you can terminate it as needed.

Process creation is achieved through the fork() system call.


The newly created process is called the child process and the process that initiated it (or the
process when execution is started) is called the parent process.
After the fork() system call, now we have two processes - parent and child processes.
How to differentiate them? Very simple, it is through their return values.

System Call
After creation of the child process, let us see the fork() system call details.

#include <sys/types.h> //fork()


#include <unistd.h>

pid_t fork(void);

Creates the child process.


After this call, there are two processes, the existing one is called the parent process and the
newly created one is called the child process.

The fork() system call returns either of the three values -


Negative value to indicate an error, i.e., unsuccessful in creating the child process.

Returns a zero for child process.

Returns a positive value for the parent process. This value is the process ID of the newly
created child process.

​Program 1

File name: basicfork.c

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
fork();
printf("Called fork() system call\n");
return 0;
}

Output:

Called fork() system call


Called fork() system call

Program 2: Calculate number of times hello is printed:

#include <stdio.h>
#include <sys/types.h>
int main()
{
fork();
fork();
fork();
printf("hello\n");
return 0;
}
Output:

hello
hello
hello
hello
hello
hello
hello
hello
The number of times ‘hello’ is printed is equal to the number of processes created.
Total Number of Processes = 2n, where n is number of fork system calls. So here n = 3,
2*3 = 8

Program 3: Predict the Output of the following program:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void forkexample()
{
// child process because return value zero
if (fork() == 0)
printf("Hello from Child!\n");

// parent process because return value non-zero.


else
printf("Hello from Parent!\n");
}
int main()
{
forkexample();
return 0;
}
Output:

1.
Hello from Child!
Hello from Parent!
(or)
2.
Hello from Parent!
Hello from Child!
In the above code, a child process is created. fork() returns 0 in the child process and positive
integer in the parent process.
Here, two outputs are possible because the parent process and child process are running
concurrently. So we don’t know whether the OS will first give control to the parent process or the
child process.

Important: Parent process and child process are running the same program, but it does not
mean they are identical. OS allocate different data and states for these two processes, and the
control flow of these processes can be different. See next example:

Ans- The Output of the Program is

Hello from Parent!


Hello from Child!

Predict the Output of the following program:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

void forkexample()
{
int x = 1;

if (fork() == 0)
printf("Child has x = %d\n", ++x);
else
printf("Parent has x = %d\n", --x);
}
int main()
{
forkexample();
return 0;
}
Output:

Parent has x = 0
Child has x = 2
(or)
Child has x = 2
Parent has x = 0
Here, global variable change in one process does not affected two other processes because
data/state of two processes are different. And also parent and child run simultaneously so two
outputs are possible.

Ans- The Output of the Program is -

Parent has x = 0
Child has x = 2

You might also like