PROCESSES Process Creation n Parent process creates children













- Slides: 13

PROCESSES

Process Creation n Parent process creates children processes, which, in turn create other processes, forming a tree of processes n Execution l Parent and children execute concurrently l Parent waits until children terminate n Resource sharing can take the form (depending on the OS): l Parent and children share all resources l Children share subset of parent’s resources l Parent and children share no resources 2

Process Creation: Unix Example n Process creates another process (child) by using fork system call l Child is a copy of the parent l Typically, child loads another program into its address space using exec system l Parent waits for its children to terminate 3

C Program Forking Separate Process int main() { pid_t pid; pid = fork(); /* fork another process */ if (pid < 0) { /* error occurred */ fprintf (stderr, "Fork Failed"); exit(-1); } else if (pid == 0) { /* child process */ execlp ("/bin/ls", "ls", NULL); } else { /* parent process */ /* parent will wait for child to complete */ wait (NULL); printf ("Child Complete"); exit(0); Note: fork returns 0 to child and the pid of the } } new process to parent. 4

A tree of processes on a typical Solaris Parent of all user Process For memory and file Mgt For networking services Represent the user login screen 5

Process Termination n Process executes last statement and asks the operating system to delete it (exit) l Output data from child to parent (via wait) l Process’ resources are deallocated by operating system n Parent may terminate execution of children processes (abort) l Child has exceeded allocated resources l Task assigned to child is no longer required l If parent is exiting 4 Some operating system do not allow child to continue if its parent terminates – All children terminated - cascading termination 6

Cooperating Processes n Cooperating process can affect or be affected by the execution of another process n Why processes cooperate? l Information sharing l Computation speed-up l Modularity, Convenience n Interprocess Communications (IPC) methods l Shared memory l Message passing 7

Interprocess Communications Models Message Passing Shared Memory 8

IPC: Shared Memory n Processes communicate by creating a shared place in memory l One process creates a shared memory—shmget() l Other processes attach shared memory to their own address space—shmat() l Then, shared memory is treated as regular memory l Synchronization is needed to prevent concurrent access to shared memory (conflicts) n Pros l Fast (memory speed) l Convenient to programmers (just regular memory) n Cons l Need to manage conflicts (tricky for distributed systems) 9

IPC: Message Passing n If processes P and Q wish to communicate, they need to: establish a communication channel between them l exchange messages via: 4 send (message) – message size fixed or variable 4 receive (message) n Pros l No conflict easy to exchange messages especially in distributed systems n Cons l Overhead (message headers) l l Slow 4 prepare messages 4 Kernel involvement: sender kernel receiver (several system calls) 10

IPC: Message Passing (cont’d) n Communication channel can be l Direct: Processes must name each other explicitly: 4 send (P, message) – send a message to process P 4 receive l (Q, message) – receive a message from process Q Indirect: Processes communicate via mailboxes (or ports) 4 Messages 4 Each are sent to and received from mailboxes mailbox has a unique id – Send (A, message) – send a message to mailbox A – Receive (A, message) – receive a message from mailbox A 11

IPC: Message Passing (cont’d) n Synchronization: message passing is either l l n Blocking (or synchronous) 4 send () has the sender block until the message is received 4 receive () has the receiver block until a message is available Non-blocking (or asynchronous) 4 send () has the sender send the message and continue 4 receive () has the receiver receive a valid message or null Buffering: Queue of messages attached to the comm. channel l Zero capacity – Sender must wait for receiver l Bounded capacity – Sender must wait if link full l Unbounded capacity – Sender never waits 12

Summary n Process is a program in execution l OS maintains process info in PCB l Process State diagram l Creating and terminating processes (fork) n Process scheduling l Long-, short-, and medium-term schedulers l Scheduling queues n Interprocess communication l Shared memory l Message passing 13