210 likes | 386 Views
Operating Systems. Agenda of Today. Review PCB Contents of PCB Context Switching Schedulers Types of Schedulers Operations on Processes Creation of a process Termination of a process. Process Control Block (PCB). Information associated with each process. Process state Program counter
E N D
Agenda of Today • Review • PCB • Contents of PCB • Context Switching • Schedulers • Types of Schedulers • Operations on Processes • Creation of a process • Termination of a process Operating System Concepts
Process Control Block (PCB) Information associated with each process. • Process state • Program counter • CPU registers • CPU scheduling information • Memory-management information • Accounting information • I/O status information Operating System Concepts
CPU Switch From Process to Process Operating System Concepts
Context Switch • Steps involved in a full Process switch are: • Save context of currently running process (including PC and other registers) • Move this PCB to an appropriate Queue • Select another process for execution (Kernel Schedules) • Update PCB of selected process • Update memory management data structures • Restore the context of the process CMP320 PUCIT Arif Butt
Schedulers • “Scheduling is a matter of managing queues to minimize queuing delay and to optimize performance in a queuing environment”. • Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue. • Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU. Operating System Concepts
Addition of Medium Term Scheduling Operating System Concepts
Schedulers (Cont.) • Long-term scheduler (or job scheduler) – selects processes from the job pool (processes spooled on hard disk) to be brought into the ready queue (inside main memory) • Long-term scheduleris invoked very infrequently (seconds, minutes) (may be slow). • The long-term scheduler controls the degree of multiprogramming. • Must select a good mix of I/O bound and CPU bound processes. • Short-term scheduler is invoked very frequently (milliseconds) (must be fast). Select processes from ready queue and execute them. • Medium-term scheduler handles swapping. • Swap-out: When cpu is idle: all processes are blocked and another new job is waiting while memory is full. Then CPU suspend a process from blocked queue and swap it out to the disk. Then swap-in another process. This adds another state in the state diagram. Operating System Concepts
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. Operating System Concepts
Process Creation (Cont.) • Address space • Child duplicate of parent. • Child has a program loaded into it. • Child same as parent: Exp. HTTP server processing clients through multiple clones • 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. • Important process-related UNIX/Linux system calls • fork() • exit() • wait() • exec() Operating System Concepts
Processes Tree on a UNIX System Daemon: System processes running in background. Operating System Concepts
Process Termination • Process executes last statement and asks the operating system to decide it (exit). • Output data from child to parent • Process’ resources are de-allocated by operating system. • Exit (0): normal or (1) for less space etc. • Parent may terminate execution of children processes (abort). • Child has exceeded allocated resources. • Task assigned to child is no longer required. • Parent is exiting. • Operating system does not allow child to continue if its parent terminates. • Cascading termination. Operating System Concepts
Process Termination • A process may terminate due to following reasons: • Normal completion • Memory unavailable • Protection error • Mathematical error • I/O failure • Cascading termination (by OS) • Operator intervention Operating System Concepts
System Call - fork() • When the fork system call is executed, a new process is created which consists of a copy of the address space of the parent • An exact copy of the parent program is created • This mechanism allows the parent process to communicate easily with the child process.
System Call - fork() ... • On success: (Child process is created) • The return code for fork is zero for the child process • The child process ID is returned to the parent process • Both processes continue execution at the instruction after the fork call • On failure: (No child process is created) • A -1 is returned to the parent process • Variable errnois set appropriately to indicate the reason of failure
Using fork() & exit() system call PID: 597 • Parent forks Parent 1. //fork1.c 2. int main() 3. { 4. int i = 54, cpid = -1; 5. cpid = fork(); 6. if (cpid == -1) 7. { 8. printf (“\nFork failed\n”); 9. exit (1); 10. } 11. if (cpid == 0) //child code 12. printf (“\n Hello I am child \n”); 13. else //parent code 14. printf (“\n Hello I am parent \n”); 15. } DATA i = 54 cpid = -1
Using fork() & exit() system call PID: 632 PID: 597 Parent Child 1. //fork1.c 2. int main() 3. { 4. int i = 54, cpid = -1; 5. cpid = fork(); 6. if (cpid == -1) 7. { 8. printf (“\nFork failed\n”); 9. exit (1); 10. } 11. if (cpid == 0) //child code 12. printf (“\n Hello I am child \n”); 13. else //parent code 14. printf (“\n Hello I am parent \n”); 15. } 1. //fork1.c 2. int main() 3. { 4. int i = 54, cpid = -1; 5. cpid = fork(); 6. if (cpid == -1) 7. { 8. printf (“\nFork failed\n”); 9. exit (1); 10. } 11. if (cpid == 0) //child code 12. printf (“\n Hello I am child \n”); 13. else //parent code 14. printf (“\n Hello I am parent \n”); 15. } DATA i = 54 cpid = 0 DATA i = 54 cpid = 632 • After fork parent and child are identical except for the return value of fork (and of course the PIDs). • Because data are different therefore program execution differs. • When both will execute line 11, parent will now execute line 12 while child will execute line 14.
fork() – Child inherits from the Parent • The child process inherits the following attributes form the parent: • Environment • Open File Descriptor table • Signal handling settings • Current working directory • Root directory • File mode creation mask (umask) CMP320 PUCIT Arif Butt
fork() – Child Differs from the Parent • The child process differs from the parent process: • Different process ID (PID). • Different parent process ID (PPID). CMP320 PUCIT Arif Butt
fork() – Reasons for Failure • Maximum number of processes allowed to execute under one user has exceeded • Maximum number of processes allowed on the system has exceeded • Not enough swap space CMP320 PUCIT Arif Butt
Unix Commands related to Processes Operating System Concepts