Chapter 3: Processes PDF
Document Details
Uploaded by GlowingBeryllium
Silberschatz, Galvin and Gagne
Tags
Related
- OPERATING SYSTEMS SLM.pdf
- 6-Process Structures - Process creation, management in Unix-25-07-2024.pdf
- RCS 205 Operating Systems Lecture 5: Introduction to Processes PDF
- Introduction to Operating Systems PDF
- Operating Systems Chapter 3 Process Description and Control PDF
- COMP 007 Process Management PDF
Summary
This document details the process concept and its parts, including the program code, current activity, and stack, data section, and heap. It also discusses process scheduling, operations on processes, and interprocess communication, along with communication in the client-server systems.
Full Transcript
Chapter 3: Processes Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Interp...
Chapter 3: Processes Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013 Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Communication in Client-Server Systems Operating System Concepts – 9th Edition 3.2 Silberschatz, Galvin and Gagne ©2013 Process Concept An operating system executes a variety of programs that run as a process Jobs, user programs, tasks… Textbook uses the terms job and process almost interchangeably Process is a program in execution Initiated by GUI or CLI or other programs Process execution must progress in sequential fashion Traditionally a process contained only a single thread of control as it ran Most modern operating systems now support processes that have multiple threads OS is responsible for several important aspects of process and thread management: the creation and deletion of processes; the scheduling of processes; and the provision of mechanisms for synchronization, communication, and deadlock handling for processes. Operating System Concepts – 9th Edition 3.3 Silberschatz, Galvin and Gagne ©2013 Process Concept (Cont.) Program is a passive entity stored on disk (executable file), process is active A file containing a list of instructions stored on disk Program becomes process when executable file loaded into memory Execution of program started via GUI mouse clicks, command line entry of its name, … double-clicking an icon representing the executable file entering the name of the executable file on the command line (as in prog.exe or a.out). One program can be several processes Consider multiple users executing the same program Or the same user may invoke many copies of the web browser program Operating System Concepts – 9th Edition 3.4 Silberschatz, Galvin and Gagne ©2013 Process Concept Process is a program in execution Multiple parts the program code, also called text section the current activity including program counter (specifying the next instruction to execute) and the content of the processor’s registers Stack containing temporary data 4 Function parameters, return addresses, local variables Data section containing global variables Heap which is memory that is dynamically allocated during process run time Operating System Concepts – 9th Edition 3.5 Silberschatz, Galvin and Gagne ©2013 Process Control Block (PCB) Each process is represented in the operating system by a process control (also called task control block) providing information associated with a specific process including: Process state – running, waiting, etc Program counter – location of instruction to next execute CPU registers – contents of all process-centric registers CPU scheduling information- priorities, scheduling queue pointers Memory-management information – memory allocated to the process Accounting information – CPU used, clock time elapsed since start, time limits I/O status information – I/O devices allocated to process, list of open files Operating System Concepts – 9th Edition 3.6 Silberschatz, Galvin and Gagne ©2013 Process State As a process executes, it changes state New: The process is being created and waiting to be admitted Ready: The process is in the main memory and is waiting to be assigned to a processor Running: Instructions are being executed Waiting: The process is waiting for some event to occur Terminated: The process has finished execution Operating System Concepts – 9th Edition 3.7 Silberschatz, Galvin and Gagne ©2013 Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Communication in Client-Server Systems Operating System Concepts – 9th Edition 3.8 Silberschatz, Galvin and Gagne ©2013 Process Scheduling Maximize CPU utilization, quickly switch processes onto CPU for time sharing Process scheduler selects among available processes for next execution on CPU For a single-processor system, there will never be more than one running process. Maintains scheduling queues of processes 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 Operating System Concepts – 9th Edition 3.9 Silberschatz, Galvin and Gagne ©2013 Ready Queue And Various I/O Device Queues Each device has its own device queue Each PCB includes a pointer field that points to the next PCB in the ready queue Operating System Concepts – 9th Edition 3.10 Silberschatz, Galvin and Gagne ©2013 Representation of Process Scheduling Queueing diagram represents: Queues, ready queue and a set of device queues (rectangles), Resources (circles) and flows (arrows) Operating System Concepts – 9th Edition 3.11 Silberschatz, Galvin and Gagne ©2013 Schedulers Long-term scheduler (or job scheduler) Short-term scheduler (or CPU scheduler) Medium-term scheduler Operating System Concepts – 9th Edition 3.12 Silberschatz, Galvin and Gagne ©2013 Schedulers Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue and loads them into memory for execution Long-term scheduler is invoked infrequently (seconds, minutes) Þ (may be slow) The long-term scheduler controls the degree of multiprogramming (the number of processes in memory) Job Queue Ready Queue Operating System Concepts – 9th Edition 3.13 Silberschatz, Galvin and Gagne ©2013 Schedulers 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 Long-term scheduler strives for good process mix of I/O-bound and CPU-bound processes Operating System Concepts – 9th Edition 3.14 Silberschatz, Galvin and Gagne ©2013 Schedulers Short-term scheduler (or CPU scheduler) – selects from among the processes that are ready to execute and allocates the CPU to one of them Sometimes the only scheduler in a system Short-term scheduler is invoked frequently (milliseconds) Þ (must be fast) Ready Queue CPU Operating System Concepts – 9th Edition 3.15 Silberschatz, Galvin and Gagne ©2013 Addition of Medium-Term Scheduling Medium-term scheduler can be added if degree of multiple programming needs to decrease Remove process from memory, store on disk, bring back in from disk to continue execution: swapping remove the process from memory and make space for other processes Improve the process mix Operating System Concepts – 9th Edition 3.16 Silberschatz, Galvin and Gagne ©2013 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 via a context switch Context of a process represented in the PCB Context-switch time is overhead; the system does no useful work while switching Operating System Concepts – 9th Edition 3.17 Silberschatz, Galvin and Gagne ©2013 CPU Switch From Process to Process Operating System Concepts – 9th Edition 3.18 Silberschatz, Galvin and Gagne ©2013 Multitasking in Mobile Systems Some mobile systems (e.g., early version of iOS) allow only one process to run, others suspended On an iOS mobile device: Single foreground process- controlled via user interface, currently open and appearing on the display screen Multiple background processes– in memory, running, but not on the display screen, and with limits 4 Limits include single, short task, receiving notification of events, specific long-running tasks like audio playback Android runs foreground and background, with fewer limits Background process must use a service to perform tasks Service is a separate application component that runs on behalf of the background process Service has no user interface, small memory use Operating System Concepts – 9th Edition 3.19 Silberschatz, Galvin and Gagne ©2013 Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Communication in Client-Server Systems Operating System Concepts – 9th Edition 3.20 Silberschatz, Galvin and Gagne ©2013 Operations on Processes System must provide mechanisms for: process creation, process termination, and so on…. Operating System Concepts – 9th Edition 3.21 Silberschatz, Galvin and Gagne ©2013 Process Creation A process may create several new processes Parent process creates children processes, which, in turn create other processes, forming a tree of processes Generally, process identified and managed via a process identifier (pid) Resource (CPU time, memory, files, I/O devices) sharing options: Parent and children share all resources Children share subset of parent’s resources Parent and child share no resources Execution options Parent and children execute concurrently Parent waits until children terminate Operating System Concepts – 9th Edition 3.22 Silberschatz, Galvin and Gagne ©2013 A Tree of Processes in Linux init pid = 1 login kthreadd sshd pid = 8415 pid = 2 pid = 3028 bash khelper pdflush sshd pid = 8416 pid = 6 pid = 200 pid = 3610 emacs tcsch ps pid = 9204 pid = 4005 pid = 9298 Operating System Concepts – 9th Edition 3.23 Silberschatz, Galvin and Gagne ©2013 A Tree of Processes in Linux The init process always has a pid of 1 serves as the root parent process for all user processes Once the system has booted, the init process can also create various user processes init pid = 1 login kthreadd sshd pid = 8415 pid = 2 pid = 3028 bash khelper pdflush sshd pid = 8416 pid = 6 pid = 200 pid = 3610 emacs tcsch ps pid = 9204 pid = 4005 pid = 9298 Operating System Concepts – 9th Edition 3.24 Silberschatz, Galvin and Gagne ©2013 A Tree of Processes in Linux kthreadd responsible for creating additional processes that perform tasks on behalf of the kernel sshd responsible for managing clients that connect to the system by using ssh (which is short for secure shell). init pid = 1 login kthreadd sshd pid = 8415 pid = 2 pid = 3028 bash khelper pdflush sshd pid = 8416 pid = 6 pid = 200 pid = 3610 emacs tcsch ps pid = 9204 pid = 4005 pid = 9298 Operating System Concepts – 9th Edition 3.25 Silberschatz, Galvin and Gagne ©2013 A Tree of Processes in Linux Login responsible for managing clients that directly log onto the system. Example: a client has logged on and is using the bash shell (pid 8416) Using the bash command-line interface, this user has created the process ps as well as the emacs editor. init pid = 1 login kthreadd sshd pid = 8415 pid = 2 pid = 3028 bash khelper pdflush sshd pid = 8416 pid = 6 pid = 200 pid = 3610 emacs tcsch ps pid = 9204 pid = 4005 pid = 9298 Operating System Concepts – 9th Edition 3.26 Silberschatz, Galvin and Gagne ©2013 Let’s check!! “ps” : Shows the processes for the current shell Operating System Concepts – 9th Edition 3.27 Silberschatz, Galvin and Gagne ©2013 Let’s check!! ps -el Operating System Concepts – 9th Edition 3.28 Silberschatz, Galvin and Gagne ©2013 Let’s code!! #include #include #include int main(){ int mypid=getpid(); printf("My PID is %d\n",mypid); printf("My Parent's PID is %d\n",getppid()); return 0; } Operating System Concepts – 9th Edition 3.29 Silberschatz, Galvin and Gagne ©2013 Process Creation (Cont.) Address space Child duplicate of the parent process Child has a new program loaded into it UNIX examples fork() system call creates a new process exec() system call used after a fork() to replace the process’ memory space with a new program Parent calls wait()system call to move itself off the ready queue until the termination of the child Operating System Concepts – 9th Edition 3.30 Silberschatz, Galvin and Gagne ©2013 Let’s code!! #include #include #include pid_t data type stands for process identification and it is used to represent process ids. int main() { int mypid=getpid(); printf("mypid is %d\n",mypid); //create Child Child duplicates parent pid_t pidFork = fork(); Operating System Concepts – 9th Edition 3.31 Silberschatz, Galvin and Gagne ©2013 Let’s code!! #include #include #include int main(){ int mypid=getpid(); Child duplicates parent printf("My PID is %d\n",mypid); printf("My Parent's PID is %d\n",getppid()); //create Child pid_t pidFork=fork(); printf("mypidFork = %d- my pid= %d- my Parent's pid= %d\n", pidFork, getpid(), getppid()); return 0; } Operating System Concepts – 9th Edition 3.32 Silberschatz, Galvin and Gagne ©2013 Let’s code!! #include #include #include int main(){ int mypid=getpid(); Child duplicates parent printf("My PID is %d\n",mypid); printf("My Parent's PID is %d\n",getppid()); //create Child pid_t pidFork=fork(); printf("mypidFork = %d- my pid= %d- my Parent's pid= %d\n", pidFork, getpid(), getppid()); return 0; } Operating System Concepts – 9th Edition 3.33 Silberschatz, Galvin and Gagne ©2013 C Program Forking Separate Process fork() returns § 0 for the new child process (executing the child) § > 0 for the parent process (executing the parent) – it is the pid of the child § < 0: error Operating System Concepts – 9th Edition 3.34 Silberschatz, Galvin and Gagne ©2013 Let’s code!! Operating System Concepts – 9th Edition 3.35 Silberschatz, Galvin and Gagne ©2013 Let’s code!! Operating System Concepts – 9th Edition 3.36 Silberschatz, Galvin and Gagne ©2013 Let’s code!! Operating System Concepts – 9th Edition 3.37 Silberschatz, Galvin and Gagne ©2013 Let’s code!! Parent Running.. Child not created yet Operating System Concepts – 9th Edition 3.38 Silberschatz, Galvin and Gagne ©2013 Let’s code!! Child is created Child duplicates Parent Operating System Concepts – 9th Edition 3.39 Silberschatz, Galvin and Gagne ©2013 Let’s code!! Child duplicates Parent Common area for child and parent Operating System Concepts – 9th Edition 3.40 Silberschatz, Galvin and Gagne ©2013 Let’s code!! Child Operating System Concepts – 9th Edition 3.41 Silberschatz, Galvin and Gagne ©2013 Let’s code!! Parent Operating System Concepts – 9th Edition 3.42 Silberschatz, Galvin and Gagne ©2013 Let’s code!! Common area for child and parent Operating System Concepts – 9th Edition 3.43 Silberschatz, Galvin and Gagne ©2013 Let’s code!! Operating System Concepts – 9th Edition 3.44 Silberschatz, Galvin and Gagne ©2013 Let’s code!! The parent waits for the child process to complete with the wait() system call Operating System Concepts – 9th Edition 3.45 Silberschatz, Galvin and Gagne ©2013 Let’s code!! Operating System Concepts – 9th Edition 3.46 Silberschatz, Galvin and Gagne ©2013 Let’s code!! Operating System Concepts – 9th Edition 3.47 Silberschatz, Galvin and Gagne ©2013 Let’s code!! Operating System Concepts – 9th Edition 3.48 Silberschatz, Galvin and Gagne ©2013 C Program Forking Separate Process The return code for the fork() is zero for the new (child) è pid of the child process is zero The parent pid is an integer value greater than zero (in fact, it is the actual pid of the child process) The parent waits for the child process to complete with the wait() system call Operating System Concepts – 9th Edition 3.49 Silberschatz, Galvin and Gagne ©2013 Process Termination Process executes last statement and then asks the operating system to delete it using the exit() system call. Returns status data from child to parent (via wait()) Process’ resources are deallocated by operating system Parent may terminate the execution of children processes. Some reasons for doing so: Child has exceeded allocated resources Task assigned to child is no longer required The parent is exiting and the operating systems does not allow a child to continue if its parent terminates The parent process may wait for termination of a child process by using the wait()system call. The call returns status information and the pid of the terminated process pid = wait(&status); Operating System Concepts – 9th Edition 3.50 Silberschatz, Galvin and Gagne ©2013 Example Operating System Concepts – 9th Edition 3.51 Silberschatz, Galvin and Gagne ©2013 Example Operating System Concepts – 9th Edition 3.52 Silberschatz, Galvin and Gagne ©2013 Example Operating System Concepts – 9th Edition 3.53 Silberschatz, Galvin and Gagne ©2013 Example Operating System Concepts – 9th Edition 3.54 Silberschatz, Galvin and Gagne ©2013 Example Operating System Concepts – 9th Edition 3.55 Silberschatz, Galvin and Gagne ©2013 Example Operating System Concepts – 9th Edition 3.56 Silberschatz, Galvin and Gagne ©2013 Example Operating System Concepts – 9th Edition 3.57 Silberschatz, Galvin and Gagne ©2013 Example Operating System Concepts – 9th Edition 3.58 Silberschatz, Galvin and Gagne ©2013 Example P1 Parent P2 P3 Child1 Child2 P4 Child2 Child1>0 and Child 2> 0èParent Child1>0 and Child2==0 è Child2(P3) Child1==0 and Child2>0èChild1 Child1==0 and Child2==0 è Child2(P4) Operating System Concepts – 9th Edition 3.59 Silberschatz, Galvin and Gagne ©2013 Example Operating System Concepts – 9th Edition 3.60 Silberschatz, Galvin and Gagne ©2013 Example P1 (PID 1448) Parent P2 (PID 1449) P3 (PID 1450) Child1 Child2 P4 (PID 1451) Child2 Operating System Concepts – 9th Edition 3.61 Silberschatz, Galvin and Gagne ©2013 Example (1): 2 children of a Parent Parent Child1 Child2 Operating System Concepts – 9th Edition 3.62 Silberschatz, Galvin and Gagne ©2013 Example (1): 2 children of a Parent Operating System Concepts – 9th Edition 3.63 Silberschatz, Galvin and Gagne ©2013 Example (1): 2 children of a Parent Operating System Concepts – 9th Edition 3.64 Silberschatz, Galvin and Gagne ©2013 Example (1): 2 children of a Parent P1 (PID 1690) Parent P2 (PID 1691) P3 (PID 1692) Child1 Child2 Operating System Concepts – 9th Edition 3.65 Silberschatz, Galvin and Gagne ©2013 Example (2) : 2 children of a Parent Operating System Concepts – 9th Edition 3.66 Silberschatz, Galvin and Gagne ©2013 Example (2) : 2 children of a Parent P1 (PID 1982) Parent P2 (PID 1983) P3 (PID 1984) Child1 Child2 Operating System Concepts – 9th Edition 3.67 Silberschatz, Galvin and Gagne ©2013 Zombie If no parent waiting (did not invoke wait()) process is a zombie All processes transition to this state when they terminate, but generally they exist as zombies only briefly. The exit status of the zombie process can be read by the parent process using the wait() system call. After that, the zombie process is removed from the system. If the parent process does not use the wait() system call, the zombie process is left in the process table. Operating System Concepts – 9th Edition 3.68 Silberschatz, Galvin and Gagne ©2013 Orphan If parent terminated without invoking wait(), process is an orphan Linux and UNIX address this scenario by assigning the init process (pid=1) as the new parent to orphan processes Operating System Concepts – 9th Edition 3.69 Silberschatz, Galvin and Gagne ©2013 Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Communication in Client-Server Systems Operating System Concepts – 9th Edition 3.70 Silberschatz, Galvin and Gagne ©2013