You are on page 1of 31

Operating Systems

Chapter 2b

UNIX PROCESS

Creating & Terminating Process


Chapter 2

1
Operating Systems

Unix
• A multitasking, multi-user computer operating
system.

• Developed in 1969 by a group of AT&T (American


Telephone & Telegraph Company) employees at
Bell Labs.

• First developed using assembly language before


entirely in C by 1973.
Chapter 2

2
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.
Chapter 2

• Shell creates a new process every time it runs a


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

• Examples 2.1a:
$cat file1 file2
shell creates a process to run command cat
Chapter 2

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

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

5
Operating Systems

Tree of Processes in Unix


Chapter 2

6
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
Chapter 2

• exit() system call is to terminate a process


7
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()
Chapter 2

8
Operating Systems

fork()
Before fork() After fork()

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

printf(“One\n”)
What is the output of pid = fork()
printf(“Two\n”)
Chapter 2

the program? PC
One
Two
Two
9
Operating Systems

fork(): Example 2.2

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

Total number of processes created is 4

10
Operating Systems

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

Id= 6
pid1 = fork();
pid2 = fork(); pid1 = 5
3 print pid1, pid2; pid2 = 0
Id= 5
pid1 = fork();
2 pid2 =fork(); pid1 = 0 pid1 = 0 Output:
print pid1, pid2; pid2 = ? pid2 = 7 5 6
0 7
Chapter 2

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

Example 2.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),}}
Chapter 2

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

fork(): Example 2.3


id= 3
i= 0
id= 4

i= 1
id= 5 id= 6

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

13
Operating Systems

Output Example 2.3:

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

parent 2

14
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.
Chapter 2

• If exec() is executed successfully, control will


never return to the program that calls it.

15
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
Chapter 2

arg1,…,argn – parameter

16
Operating Systems

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

exit(status);

status = 0 ---> normal termination


Chapter 2

status = nonzero ---> abnormal

17
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); }
Chapter 2

perror(“fork failed”);
exit(1); }
18
Operating Systems

THREAD CONCEPT
Chapter 2

19
Operating Systems

Introduction
• Multiple actions executing simultaneously
a) Heavyweight process (traditional process) Processes with
multiple
• Owns the resources subprocesses
• Passive element
b) Lightweight process (Thread)
Proces with
• Uses CPU and scheduled for execution multiple threads

• Active element
c) Multithreaded applications programs
• Contain several threads running at one time
• Same or different priorities
Chapter 2

• Examples:
– Web browsers and time-sharing systems
20
Operating Systems

Thread
• Thread: a basic unit of CPU utilization;
• It composes of:
» Program counter
» Set of registers
» Stack
• A thread has its own thread ID.
• Thread shares the following items with other threads
that are created in the same process:
» code section
Chapter 2

» data section
» Other operating system resources

21
Operating Systems

• A group of threads is called a process or task.


• Traditional process (or heavyweight process) is
similar to a task with a single thread of control.

http://smart-dzikra.blogspot.com/2010/10/thread-states.html
Chapter 2

22
Operating Systems

Components of Threads vs. Process

• Items shared by all threads in a process


Chapter 2

• Items private to each thread

23
Operating Systems

http://smart-dzikra.blogspot.com/2010/10/thread-states.html
Chapter 2

Figure: Different process models and thread control in a single


thread and multithreaded application
24
Operating Systems

Applications
• Use by many software packages that run on
modern desktop PCs.

Word Processor a thread for displaying graphic,


another thread for reading keystrokes
Web browser one thread displays image or text,
while another thread retrieves data
from the network
Web server when server receives a request, a
Chapter 2

thread will be created to service that


request

25
Operating Systems

Benefits of Using Threads (1/3)

• Increase responsiveness

– When one thread in multithreading application is


blocked or performing a lengthy operation, the
application process can still continue running.

– Eg: a multithreaded web browser could still allow


user interaction in one thread while an image is
being loaded in another thread.
Chapter 2

26
Operating Systems

Benefits of Using Threads (2/3)

• Does not need special mechanism for resource


sharing

– Threads share the memory and the resources of the


process to which they belong.

– Therefore a thread can read data which was written


by another thread into the memory.
Chapter 2

27
Operating Systems

Benefits of Using Threads (3/3)

• Process creation and context switching is more


economical

– Threads in one process share resources, eg.


Memory. Therefore less time consuming to create a
thread as compared to creating a process

– Example: Solaris OS
• process creation is 30 times slower than thread
Chapter 2

creation and
• context switching is about 5 times slower.
28
Operating Systems

EXAMPLE:

Screenshot from
MacBook Pro
Chapter 2

29
Chapter 2 Operating Systems

30
Chapter 2 Operating Systems

31

You might also like