E N D
1. 1 2. Processes A process is the activity of executing a program on a CPU. Also, called a “task.”
Conceptually, each process has its own CPU
Conceptually, processes are running concurrently
Physical concurrency = parallelismIt requires multiple CPUs
Logical concurrency = time-shared CPU
Processes compete or cooperate
Processes compete and cooperate
2. 2 Why Use Process Structure? Hardware-independent solutions
Processes cooperate and compete correctly, regardless of the number of CPUs
Structuring mechanism
Tasks are isolated with well-defined interfaces
3. 3 Defining/Instantiating ProcessesExamples of Precedence Relationships
4. 4 Process flow graphs Serial execution is expressed as: S(p1, p2, …)
Parallel execution is expressed as: P(p1, p2, …)
Figure 2.1(c) represents the following:
S(p1, P(p2, S(p3, P(p4, p5)), p6), P(p7, p8))
5. 5 Process flow graphs (a + b) * (c + d) - (e / f) gives rise to
6. 6 Process flow graphs Compare
7. 7 Implicit Process Creation Processes are created dynamically using language constructs; no process declaration
cobegin/coend
syntax: cobegin C1 // C2 // … // Cn coend
meaning:
All Ci may proceed concurrently
When all terminate, the statement following cobegin/coend continues
8. 8 cobegin/coend cobegin/coend have the same expressive power as S/P notation
S(a,b) ? a; b (sequential execution by default)
P(a,b) ? cobegin a // b coend
9. 9 cobegin/coend example cobegin
Time_Date // Mail // Edit; cobegin
Compile; Load; Execute // Edit; cobegin Print // Web coend
coend
coend;
10. 10 Data parallelism Same code is applied to different data
The forall statement
syntax: forall (parameters) statements
Meaning:
Parameters specify set of data items
Statements are executed for each item concurrently
11. 11 The forall statement Example: Matrix Multiply
forall ( i:1..n, j:1..m )
A[i][j] = 0;
for ( k=1; k<=r; ++k )
A[i][j] = A[i][j] +
B[i][k]*C[k][j];
Each inner product is computed sequentially
All inner products are computed in parallel
12. 12 The fork and join primitives cobegin/coend are limited to properly nested graphs
forall is limited to data parallelism
fork/join can express arbitrary functional parallelism (any process flow graph)
13. 13 The fork and join primitives Syntax: fork xMeaning: create new process that begins executing at label x
Syntax: join t,yMeaning:
t = t–1;
if (t==0) goto y;
14. 14 The fork and join primitives Example: Graph in Figure 2-1(d)
t1 = 2; t2 = 3;
p1; fork L2; fork L5;
fork L7; quit;
L2: p2; fork L3; fork L4; quit;
L5: p5; join t1,L6; quit;
L7: p7; join t2,L8; quit;
L4: p4; join t1,L6; quit;
L3: p3; join t2,L8; quit;
L6: p6; join t2,L8; quit;
L8: p8; quit;
15. 15 The Unix fork procid = fork()
Replicates calling process
Parent and child are identical except for the value of procid
Use procid to diverge parent and child:
if (procid==0)do_child_processing
else do_parent_processing
16. 16 Process Concept An operating system executes a variety of programs:
Batch system – jobs
Time-shared systems – user programs or tasks
Textbook uses the terms job and process almost interchangeably.
Process – a program in execution; process execution must progress in sequential fashion.
A process includes:
program counter
stack
data section
17. 17 UNIX Processes
Each process has its own address space
– Subdivided into text, data, & stack segment
– a.out file describes the address space
OS kernel creates descriptor to manage
process
Process identifier (PID): User handle for
the process (descriptor)
18. 18 Creating/Destroying Processes UNIX fork() creates a process
– Creates a new address space
– Copies text, data, & stack into new adress space
– Provides child with access to open files
UNIX wait() allows a parent to wait for a
child to terminate
UNIX execva() allows a child to run a
new program
19. 19 Creating a UNIX Process int pidValue;
...
pidValue = fork(); /* Creates a child process */
if(pidValue == 0) {
/* pidValue is 0 for child, nonzero for parent */
/* The child executes this code concurrently with parent */
childsPlay(…); /* A procedure linked into a.out */
exit(0);
}
/* The parent executes this code concurrently with child */
parentsWork(..);
wait(…);
...
20. 20 Child Executes Something else int pid;
...
/* Set up the argv array for the child */
...
/* Create the child */
if((pid = fork()) == 0) {
/* The child executes its own absolute program */
execve(childProgram.out, argv, 0);
/* Only return from an execve call if it fails */
printf(“Error in the exec … terminating the child …”);
exit(0);
}
...
wait(…); /* Parent waits for child to terminate */
...
21. 21 Example: Parent #include <sys/wait.h>
int main (void)
{
if ((c=fork()) == 0){ /* This is the child process */
execve("child",NULL,NULL);
exit(0); /* Should never get here, terminate */
}
/* Parent code here */
printf("Process[%d]: Parent in execution ...\n", getpid());
sleep(2);
if(wait(NULL) > 0) /* Child terminating */
printf("Process[%d]: Parent detects terminating child \n",c);
printf("Process[%d]: Parent terminating ...\n", getpid());
}
22. 22 Process State As a process executes, it changes state
new: The process is being created.
running: Instructions are being executed.
waiting: The process is waiting for some event to occur.
ready: The process is waiting to be assigned to a process.
terminated: The process has finished execution.
23. 23 Diagram of Process State
24. 24 Change of State - Reasons NULL-NEW: A new process is created due to any of the four reasons (New batch Job, Interactive logon, To provide service & Spawning)
NEW?READY: Operating system moves a process from new state to ready state, when it is prepared to accept an additional process. There could be limit on the number
READY ->RUNNING: Any process can be moved from ready to running state when ever it is scheduled.
RUNNING->TERM: The currently running process is terminated if it has signaled its completion or it is aborted.
25. 25 Change of State- Reasons (cont.) RUNNING->READY: Currently running proces has finished share of time for execution (Time out). Also, in some events a process may have to be admitted from running to ready if a high priority process has occurred.
RUNNING->BLOCKED: A process is moved to the blocked state, if it has requested some data for which it may have to wait. For example the process may have requested a resource such as data file from virtual memory, which is not ready at that time.
BLOCKED->READY: A process is moved to the ready state, if the event for which it is waiting has occurred.
26. 26 Change of State – Reasons (cont.) READY->EXIT: A parent process has generated a single or multiple children processes & they are in the ready state. Now during the execution of the process it may terminate any child process, therefore, it will directly go to exit state.
BLOCKED->EXIT: Similarly as above, during the execution of a parent process any child process waiting for an event to occur may directly go to exit if the parent itself terminates.
27. 27 SUSPENSION -Reasons
Swapping: The OS needs some space to bring in a ready process to execute.
Other OS origin: The o/s may suspend a process in the background or any process which may be causing some problem.
Interactive User Request: A user may wish to suspend a process for debugging or some data change or in fact may be a resource change is required.
Timing: A process may be periodically executing to monitor different processes or do some periodic task
Parent Process Request: A parent process may suspend any of its descendent process for some reason, may be for coordination.
28. 28 More States ?? BLOCKED-SUSPENDED :Process is in VM and waiting for an event
BLOCKED-READY : Process in VM and waiting for Physical Memory
BLOCKED -BLOCKED SUSPENDED: If there are no ready processes, then at least one blocked process is swapped out to make room for another process that is not blocked. This transition can be made even if there are ready processes available, if the OS determines that the currently running process or a ready process that it would like to dispatch requires more main memory to maintain adequate performance.
29. 29 More State Transitions (cont.) BLOCKED SUSPENDED – READY SUSPENDED: A process can be sent from BLOCKED SUSPENDED state to READY SUSPENDED state if the event it was waiting has occurred.
READY SUSPENDED – READY: A process in the READY SUSPENDED state may have a higher priority compared with any of the processes in the ready state
READY – READY SUSPENDED: Normally OS suspends a blocked process rather than a ready process, but in a situation when some space is required, a low priority ready process may be swapped with a blocked high priority process in anticipation that it will be ready soon.
30. 30 More State Transitions (cont) New – Ready, Suspended & New – Ready: When ever a new process is created it can either go to a ready queue or to a ready, suspended queue. In either case OS creates tables & allocates address space to these processes. It might be preferable to perform this housekeeping at an early time so that it maintain a large pool of processes that are not blocked. With this strategy, there would be insufficient room in the main memory for a new process; hence the use of New–Ready Suspended transition. On the other hand, one can argue that a just in time philosophy of creating processes as late as possible reduces OS overhead & allows that OS to perform the process creation duties at a time when the system is clogged with blocked processes anyway
Various – Exit: Normally a process terminates logically or otherwise while it is executing. However, in certain scenarios a process may be terminated while it is not in the execution. For example, when a parent process terminates, all of its children which may not be executing terminates with it.
31. 31 More Transitions (cont) BLOCKED SUSPENDED – BLOCKED: Inclusion of this transition may seem not very logical, because the process is not ready to execute & is not in the main memory. Consider the situation that a process terminates, resulting space in the main memory. There is a process in the blocked, suspended state with higher priority that any of the processes in the ready, suspended state & OS anticipates that the blocking event will occur soon. In this case, it may be reasonable to bring a blocked process into main memory instead of a ready process.
RUNNING –> READY SUSPENDED: Normally, a running process is moved to the ready state when its time runs out. However, if OS finds that a higher priority process on the BLOCKED SUSPENDED state has just become unblocked, it can move a running process to ready, suspended state to free some memory.
32. 32 Process Control Block (PCB) Information associated with each process.
Process state
Program counter
CPU registers
CPU scheduling information
Memory-management information
Accounting information
I/O status information
33. 33 Process Control Block (PCB)
34. 34 PCB Data Identifiers:
- Identifier of the process
- Identifier of the parent
- User identifier
Processor State Information
- User Visible Registers
- Control & Status Registers:
- Program Counter: Indicates the address of the next instruction - Condition Codes: Indicates certain operation status, e.g., arithmetic ov.
- Status Information: Indicates enabled/disabled flags of interrupts, execution mode etc.
- Stack Pointer: LIFO struct ptr for parameters, calling addresses of procedures & system calls.
35. 35 PCB Data (Cont.) Scheduling & State Information: Typically this information is used by the OS to schedule the processes:
- Process State: The process could be in any of the states like ready, blocked etc. Usually an integer
- Priority: An integer denoting the priority of a process
- Scheduling Related Information: This depends on the technique/scheme used such as real-time scheduling
- Event: Identification of the event for which the process is waiting.
36. 36 PCB data (cont.) Pointers to data structures: A process may be linked with some other process through a queue, linked list etc., or it may be a parent-child relationship
Inter Process Communication: Various flags, messages and depsoitiris such as
Memory Management: Pointers to pages or segments assigned in the virtual memory for a particular process.
Resource Ownership & Utilization: A track/history of the resources utilized by a process may be kept in the PCB for scheduling purpose, resources like open/close etc.,
37. 37 CPU Switch From Process to Process
38. 38 Process Creation Assign a unique process identifier (PID) number to the new process.
Add a new entry to the primary process table
Allocate memory space: This includes all the blocks mentioned in the process image. These values can be assigned by default on the type of process or that can be dynamically set at the time of creation of the process. In case any sharing of the space must be defined by providing an appropriate linkage set up.
Initializate PCB :
- PID, parent Id, User ID
- Program Entry point
- State (READY)
- No resource owned
- Prioirity as required by parent or default
39. 39 Context Switch - When Interrupt (External event )
IO completes
Clock Interrupt
Memory Fault (VM)
Trap (Internal)
If fatal -> Exit
Else depends on OS
Supervisory Call (SVC)
Do a system call – write to a file
40. 40 Context Switch- Steps Save the context of the processor, including PC & other registers into PCB of the running process.
Update the state of the process into PCB (READY, BLOCKED etc.)
Move the PCB of the current process to an appropriate queue, ready, blocked or suspended.
Select another process for execution (Schedule).
Set the state of the new PCB to RUNNING
Update memory management data structure (table).
Restore the context of the processor to these settings (PC, SP, regs. , memory, file pointers etc.) at which the current process was last switched out of the running state.
41. 41 Process Scheduling Queues Job queue – set of all processes in the system.
Ready queue – set of all processes residing in main memory, ready and waiting to execute.
Device queues – set of processes waiting for an I/O device.
Process migration between the various queues.
42. 42
43. 43 Representation of Process Scheduling
44. 44 Schedulers Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue.
Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU.
45. 45 Addition of Medium Term Scheduling
46. 46 Schedulers (Cont.) Short-term scheduler is invoked very frequently (milliseconds) ? (must be fast).
Long-term scheduler is invoked very infrequently (seconds, minutes) ? (may be slow).
The long-term scheduler controls the degree of multiprogramming.
Processes can be described as either:
I/O-bound process – spends more time doing I/O than computations, many short CPU bursts.
CPU-bound process – spends more time doing computations; few very long CPU bursts.
47. 47 Context Switch When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process.
Context-switch time is overhead; the system does no useful work while switching.
Time dependent on hardware support.
48. 48 Process Creation Parent process create children processes, which, in turn create other processes, forming a tree of processes.
Resource sharing
Parent and children share all resources.
Children share subset of parent’s resources.
Parent and child share no resources.
Execution
Parent and children execute concurrently.
Parent waits until children terminate
49. 49 Processes Tree on a UNIX System
50. 50 Process Termination Process executes last statement and asks the operating system to decide it (exit).
Output data from child to parent (via wait).
Process’ resources are deallocated by operating system.
Parent may terminate execution of children processes (abort).
Child has exceeded allocated resources.
Task assigned to child is no longer required.
Parent is exiting.
Operating system does not allow child to continue if its parent terminates.
Cascading termination.
51. 51 Cooperating Processes Independent process cannot affect or be affected by the execution of another process.
Cooperating process can affect or be affected by the execution of another process
Advantages of process cooperation
Information sharing
Computation speed-up
Modularity
Convenience
52. 52 Producer-Consumer Problem Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process.
unbounded-buffer places no practical limit on the size of the buffer.
bounded-buffer assumes that there is a fixed buffer size.
53. 53 Bounded-Buffer – Shared-Memory Solution Shared data
#define BUFFER_SIZE 10
Typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
Solution is correct, but can only use BUFFER_SIZE-1 elements
54. 54 Bounded-Buffer – Producer Process
item nextProduced;
while (1) {
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
}
55. 55 Bounded-Buffer – Consumer Process
item nextConsumed;
while (1) {
while (in == out)
; /* do nothing */
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
}
56. 56 Interprocess Communication (IPC) Mechanism for processes to communicate and to synchronize their actions.
Message system – processes communicate with each other without resorting to shared variables.
IPC facility provides two operations:
send(message) – message size fixed or variable
receive(message)
If P and Q wish to communicate, they need to:
establish a communication link between them
exchange messages via send/receive
Implementation of communication link
physical (e.g., shared memory, hardware bus)
logical (e.g., logical properties)
57. 57 Direct Communication Processes must name each other explicitly:
send (P, message) – send a message to process P
receive(Q, message) – receive a message from process Q
Properties of communication link
Links are established automatically.
A link is associated with exactly one pair of communicating processes.
Between each pair there exists exactly one link.
The link may be unidirectional, but is usually bi-directional.
58. 58 Indirect Communication Messages are directed and received from mailboxes (also referred to as ports).
Each mailbox has a unique id.
Processes can communicate only if they share a mailbox.
Properties of communication link
Link established only if processes share a common mailbox
A link may be associated with many processes.
Each pair of processes may share several communication links.
Link may be unidirectional or bi-directional.
59. 59 Indirect Communication Operations
create a new mailbox
send and receive messages through mailbox
destroy a mailbox
Primitives are defined as:
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A
60. 60 Indirect Communication Mailbox sharing
P1, P2, and P3 share mailbox A.
P1, sends; P2 and P3 receive.
Who gets the message?
Solutions
Allow a link to be associated with at most two processes.
Allow only one process at a time to execute a receive operation.
Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was.
61. 61 Synchronization Message passing may be either blocking or non-blocking.
Blocking is considered synchronous
Non-blocking is considered asynchronous
send and receive primitives may be either blocking or non-blocking.
62. 62 Buffering Queue of messages attached to the link; implemented in one of three ways.
1. Zero capacity – 0 messagesSender must wait for receiver (rendezvous).
2. Bounded capacity – finite length of n messagesSender must wait if link full.
3. Unbounded capacity – infinite length Sender never waits.
63. 63 Client-Server Communication Sockets
Remote Procedure Calls
Remote Method Invocation (Java)
64. 64 Sockets A socket is defined as an endpoint for communication.
Concatenation of IP address and port
The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8
Communication consists between a pair of sockets.
65. 65 Socket Communication
66. 66 Remote Procedure Calls Remote procedure call (RPC) abstracts procedure calls between processes on networked systems.
Stubs – client-side proxy for the actual procedure on the server.
The client-side stub locates the server and marshalls the parameters.
The server-side stub receives this message, unpacks the marshalled parameters, and peforms the procedure on the server.
67. 67 Execution of RPC
68. 68 Remote Method Invocation Remote Method Invocation (RMI) is a Java mechanism similar to RPCs.
RMI allows a Java program on one machine to invoke a method on a remote object.
69. 69 Marshalling Parameters