Professional Documents
Culture Documents
CH 3 Process Concept UNIX EDITED
CH 3 Process Concept UNIX EDITED
UNIX PROCESS
(Animations)
Process Creation and Termination
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
UNIX Process
http://duckopensource.blogspot.com/2012/12/apostila-de-programacao-shell-rubens.html
Unix shell a command-
line interpreter that provides
a traditional user interface.
http://s0.cyberciti.org/uploads/faq/2012/02/cat-command-with-numbers.png
Operating System Concepts – 9th Edition 2.4 Silberschatz, Galvin and Gagne ©2013
Tree of Processes in Unix
Operating System Concepts – 9th Edition 2.5 Silberschatz, Galvin and Gagne ©2013
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.
Operating System Concepts – 9th Edition 2.6 Silberschatz, Galvin and Gagne ©2013
Application of fork()
To make a duplicate of a process so that a copy will do one
task while another do another task.
Operating System Concepts – 9th Edition 2.7 Silberschatz, Galvin and Gagne ©2013
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
Operating System Concepts – 9th Edition 2.8 Silberschatz, Galvin and Gagne ©2013
fork(): Example 3.1
pid1 = fork();
pid2 = fork();
print pid1, pid2;
Operating System Concepts – 9th Edition 2.9 Silberschatz, Galvin and Gagne ©2013
Id= 4
pid1 = fork(); pid1 =
pid2 = fork(); pid2 =
print pid1, pid2;
Operating System Concepts – 9th Edition 2.10 Silberschatz, Galvin and Gagne ©2013
Id= 4
pid1 = fork(); pid1 = 5
1 pid2 = fork(); pid2 =
print pid1, pid2;
Id= 5
pid1 = fork();
2 pid2 =fork(); pid1 = 0
pid2 =
print pid1, pid2;
Operating System Concepts – 9th Edition 2.11 Silberschatz, Galvin and Gagne ©2013
Id= 4
pid1 = fork(); pid1 = 5
pid2 = fork(); pid2 = 6
print pid1, pid2;
Id= 6
pid1 = fork();
pid1 = 5
pid2 = fork(); pid2 = 0
3 print pid1, pid2;
Id= 5
pid1 = fork();
pid2 =fork(); pid1 = 0
pid2 =
print pid1, pid2;
Operating System Concepts – 9th Edition 2.12 Silberschatz, Galvin and Gagne ©2013
Id= 4
pid1 = fork(); pid1 = 5
pid2 = fork(); pid2 = 6
print pid1, pid2;
Id= 6
pid1 = fork();
pid1 = 5
pid2 = fork(); pid2 = 0
3 print pid1, pid2;
Id= 5
pid1 = fork();
pid2 =fork(); pid1 = 0
pid2 = 7
print pid1, pid2;
Id= 7
pid1 = fork();
4 pid2 = fork(); pid1 = 0
pid2 = 0
print pid1, pid2;
Operating System Concepts – 9th Edition 2.13 Silberschatz, Galvin and Gagne ©2013
Id= 4
pid1 = fork(); pid1 = 5
pid2 = fork(); pid2 = 6
print pid1, pid2;
Id= 6
pid1 = fork();
pid1 = 5
pid2 = fork(); pid2 = 0
print pid1, pid2;
Id= 5
pid1 = fork();
pid2 =fork(); pid1 = 0
pid2 = 7 Output:
print pid1, pid2; 5 6
0 7
Id= 7
5 0
pid1 = fork(); 0 0
pid2 = fork(); pid1 = 0
pid2 = 0
print pid1, pid2;
Operating System Concepts – 9th Edition 2.14 Silberschatz, Galvin and Gagne ©2013
exec ()
Application:
To execute a new program
exec() does not create a new process.
Operating System Concepts – 9th Edition 2.21 Silberschatz, Galvin and Gagne ©2013
exec()System Call
ret = execl(path,arg0,arg1,…,argn,(char*)0);
Operating System Concepts – 9th Edition 2.22 Silberschatz, Galvin and Gagne ©2013
exit()
int status;
exit(status);
Operating System Concepts – 9th Edition 2.23 Silberschatz, Galvin and Gagne ©2013
Using exec()and fork()
int 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); }
Operating System Concepts – 9th Edition 2.24 Silberschatz, Galvin and Gagne ©2013