Process Fundamentals Notice: This set of slides is based on the notes by Professor Perrone of Bucknell and the textbook authors Silberschatz, Galvin, and Gagne CSCI 315 Operating Systems Design 1
What Is A Process? A process is a program in execution. #include <stdio.h> int main(int argc, char*argv[]) { int v; printf( hello world\n ); scanf( %d, %d,&v); return 0; } Program test process is running %gcc o test process test process.c %./test process & hello world %ps ef grep test process xmeng 22807 4288 0 10:08 pts/0 00:00:00./test process
Process Concept A process includes: program counter stack data section Process execution must progress in sequential fashion. Program Counter (PC) designates the next instruction to be executed program counter heap stack data code 9/6/2013 CSCI 315 Operating Systems Design 3
Process States 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 processor. terminated: The process has finished execution. 9/6/2013 CSCI 315 Operating Systems Design 4
Process State Transition Diagram created new admitted interrupt exit terminated ready running scheduler dispatch I/O or event completion I/O or event wait waiting 9/6/2013 CSCI 315 Operating Systems Design 5
Process Control Block (PCB) OS bookkeeping information associated with each process: Process state Program counter CPU registers CPU scheduling information Memory management information Accounting information I/O status information process id process state program counter registers memory limits list of open files 9/6/2013 CSCI 315 Operating Systems Design 6
A Sample Linux PCB http://www.eecs.harvard.edu/ harvard edu/~margo/cs161/ videos/sched.h.html http://www.tldp.org/ldp/tlk/ds/ds.htmltldp html Look for struct task_struct in the source code distribution ib i of Linux.
CPU Switching 9/6/2013 CSCI 315 Operating Systems Design 8
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. ec Device queues set of processes waiting for an I/O device. Processes migrate between the various queues. 9/6/2013 CSCI 315 Operating Systems Design 9
Processes and OS Queues 9/6/2013 CSCI 315 Operating Systems Design 10
Process Scheduling ready queue CPU I/O I/O queue I/O request time slice expired child executes fork a child interrupt occurs wait for interrupt 9/6/2013 CSCI 315 Operating Systems Design 11
Schedulers Long term scheduler (or job scheduler) selects which processes should be brought into the ready queue Short term term scheduler (or CPU scheduler) selects which process should be executed next and allocates CPU 9/6/2013 CSCI 315 Operating Systems Design 12
Schedulers Short term scheduler is invoked very frequently (milliseconds) (must be fast) Long term scheduler is invoked very infrequently (seconds, minutes) (may be slow; controls the degree of multiprogramming) g) Processes can be described as either: I/O bound b dprocess spends more time doing I/O than computations, tti many short CPU bursts CPU bound process spends more time doing computations; few very long CPU bursts 9/6/2013 CSCI 315 Operating Systems Design 13
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. What is in context? (Information in PCBs) Context switch time is overhead; the system does no useful work while switching. Time dependent on hardware support. 9/6/2013 CSCI 315 Operating Systems Design 14
Operations on Processes Creation Termination
Process Creation Parent process create children processes, which, in turn can 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 may wait until children terminate. 9/6/2013 CSCI 315 Operating Systems Design 16
Process Creation (Cont.) Address space: Child has duplicate of parent s address space, or Child can have a program loaded onto it. UNIX examples: fork system call creates new process and returns with a pid (0 in child, > 0 in the parent), exec system call can be used after a forkto replace the process memory space with a new program. 9/6/2013 CSCI 315 Operating Systems Design 17
Process Tree (Linux Example) See example by running ps aef less
Create a Process in Linux #include <stdio.h> #include <unistd.h> int main(int argc, char *argv[]) { pid_t pid; if ((pid = fork()) < 0) { // error occured fprintf(stderr, "Fork failed\n"); return 1; } else if (pid == 0) { // child process execlp("/bin/ls", "ls", NULL); } else { // parent process wait(null); printf("child completed\n"); } return 0; // or exit(0) } [xmeng@polariscode]$ d]$ gcc o create process fork.c [xmeng@polaris code]$./create process create process fork.c fork.c~ fork.o test process.c Child completed ltd [xmeng@polaris code]$ Screen recording http://www.eg.bucknell.edu/~cs315/fall13/code/process/fork.c
Process Termination Process executes last statement and asks the operating system to terminate t 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) if: Child has exceeded ddallocated resources, Task assigned to child is no longer required, If parent is exiting (some operating system do not allow child to continue if its parent terminates) All children terminated cascading termination 9/6/2013 CSCI 315 Operating Systems Design 20
Interprocess Communications (IPC) Shared memory Message passing Pipe Socket
Cooperating Processes An independent process cannot affect or be affected by the execution of another process. A cooperating process can affect or be affected by the execution of another process. Cooperating processes have to communicate with each other! (Don t we?) Inter process communication (IPC) Shared memory: processes share a common area of memory for information Message passing: processes send messages to each other through message delivery system 9/6/2013 CSCI 315 Operating Systems Design 22
Producer Consumer Problem (h (shared memory) A paradigm for cooperating gprocesses in which a producer process produces information that is consumed by a consumer process: unbounded bufferbuffer placesnopractical practical limit onthesize ofthebuffer buffer, bounded buffer assumes that there is a fixed buffer size. producer resource buffer consumer 9/6/2013 CSCI 315 Operating Systems Design 23
Bounded Buffer (shared memory solution) // define product structure struct product_t t { char name[32]; double value; }; #include buffer.h #define BFSIZE 5 int main(int argc, char* argv[]) { int count; // number of items in the buffer int in; // index to the next free position int out; // index to the next full position struct product_t t * buffer; // function prototype for producers void insert(struct product_t * item); // function prototype for consumers struct product_t * remove(); } // buffer is initially empty count = 0; in = 0; out = 0; buffer = (struct product_t *)malloc(bfsize* sizeof(struct product_t)); while (true) { producer(); // these two would run in parallel consumer(); } buffer.h buffer.c 9/6/2013 CSCI 315 Operating Systems Design 24
void producer() { struct product_t * item; while (true) { item = makeitem(); insert(item); } } Bounded Buffer (shared memory solution for producer) public void insert(struct product_t * item) { while (count == BFSIZE); // do nothing -- no free buffers // now we have a free slot, add an item to the buffer ++count; buffer[in] = item; in = (in + 1) % BFSIZE; } 9/6/2013 CSCI 315 Operating Systems Design 25
struct product_t * consumer() { struct product_t * item; while (true) { item = remove(); } } Bounded Buffer (shared memory solution for consumer) struct product_t * remove() { struct product_tt * item; while (count == 0); // do nothing -- nothing to consume // remove an item from the buffer --count; item = buffer[out]; out = (out + 1) % BUFFER SIZE; return item; } 9/6/2013 CSCI 315 Operating Systems Design 26
Synchronization Between Consumer and Producer In the above solution, we used a busy waiting strategy while (count == 0); // for consumer while (count == BFSIZE); // for producer Issues? (we will tackle them at a later time) What if both the consumer and the producer updates the count at the exact same moment? Busy waiting is less efficient in using resources (CPU time), better mechanism has to be developed.
Posix Shared Memory Solution See the code example at http://www.eg.bucknell.edu/~cs315/fall13/code/ process/shm posix producer posix producer.c c http://www.eg.bucknell.edu/~cs315/fall13/code/ process/shm posix consumer.c posix consumer.c
Message Passing Processes must use communications facility provided by the OS, e.g., socket(), or pipe() See the code example for Unix pipe at http://www.eg.bucknell.edu/fall13/code/process/ pipe.c
Synchronization Message passing may be either blocking or non blocking. Blocking is considered synchronous: Blocking send has the sender block until the message is received. Blockingreceive has the receiver block until a message is available. Non blocking is considered asynchronous Non blocking send has the sender send the message and continue. Non blocking receive has the receiver receive a valid message or null. 9/6/2013 CSCI 315 Operating Systems Design 30
Buffering Queue of messages attached to the link; implemented in one of three ways: 1.Zero capacity 0 messages Sender must wait for receiver (rendezvous). 2.Bounded capacity finite length of n messages. Sender must wait if link full. 3.Unbounded capacity infinite length. Sender never waits. 9/6/2013 CSCI 315 Operating Systems Design 31