7-Process Concept-11-05-2023
7-Process Concept-11-05-2023
Processes
Outline
•Process Concept
•Process Scheduling
•Operations on Processes
•Interprocess Communication
•IPC in Shared-Memory Systems
•IPC in Message-Passing Systems
•Examples of IPC Systems
Process Concept
• Process creation
• Process termination
Process Creation
• Parent process create children processes, which, in turn
create other processes, forming a tree of processes
• Generally, process identified and managed via a process
identifier (pid)
• Resource sharing options
• Parent and children share all resources
• Children share subset of parent’s resources
• Parent and child share no resources
• 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
A Tree of Processes in Linux
Process Termination
• Process executes last statement and then asks the
operating system to delete it using the exit() system
call.
• Returns status data from child to parent (via wait())
• Process’ resources are deallocated by operating system
• Parent may terminate the execution of children processes
using the abort() system call. Some reasons for doing
so:
• Child has exceeded allocated resources
• Task assigned to child is no longer required
• The parent is exiting, and the operating systems does
not allow a child to continue if its parent terminates
Process Termination
register1 = counter
register1 = register1 + 1
counter = register1
• counter-- could be implemented as
register2 = counter
register2 = register2 - 1
counter = register2
• Shared data
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
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 */
}
IPC – Message Passing
•Physical:
•Shared memory
•Hardware bus
•Network
•Logical:
• Direct or indirect
• Synchronous or asynchronous
• Automatic or explicit buffering
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
Indirect Communication
•Operations
•Create a new mailbox (port)
•Send and receive messages through mailbox
•Delete a mailbox
•Primitives are defined as:
•send(A, message) – send a message to
mailbox A
•receive(A, message) – receive a message
from mailbox A
Indirect Communication (Cont.)
•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.
Synchronization
Message passing may be either blocking or non-blocking
send(next_produced);
}
• Consumer
message next_consumed;
while (true) {
receive(next_consumed)
#include<mach/mach.h>
struct message {
mach_msg_header_t header;
int data;
};