0% found this document useful (0 votes)
17 views

4 Processes

Uploaded by

manjunathsg85
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

4 Processes

Uploaded by

manjunathsg85
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 38

CS241

Processes
R:Ch 3 p60-77, T:Ch2 pp 71-77

University of Illinois at Urbana-Champaign

Professor: Roy Campbell

08/13/24 CS241 © 2005 Roy Campbell, All R 1


ights Reserved
Discussion
 Why is a process a basic mechanism
for structuring software?
 How many different views could one
have of a process?

08/13/24 CS241 © 2005 Roy Campbell, All R 2


ights Reserved
Contents

 Process management
 Interference and shared variables
 Reentrant code and Coroutines
 Motivation for a process
 Fork, join process notation
 Process: An object with state

08/13/24 CS241 © 2005 Roy Campbell, All R 3


ights Reserved
Process Management
 An operating system involves
asynchronous, and sometimes
parallel, activities
 The notion of a software process is
used in operating systems to express
the management and control of such
activities

08/13/24 CS241 © 2005 Roy Campbell, All R 4


ights Reserved
Process Management
 Process is also used to describe
concurrent user activities and many
forms of parallel computation
 A process is one of the key concepts
underlying modern operating system
design

08/13/24 CS241 © 2005 Roy Campbell, All R 5


ights Reserved
An Abstraction
The process is an abstraction of the
sequence of instructions that are
executed by the processor because
the processor may need to service
hardware devices by executing their
device drivers in between the
instructions of the process

08/13/24 CS241 © 2005 Roy Campbell, All R 6


ights Reserved
An Abstraction
 In a time-sharing system, the actual
execution of the instructions of a process
may become interleaved with other
instructions of other processes
 In a multiprocessor system, the concurrent
processes may each execute on an
independent processor

08/13/24 CS241 © 2005 Roy Campbell, All R 7


ights Reserved
Thread of Control

 The path that a computation taken


through a program corresponding to
the individual statements that are
executed by a particular process

08/13/24 CS241 © 2005 Roy Campbell, All R 8


ights Reserved
Locus of Control

 The end of the thread of control of a


process marking the instruction that
will be executed next

08/13/24 CS241 © 2005 Roy Campbell, All R 9


ights Reserved
Concurrent Program

 A program that has many processes

08/13/24 CS241 © 2005 Roy Campbell, All R 10


ights Reserved
Discussion
 What mechanisms are needed to
implement a process?
 How does hardware architecture
support the concept of a process?
 How has the notion of a process
evolved?

08/13/24 CS241 © 2005 Roy Campbell, All R 11


ights Reserved
History of the Concept of
Process
 Programs
 Re-entrant code
 Co-routines
 Processes
 Threads

08/13/24 CS241 © 2005 Roy Campbell, All R 12


ights Reserved
Reentrant Code
Two or more processes may invoke and
execute a common procedure concurrently.

Such procedures must use reentrant code to


store process specific data in storage
locations that are local to the process.

08/13/24 CS241 © 2005 Roy Campbell, All R 13


ights Reserved
Reentrant Code

08/13/24 CS241 © 2005 Roy Campbell, All R 14


ights Reserved
Co-routines
 Single processor -- to cope with
asynchronous tasks
 Co-routines are interdependent
 They structure a program that contains
several concurrent sequential activities
 The thread of control winds between co-
routines in a program

08/13/24 CS241 © 2005 Roy Campbell, All R 15


ights Reserved
Co-routines Control
 The locus of control is transferred between
the co-routines explicitly
 The suspend primitive transfers activity
from the current thread in the current co-
routine to a different thread in another co-
routine
 The resume primitive returns activity to a
thread in a co-routine that had previously
suspended itself

08/13/24 CS241 © 2005 Roy Campbell, All R 16


ights Reserved
Consumer/producer Co-routine

08/13/24 CS241 © 2005 Roy Campbell, All R 17


ights Reserved
Consumer/producer
Thread of Control

08/13/24 CS241 © 2005 Roy Campbell, All R 18


ights Reserved
Motivation for a Process
 Co-routine
 Co-routines specify concurrency of
activities but they do not provide a
mechanism with which to program
parallelism on a multiprocessor
 Co-routines require explicit exchange of
locus of control

08/13/24 CS241 © 2005 Roy Campbell, All R 19


ights Reserved
Motivation for a Process

 Process
 Each process has an ``abstract'' locus of
control
 Any exchanges of control are done
automatically and implicitly
 Runs in parallel on multiprocessor

08/13/24 CS241 © 2005 Roy Campbell, All R 20


ights Reserved
Parallelism
2
Consider the problem of calculating  b  b  4ac
2a

08/13/24 CS241 © 2005 Roy Campbell, All R 21


ights Reserved
Root of Quadratic Equation

08/13/24 CS241 © 2005 Roy Campbell, All R 22


ights Reserved
Parallel Quadratic Equation
Root

08/13/24 CS241 © 2005 Roy Campbell, All R 23


ights Reserved
Processes
The Multiprogramming Process Model

 Multiprogramming of four programs


 Conceptual model of 4 independent, sequential processes
 Only one program active at any instant

08/13/24 CS241 © 2005 Roy Campbell, All R 24


ights Reserved
Process Creation
Principal events that cause process creation
1. System initialization
2. Execution of a process creation system
call request:
a) User request to create a new process
b) Initiation of a batch job

08/13/24 CS241 © 2005 Roy Campbell, All R 25


ights Reserved
Process Termination
Conditions which terminate processes
1. Normal exit (voluntary)
2. Error exit (voluntary)
3. Fatal error (involuntary)
4. Killed by another process (involuntary)

08/13/24 CS241 © 2005 Roy Campbell, All R 26


ights Reserved
Process Hierarchies
 Parent creates a child process, child
processes can create its own process
 Forms a hierarchy
 UNIX calls this a "process group"
 Windows has no concept of process
hierarchy
– all processes are created equal

08/13/24 CS241 © 2005 Roy Campbell, All R 27


ights Reserved
Fork, Join Process Notation
 Fork The fork primitive creates a named
parallel process that executes a designated
code fragment
 Join The join primitive waits for a parallel
process created by a fork to terminate
 Quit terminates a forked process

08/13/24 CS241 © 2005 Roy Campbell, All R 28


ights Reserved
Fork/join Example

process Main
fork A; t1 = b*b; join A
t4 = t1 - t3; t5 = sqrt(t4)
fork B; fork C; t6 = -b - t5; join B; join C
fork D; t10 = t6/t7; join D
end process Main

08/13/24 CS241 © 2005 Roy Campbell, All R 29


ights Reserved
Fork/join Example

process A
t2 = 4 * a; t3 = t2 * c
end process A

08/13/24 CS241 © 2005 Roy Campbell, All R 30


ights Reserved
Fork/join Example

process B
t7 = 2 * a
end process B
process C
t8 = -b + t5
end process C

08/13/24 CS241 © 2005 Roy Campbell, All R 31


ights Reserved
Fork/join Example

process D
t9 = t8/t7
end process D

08/13/24 CS241 © 2005 Roy Campbell, All R 32


ights Reserved
Creating a Process in Unix
#include <stdio.h>
#include <unistd.h>

int main(void) {
int x;

x = 0;
fork();
x = 1;
printf("I am process %ld and my x is %d\n", (long)getpid(), x);
return 0;
}

08/13/24 CS241 © 2005 Roy Campbell, All R 33


ights Reserved
Waiting for a child to finish
#include <errno.h>
#include <sys/wait.h>

pid_t r_wait(int *stat_loc) {


int retval;

while (((retval = wait(stat_loc)) == -1) && (errno == EINTR)) ;


return retval;
}

08/13/24 CS241 © 2005 Roy Campbell, All R 34


ights Reserved
Codes
The following macros are available for checking the return
status of a child:
#include <sys/wait.h>
WIFEXITED(int stat_val)
WEXITSTATUS(int stat_val)
WIFSIGNALED(int stat_val)
WTERMSIG(int stat_val)
WIFSTOPPED(int stat_val)
WSTOPSIG(int stat_val)
They are used in pairs.
If WIFEXITED returns true, the child executed normally
and the return status (at most 8 bits) can be gotten with WEXITSTATUS.

08/13/24 CS241 © 2005 Roy Campbell, All R 35


ights Reserved
Wait details
wait:
If a child terminated, return its pid
Otherwise return -1 and set errno

waitpid:
Allows you to wait for a particular process, or all process if pid is -1.
Important option is NOHANG which will return 0 if there is a
specified child to wait for but it has not yet terminated.

Important values of errno:


ECHILD no unwaited for children
EINTR a signal was caught
The status value is 0 if and only if the process terminated normally
and returned 0. In all other cases the status should be examined
using the provided macros defined in sys/wait.h.

08/13/24 CS241 © 2005 Roy Campbell, All R 36


ights Reserved
Typical process creation code
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main (void) {
pid_t childpid;
/* set up signal handlers here ... */
childpid = fork();
if (childpid == -1) {
perror("Failed to fork");
return 1;
}
if (childpid == 0)
fprintf(stderr, "I am child %ld\n", (long)getpid());
else if (wait(NULL) != childpid)
fprintf(stderr, "A signal must have interrupted the wait!\n");
else
fprintf(stderr, "I am parent %ld with child %ld\n", (long)getpid(),
(long)childpid);
return 0;
}
08/13/24 CS241 © 2005 Roy Campbell, All R 37
ights Reserved
Summary
•Process management
•Reentrant code
•Coroutines
•Motivation for a process
•Fork, join process notation
•C Code

08/13/24 CS241 © 2005 Roy Campbell, All R 38


ights Reserved

You might also like