Download Processes: Concept, Scheduling, Interprocess Communication and Cooperation and more Slides Computer Science in PDF only on Docsity!
Processes
Chapter 3
Processes
• Process Concept
• Process Scheduling
• Operations on Processes
• Cooperating Processes
• Interprocess Communication
• Communication in Client-Server Systems
Process in Memory
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 processor
- terminated : The process has finished execution
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
- Processes migrate among the various queues
Representation of Process Scheduling
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
CPU Switch From Process to Process
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
- Address space
- Child duplicate of parent
- Child has a program loaded into it
Process Creation (Cont.)
- 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
C Program Forking Separate Process
int main() { pid_t pid; / fork another process / pid = fork(); 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 the child to complete / wait (NULL); printf ("Child Complete"); exit(0); } }
Process synchronization
- The parent can wait for a child to terminate using the wait( ) function. int cpid_done, status; cpid_done = wait(&status);
- The caller sleeps until any child terminates.
- cpid_done gets the pid of the child that terminated.
- status gets the exit status of the terminated process.
int cpid_done, status, options = 0; cpid_done = waitpid(cpid, &status, options);
- caller sleeps until child with cpid terminates.
- returns the pid of the terminating child
- stores the status of the terminating child in status.
Basic Shell code
Pseudo-code a basic shell (not real C++ code):
while (1) { parse_command_line; (get command, args, redirect, etc.) if(cmd == exit) exit; p = fork( ); if (p == 0) { exec (cmd, args) } else { if (command doesn't end with &) wait( ); } }
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