Lab 4
Lab 4
Lab 4
CONCEPTS
Linux Processes
A Linux process or task is known as the instance of a program running under Linux environment. This means that if 10
users from servers are running gedit editor, then there are 10 gedit processes running on the server. Although they are
sharing same executable code. The processes running in a Linux system can be view using ‘ps’ command which we
covered.
Process ID
In Linux system, each process running has been assigned a unique ID which is known as PID (Process Identification
Number). For example, Firefox is a running process if you are browsing the internet. Each time you start a new Firefox
browser the system will automatically assign a PID to the new process of Firefox. A PID is automatically assigned when a
new process is created on the system. If you wish to find out the PID of the running process like Firefox you may use the
command ‘pidof’.
$ pidof firefox
$ pidof bash
$ pidof bioset
To view the running process in a form of a tree we can use ‘pstree’
command. Which shows the running process in a form of a tree.
Process Management
In general, a process is an instance of a program written and compiled. There
can be multiple instances of a same program. In other words, a program that
is loaded into computer’s memory and is in a state of execution is called a
process. Processes are dynamic entity. Process essentially requires CPU and
RAM resources but may also require I/O, Network or Printer depending on
the program written.
System Calls
A system call is a mechanism that provides the interface between a process
and the operating system. It is a programmatic method in which a computer
program requests a service from the kernel of the OS. System call offers the
1
LAB # 05
services of the operating system to the user programs via API (Application Programming Interface). System calls are the
only entry points for the kernel system.
Architecture of the System Call
Given System Call example diagram.
Step 1) The processes executed in the user mode till the
time a system call interrupts it.
Step 2) After that, the system call is executed in the kernel-
mode on a priority basis.
Step 3) Once system call execution is over, control returns
to the user mode.,
Step 4) The execution of user processes resumed in Kernel
mode.
Types of System Calls
Here are the five types of System Calls in OS:
1. Process Control
2. File Management
3. Device Management
4. Information Maintenance
5. Communications
Process Control File Management
This system calls perform the task of process creation, File management system calls
process termination, etc. handle file manipulation jobs like
Functions: creating a file, reading, and writing, etc.
1. End and Abort Functions:
2. Load and Execute
1. Create a file
Communication:
These types of system calls are specially used for inter-process communications.
Functions:
1. Create, delete communications connections
2. Send, receive message
2
LAB # 05
3. Help OS to transfer status information
4. Attach or detach remote devices
3
LAB # 05
Fork System Call
In Linux, process is created by duplicating parent process. This is called forking. You invoke the fork system call with fork()
function. Usually A new process is created by the fork() system call. Process may be created with fork () without a new
program being run-the new sub-process simply continues to execute exactly the same program that the first (parent)
process was running. It is one of the most widely used system calls under process management.
Definition
It is a system call that creates a new process under Linux operating system. It takes no argument. The purpose of fork() is
to create a new process which becomes the child process to the caller. After the new child process is created, both
processes will execute next instruction following the fork system call, therefore we have to distinguish the parent process
from the child which can be done by evaluating the returned value of fork() function.
By examining the returned value of fork function, we can can determine the current process is parent or child process.
following are the returned value and their meaning. If the returned value is negative, it means that the child process
creation was unsuccessful. If the returned value is zero, the child process is created with pid = 0. If the returned value is
positive, the child process is created with the process with a process ID to the parent process. The returned process ID is
of type pid_t defined in sys.type.h). Normally this process ID is an integer.
After the system call to fork() is issued, a simple test can tell which process is the child. Note that Linux will make an exact
copy of the parent address space and give it to the child. Therefore, the parent and child processes have separate address
space. Consider the following C language program:
EXCERCISES
Example
Implement the above code and check the output. Compilation of program:
./obj
4
LAB # 05
When the main program executes fork(), an identical copy of its address space, including the program and all data, is
created. System call fork() returns the child process ID to the parent and returns 0 to the child process. The images below
will diagrammatically show the above code execution of forking:
In the above image, the fork function is called, which creates a new process and assign value of pid in parent process and
assign 0 in child process. During if condition, it checked if the pid is zero or not to distinguish between parent and child
process and invoke the functions of each process respectively.
Due to the fact that the CPU scheduler will assign a time quantum to each process, the parent or the child process will run
for some time before the control is switched to the other and the running process will print some lines before you can see
any line printed by the other process. The following codes distinguishes parent and child process working on variable in
5
LAB # 05
shared and separate address spaces: Even though the variable name remain same in child process yet due to separate
address of process the variable is considered a new variable in process.
Execute the program and verify the creation of multiple child. Understand the tree.
6
LAB # 05
7
LAB # 05
Different Kinds of Child Process • Zombie Process
Implement the below program and check the output by opening two terminals. In one terminal, you need to execute your
zom.c program and in second terminal execute ps -al to check the status of child process. As given below. Then find the
parent process id and kill the child process.
8
LAB # 05
Solution to Avoid Creation of Zombie Processes
Using wait() system call we can avoid the creation of zombie process. The below changes to the above code will force the
parent to wait:
9
LAB # 05
What happens when the main make-zombie program ends when the parent process exits, without ever calling wait? Does
the zombie process stay around? No—try running ps again, and note that both of the make-zombie processes are gone.
When a program exits, its children are inherited by a special process, the init program, which always runs with process ID
of 1 (it’s the first process started when Linux boots).The init process automatically cleans up any zombie child processes
that it inherits. Implement the above program and check the output.
Orphan Process:
In general English terms, orphan is someone who lost parents. Same is the story here. If the parent process has finished
execution and exited, but at the same time if the child process remains UN-executed, the child is then termed to be an
orphan. This is done by making the child process sleep for sometimes. By that time of child’s sleep, parent process will
complete its execution and will exit. Since, parent process is no more there, child is referred to be an orphan now.
10
LAB # 05
Implement the given below program and check the child process with ps -al command.
Task: As you do not see child process therefore, us wait system call to the program and again check the
successful execution and return of child to parent.
11
LAB # 05
Execute the above code , you should get output of current directory files.
To avoid the replacement of current process, we can use fork and create child process which will be executing the
other execution units.
Execute the above example and find the output, understand how it works.
12
LAB # 05
Exercises:
1. Write a Program that Creates n-child process from same parent process using fork() in C
2. Write Program to create four processes (1 parent and 3 children) where they terminate in a sequence as
follows:
(a) Parent process terminates at last.
(b) First child terminates before parent and after second child.
(c) Second child terminates after last and before first child.
(d) Third child terminates first.
3. Write a program which creates processes 4 processes for parallel programming. Each parent will wait for the
termination of its child.
4. Write a C program that creates child processes as many times as user wants until user inputs.
zero (use while loop). Also display process id of each child process.
5. Implement the following 9 tree structure. Each node must print its name and PID. e.g. I am Process A and
my PID is 2453
13
LAB # 05