CH 3
CH 3
Process Concept
• Execution options
• Parent and children execute concurrently
• Parent waits until children terminate
Process Creation (Cont.)
• Address space
• Child duplicate of parent
• Child has a program loaded into it
• UNIX examples
• fork() system call creates new process
• exec() system call used after a fork() to replace the process’ memory
space with a new program
• Parent process calls wait()waiting for the child to terminate
C Program Forking Separate Process
Process Termination
• Two variations:
• unbounded-buffer places no practical limit on the size of
the buffer:
• Producer never waits
• Consumer waits if there is no buffer to consume
• bounded-buffer assumes that there is a fixed buffer size
• Producer must wait if all buffers are full
• Consumer waits if there is no buffer to consume
IPC – Shared Memory
• Shared data
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
item next_produced;
while (true) {
/* produce an item in next produced */
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
}
Consumer Process – Shared Memory
item next_consumed;
while (true) {
while (in == out)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
while (true) {
/* produce an item in next produced */
while (true) {
while (counter == 0)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next consumed */
}
Race Condition
register1 = counter
register1 = register1 + 1
counter = register1
• Physical:
• Shared memory
• Hardware bus
• Network
• Logical:
• Direct or indirect
• Synchronous or asynchronous
• Automatic or explicit buffering
Direct Communication
• Operations
• Create a new mailbox (port)
• Send and receive messages through mailbox
• Delete a mailbox
• Mailbox sharing
• P1, P2, and P3 share mailbox A
• 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.
Synchronization
Message passing may be either blocking or non-blocking
• Producer
message next_produced;
while (true) {
/* produce an item in next_produced */
send(next_produced);
}
• Consumer
message next_consumed;
while (true) {
receive(next_consumed)