You are on page 1of 24

Operating Systems

Chapter 3

UNIX PROCESS
(Animations)

Process Creation and Termination

Prepared by:
Muhalim Mohamed Amin

Feb 2014
1
Operating Systems

http://duckopensource.blogspot.com/2012/12/apostila-de-programacao-shell-rubens.html
Unix Process

• Unix shell a
command-line
interpreter that provides
a traditional user
interface.

• Shell creates a new process every time it runs a


program for the command entered by a user.
2
Operating Systems

• Examples 3.1a:
$cat file1 file2
shell creates a process to run command cat

http://s0.cyberciti.org/uploads/faq/2012/02/cat-command-with-numbers.png
3
Operating Systems

• Example 3.1b:
$ls | wc -l
2 processes are created to run ls and wc
concurrently

4
Operating Systems

Tree of Processes in Unix

5
Operating Systems

Creating and Terminating Process


• fork() system call is to create a process:
– pid = fork() – creates a child process where
a child process is a copy of the parent process.
– Called without any argument.

– Will return an integer value to variable pid.


• Parent process – a process id for child process (+ve
value)
• Child process – zero

• exit() system call is to terminate a process


6
Operating Systems

Application of fork()
• To make a duplicate of a process so that a copy will
do one task while another do another task.

• To execute another program


– use exec() system call after fork()

7
Operating Systems

fork()
Before fork() After fork()

printf(“One\n”) printf(“One\n”)
pid = fork() PC pid = fork()
printf(“Two\n”) printf(“Two\n”) PC

printf(“One\n”)
What is the output of pid = fork()
the program? printf(“Two\n”) PC
One
Two
Two
8
Operating Systems

fork(): Example 3.2

pid1 = fork();
pid2 = fork();
print pid1, pid2;

Total number of processes created is 4

9
Operating Systems

Id= 4
pid1 = fork(); pid1 =
pid2 = fork();
pid2 =
print pid1, pid2;

10
Operating Systems

Id= 4
pid1 = fork(); pid1 = 5
1 pid2 = fork();
pid2 =
print pid1, pid2;

Id= 5
pid1 = fork();
2 pid2 =fork(); pid1 = 0
print pid1, pid2; pid2 =

11
Operating Systems

Id= 4
pid1 = fork(); pid1 = 5
pid2 = fork();
pid2 = 6
print pid1, pid2;
Id= 6
pid1 = fork();
pid2 = fork(); pid1 = 5
3 print pid1, pid2; pid2 = 0
Id= 5
pid1 = fork();
pid2 =fork(); pid1 = 0
print pid1, pid2; pid2 =

12
Operating Systems

Id= 4
pid1 = fork(); pid1 = 5
pid2 = fork();
pid2 = 6
print pid1, pid2;
Id= 6
pid1 = fork();
pid2 = fork(); pid1 = 5
3 print pid1, pid2; pid2 = 0
Id= 5
pid1 = fork();
pid2 =fork(); pid1 = 0
print pid1, pid2; pid2 = 7

Id= 7
pid1 = fork();
4 pid2 = fork(); pid1 = 0
print pid1, pid2; pid2 = 0
13
Operating Systems

Id= 4
pid1 = fork(); pid1 = 5
pid2 = fork();
pid2 = 6
print pid1, pid2;
Id= 6
pid1 = fork();
pid2 = fork(); pid1 = 5
print pid1, pid2; pid2 = 0
Id= 5
pid1 = fork();
pid2 =fork(); pid1 = 0 Output:
print pid1, pid2; pid2 = 7 5 6
0 7
Id= 7
5 0
pid1 = fork(); 0 0
pid2 = fork(); pid1 = 0
print pid1, pid2; pid2 = 0
14
Operating Systems

Activity 3.3: Understanding fork()


main(){
int i, pid;
for (i=0; i<3; i++) {
pid=fork())
if (pid< 0) {
printf(“Sorry, cannot fork\n”);
} else if (pid ==0) {
printf (“child %d\n”, i);
} else {
printf(“parent %d\n”, i),}}

Question:
• Trace the program and determine the output and the number
of processes created when the above program is executed.?
15
Operating Systems

id= 3
i= 0

i= 1

i= 2

16
Operating Systems

id= 3 Parent 0

i= 0
id= 4 Child 0

i= 1

i= 2

17
Operating Systems

id= 3 Parent 0 Parent 1

i= 0
id= 4 Child 0 Parent 1

i= 1 Child 1 Child 1
id= 5 id= 6

i= 2

18
Operating Systems

id= 3 Parent 0 Parent 1 Parent 2

i= 0
id= 4 Child 0 Parent 1 Parent 2

i= 1 Child 1 Child 1
id= 5 id= 6
Parent 2 Parent 2

i= 2
id= 7 id= 8 id= 9 id= 10
Child 2 Child 2 Child 2 Child 2

19
Operating Systems

Output Exercise 3.3:

parent 0 child 2
child 0 parent 2
parent 1 child 2
parent 1 parent 2
child 1 child 2
child 1 parent 2
parent 2 child 2

20
Operating Systems

exec()
• Application:
– To execute a new program
• exec() does not create a new process.

• Process id for new program = Process id for a


program that calls the exec() system call.

• If exec() is executed successfully, control will


never return to the program that calls it.

21
Operating Systems

exec() System Call


Char *path, *arg0, *arg1, …, *argn;
int ret

ret = execl(path,arg0,arg1,…,argn,(char*)0);

path – name of a file that contains the program to be executed


arg0 – program name
arg1,…,argn – parameter

22
Operating Systems

exit()
• Application: to terminate a process.
• Format:
int status;

exit(status);

status = 0 ---> normal termination


status = nonzero ---> abnormal

23
Operating Systems

Using exec() and fork()


Main()
{ int pid;
pid = fork();
if (pid>0){
wait((int*)0);
printf(“ls completed\n”);
exit(0); }
if pid == 0){
execl(“/bin/ls”, “ls”,”-l”,(char *)0); }
perror(“fork failed”);
exit(1); }

24

You might also like