1 / 21

Operating Systems

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

ashby
Download Presentation

Operating Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operating Systems

  2. 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

  3. 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

  4. CPU Switch From Process to Process Operating System Concepts

  5. 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

  6. 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

  7. Addition of Medium Term Scheduling Operating System Concepts

  8. 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

  9. 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

  10. 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

  11. Processes Tree on a UNIX System Daemon: System processes running in background. Operating System Concepts

  12. 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

  13. 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

  14. 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.

  15. 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

  16. 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

  17. 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.

  18. 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

  19. 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

  20. 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

  21. Unix Commands related to Processes Operating System Concepts

More Related