Session 3 : Operating System Programming Concepts : Lab 3 - Process Management


Lab 3 : Process Management 

Thought of todaY : 
A child on farm sees plane fly overhead 
& dreamz of a far away place. 
D Pilot on d plane sees d farmhouse 
& dreamz of his home. 
Dats life!:) "

Q?What is a processes 
/> It is an instance of a program
in memeory 
/> any program execution creates a process
/> Program: can be command, a shell script, or any binary executable or
any application

Q?What are the process attributes 
/>They are : 
/1>PID : process id 
 It is used by the kernel to identify the process similar to how the
inode number is used for file identification. 
/2>PPID : parent process id 
 The process which creates a process is the parent process, and the
process being created is the child process. The pid of the parent
process is called the parent process id. 
/3>TTY: TeleTYpewriter
 The terminal to which the process is associated to.
 There are some processes which do not belong to any terminal(daemons → it
is a computer program that runs as a background process).
/4>UID(User Id)
 The user to whom the process belongs to .
/5>File Descriptors 
 It is an abstract indicator for accessing a file. The file descriptors
related to the process are : input, output and error file
descriptors. 

Q?How to create a process 
/>We use the system call fork() to create processes. 
/>This fork() system call takes no arguments. 
/>fork() returns : 
                0 to child process
 PID to Parent process
 -1 if an error occurs 

Q?which process terminates first 
/>When we use the fork() system call it creates an identical copy of the
child process. They execute in parallel calling a system call exit(0).

Example : 1 children creation

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


int main(){
pid_t pid;
fprintf(stdout,"Parent: my PID is %d : my parent is process %d\n ",getpid(), getppid());
pid = fork();

if(pid == 0){
//child
fprintf(stdout,"id returned to child: %d\n",pid);
}else{
//parent
fprintf(stdout,"id returned to parent: %d\n",pid);
}

exit(0);
}

Example: 2 children creation 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


int main(){
pid_t pid1;
pid_t pid2;

fprintf(stdout,"Parent: my PID is %d : my parent is process %d\n ",getpid(), getppid());
pid1 = fork();

if(pid1 == 0){
//child1
fprintf(stdout,"id returned to child: %d\n",pid1);
}else{
//parent
fprintf(stdout,"id returned to parent- First child: %d\n",pid1);
pid2 = fork();
if(pid2==0){
//child2
fprintf(stdout,"id returned to child: %d\n",pid2);
}else{
//parent
fprintf(stdout,"id returned to parent-second child : %d\n",pid2);
}
}

exit(0);
}



Q?What is system call sleep()
/>unsigned sleep(unsigned seconds);
/>when the requested time has elapsed the sleep() returns 0
/>when sleep returns due to delivery of a signal, the sleep() returns the
“unslept” amount (the requested time minus the time actually slept) in seconds 

Q?In how many ways can a process run 
/>We can run process in two ways 
/1>foreground processes : 
 - By default every process that we start run in the foreground 
 - It gets its input from the keyboard and sends it output to the screen
 - while the program is running in foreground and taking much time, we
cannot run/start any other processes  
ex:$ ls

/2>background processes : 
 - The advantage of running a process in the background is that you can
run other commands. We actually do not have to wait unitl it
completes to start another.
 - The way to start a background process is to use &

ex:$ ls &


Commands for Process Management 
//for normal listing of the running processes 
$ ps 

//process options we can use in ps command are 
-a: shows information about all users 
-x: shows information about processes without terminals 
-u: shows additional information like -f option
-e: display extended information

//for the full-format listing of the running processes 
$ ps -f 

the result is : 
UID PID PPID C STIME TTY TIME CMD 

UID  : User ID that this process belongs to 
PID  : process id 
PPID : parent process ID (the id of the process that started it)
C    : cpu utilization of process 
STIME: process start time 
TTY  : terminal type associated with the process 
TIME : cpu time taken by the process 
CMD  : the command that started this process 

#Stop the process 
//if the processes is running in foreground mode 
 $ CTRL + C keystroke 

//if the process is running in background mode 
$ kill -9 PID 
Note*:to get the pid of process u need to kill use the ps command



Lab 3 : Process Management 

Learning goals: this laboratory activity is useful to understand how to
create child processes using the fork function. fork() creates a new
process by duplicating the calling process. 
The new process, referred to as the child, is an exact duplicate of the
calling process, referred to as the parent. 
On success, the PID of the child process is returned in the parent, and
0 is returned in the child. 
On failure, -1 is returned in the parent. 

Exercise 1 
Make a program that forks a single child process. 
1.The child has to show his PID, the parent PID and sleep for n seconds
(n has to be passed as command line parameter) 
2.Before the child process exits, it asks for an 8-bit number from the
user that will be used as its return code. 
3.The parent waits for the child to exit. It retrieves the child's exit
code, and displays it. The parent then print his child PID and exits shortly thereafter. 
4.Compare the child PID printed by parent with ones printed by child
itself. 

Exercise 2 
Compile and run the Exercise 1 program. Once the program is running do the
following: 
1.Search the PID of the process using the ps command: 
a.From the same shell used to run the program; 
b.From a different shell; 
2.Run 4 instances of the program in parallel (using &) and: 
a.Use ps to figure out all the process PIDs; 
b.Choose and terminate five of those processes from the same shell; 
c.Choose and terminate the other five processes from a different shell;

3.Move to a different directory and run the program: 
a.Using a relative path; 
b.Using an absolute path; 
c.Using a softlink; 

Exercise 3 
Write a C program which receives two command line parameters, n and t. The
parent process has to initialize a counter, create two child
processes and exit. Each one of the two child processes has to
increment the counter, print it and create again two child processes
and terminate immediately after. The process of creating childs stops
after creating 2^n leaves processes. So for example, if n=2, the
parent creates two child processes, and each child creates two
processes for a total number of 4 leaves processes. At this point
process creation stops. The last set of child processes (leaf child
processes) have to increment their counter and sleep for t seconds
before printing it. Be careful, each process creates two child
processes but only leaf processes are allowed to wait. What’s the
order in which processes terminate? Is the order always the same? 

Summary

Once the lab activity is complete you should have understood how fork
works. Moreover you should have understood how processes and
parent-child relationships are identified. It is important to
understand that after fork is called, two independent flows of
execution exist, one for the parent and one for the child.
 

Comments

Popular Posts