1 Operating Systems Chapter 3:Processes Operating System Concepts – 10th Edition 3.1 Silberschatz, Galvin and Gagne ©2018 Outline Process Concept Process Scheduling Operations on Processes Interprocess Communication IPC in Shared-Memory Systems IPC in Message-Passing Systems Operating System Concepts – 10th Edition 3.2 Silberschatz, Galvin and Gagne ©2018 Objectives Identify the separate components of a process and illustrate how they are represented and scheduled in an operating system. Describe how processes are created and terminated in an operating system. Describe and contrast interprocess communication using shared memory and message passing. Operating System Concepts – 10th Edition 3.3 Silberschatz, Galvin and Gagne ©2018 Process Concept An operating system executes a variety of programs that run as a process. Process – a program in execution; process execution must progress in sequential fashion. No parallel execution of instructions of a single process. Program is passive entity stored on disk (executable file); process is active. • Program becomes process when an executable file is loaded into memory. Execution of program started via GUI mouse clicks, command line entry of its name, etc. One program can be several processes. Operating System Concepts – 10th Edition 3.4 Silberschatz, Galvin and Gagne ©2018 Process Concept (Cont.) The memory layout of a process is typically divided into multiple sections: • • • • • The program code, also called text section. Data section containing global variables. Current activity including program counter, processor registers. Heap containing memory dynamically allocated during run time. Stack containing temporary data. Function parameters, return addresses, local variables. The sizes of the text and data sections are fixed, as their sizes do not change during program run time. However, the stack and heap sections can shrink and grow dynamically during program execution; operating system must ensure they do not overlap one another. Operating System Concepts – 10th Edition 3.5 Silberschatz, Galvin and Gagne ©2018 Process in Memory Operating System Concepts – 10th Edition 3.6 Silberschatz, Galvin and Gagne ©2018 Memory Layout of a C Program This figure is similar to the general concept of a process in memory as, with a few differences: The global data section is divided into different sections for (a) initialized data and (b) uninitialized data. A separate section is provided for the argc and argv parameters passed to the main() function. Operating System Concepts – 10th Edition 3.7 Silberschatz, Galvin and Gagne ©2018 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 (such as an I/O completion or reception of a signal). • Ready: The process is waiting to be assigned to a processor. • Terminated: The process has finished execution. Operating System Concepts – 10th Edition 3.8 Silberschatz, Galvin and Gagne ©2018 Diagram of Process State • New: The process is being created. • Running: Instructions are being executed. • Waiting: The process is waiting for some event to occur (such as an I/O completion or reception of a signal). • Ready: The process is waiting to be assigned to a processor. • Terminated: The process has finished execution. Operating System Concepts – 10th Edition 3.9 Silberschatz, Galvin and Gagne ©2018 Process Control Block (PCB) Information associated with each process (also called task control block). 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. In brief, the PCB serves as the repository for all the data needed to start, or restart, a process, and some accounting data. Operating System Concepts – 10th Edition 3.10 Silberschatz, Galvin and Gagne ©2018 Threads So far, a process is a program that performs a single thread of execution. For example, when a process is running a word-processor program, a single thread of instructions is being executed. This single thread of control allows the process to perform only one task at a time. Thus, the user cannot simultaneously type in characters and run the spell checker. Most modern operating systems have extended the process concept to allow a process to have multiple threads of execution and thus to perform more than one task at a time. This feature is especially beneficial on multicore systems, where multiple threads can run in parallel. A multithreaded word processor could, for example, assign one thread to manage user input while another thread runs the spell checker. Must then have storage for thread details, multiple program counters in PCB. Operating System Concepts – 10th Edition 3.11 Silberschatz, Galvin and Gagne ©2018 Process Representation in Linux Represented by the C structure task_struct pid t_pid; /* long state; /* unsigned int time_slice /* struct task_struct *parent;/* struct list_head children; /* struct files_struct *files;/* struct mm_struct *mm; /* process */ Operating System Concepts – 10th Edition 3.12 process identifier */ state of the process */ scheduling information */ this process’s parent */ this process’s children */ list of open files */ address space of this Silberschatz, Galvin and Gagne ©2018 Process Scheduling For a system with a single CPU core, there will never be more than one process running at a time, whereas a multicore system can run multiple processes at one time. Process scheduler selects among available processes for next execution on CPU core. Goal -- Maximize CPU use, quickly switch processes onto CPU core. Maintains scheduling queues of processes. • Ready queue – set of all processes residing in main memory, ready and waiting to execute. • Wait queues – set of processes waiting for an event (i.e., I/O). • Processes migrate among the various queues. Operating System Concepts – 10th Edition 3.13 Silberschatz, Galvin and Gagne ©2018 Ready and Wait Queues As processes enter the system, they are put into a ready queue, where they are ready and waiting to execute on a CPU’s core. This queue is generally stored as a linked list; a ready-queue header contains pointers to the first PCB in the list, and each PCB includes a pointer field that points to the next PCB in the ready queue. Operating System Concepts – 10th Edition 3.14 Silberschatz, Galvin and Gagne ©2018 Ready and Wait Queues The system also includes other queues. When a process is allocated a CPU core, it executes for a while and eventually terminates, is interrupted, or waits for the occurrence of a particular event, such as the completion of an I/O request. Process is placed in a wait queue. Operating System Concepts – 10th Edition 3.15 Silberschatz, Galvin and Gagne ©2018 Representation of Process Scheduling A new process is initially put in the ready queue. It waits there until it is selected for execution, or dispatched. Once the process is allocated a CPU core and is executing, one of several events could occur: Operating System Concepts – 10th Edition 3.16 Silberschatz, Galvin and Gagne ©2018 Representation of Process Scheduling 1. The process could issue an I/O request and then be placed in an I/O wait queue. 2. The process could create a new child process and then be placed in a wait queue while it awaits the child’s termination. 3. The process could be removed forcibly from the core, as a result of an interrupt or having its time slice expire, and be put back in the ready queue. Operating System Concepts – 10th Edition 3.17 Silberschatz, Galvin and Gagne ©2018 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 pure overhead; the system does no useful work while switching. • The more complex the OS and the PCB the longer the context switch. Time dependent on hardware support. • Some hardware provides multiple sets of registers per CPU multiple contexts loaded at once. Operating System Concepts – 10th Edition 3.18 Silberschatz, Galvin and Gagne ©2018 CPU Switch From Process to Process Operating System Concepts – 10th Edition 3.19 Silberschatz, Galvin and Gagne ©2018 Operations on Processes System must provide mechanisms for: • Process creation • Process termination Operating System Concepts – 10th Edition 3.20 Silberschatz, Galvin and Gagne ©2018 Process Creation Parent process create children processes, which, in turn create other processes, forming a tree of processes. Generally, process identified and managed via a process identifier (pid). The pid provides a unique value for each process in the system. When a process creates a child process, that child process will need certain resources (CPU time, memory, files, I/O devices). Resource 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 – 10th Edition 3.21 Silberschatz, Galvin and Gagne ©2018 Process Creation There are two address-space possibilities for the new process: • The child process is a duplicate of the parent process (it has the same program and data as the parent). • The child process has a new program loaded into it. Operating System Concepts – 10th Edition 3.22 Silberschatz, Galvin and Gagne ©2018 A Tree of Processes in Linux Operating System Concepts – 10th Edition 3.23 Silberschatz, Galvin and Gagne ©2018 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 using the abort() system call. 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. Operating System Concepts – 10th Edition 3.24 Silberschatz, Galvin and Gagne ©2018 Process Termination Some operating systems do not allow child to exists if its parent has terminated. If a process terminates, then all its children must also be terminated. • cascading termination. All children, grandchildren, etc., are terminated. • The termination is initiated by the operating system. 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); If no parent waiting (did not invoke wait()) child process is a zombie. If parent terminated without invoking wait(), child process is an orphan. Operating System Concepts – 10th Edition 3.25 Silberschatz, Galvin and Gagne ©2018 Android Process Importance Hierarchy Because of resource constraints such as limited memory, mobile operating systems may have to terminate existing processes to reclaim limited system resources. Rather than terminating an arbitrary process, Android has identified an importance hierarchy of processes. Android will begin terminating processes that are least important. Operating System Concepts – 10th Edition 3.26 Silberschatz, Galvin and Gagne ©2018 Android Process Importance Hierarchy From most to least important, the hierarchy of process classifications is as follows: • Foreground process: The current process visible on the screen, representing the application the user is currently interacting with. • Visible process: A process that is not directly visible on the foreground but that is performing an activity that the foreground process is referring to (that is, a process performing an activity whose status is displayed on the foreground process). • Service process: A process that is similar to a background process but is performing an activity that is apparent to the user (such as streaming music). • Background process: A process that may be performing an activity but is not apparent to the user. • Empty process: A process that holds no active components associated with any application. Operating System Concepts – 10th Edition 3.27 Silberschatz, Galvin and Gagne ©2018 Android Process Importance Hierarchy Operating System Concepts – 10th Edition 3.28 Silberschatz, Galvin and Gagne ©2018 Interprocess Communication Processes within a system may be independent or cooperating. A process is independent if it does not share data with any other processes executing in the system. A process is cooperating if it can affect or be affected by the other processes executing in the system. Reasons for cooperating processes: • Information sharing: Several applications may be interested in the same piece of information (for instance, copying and pasting), OS must provide an environment to allow concurrent access to such information. • Computation speed up: If we want a particular task to run faster, we must break it into subtasks, each of which will be executing in parallel with the others. Notice that such a speedup can be achieved only if the computer has multiple processing cores. Operating System Concepts – 10th Edition 3.29 Silberschatz, Galvin and Gagne ©2018 Interprocess Communication Cooperating processes need interprocess communication (IPC) Two models of IPC • Shared memory: In the shared-memory model, a region of memory that is shared by the cooperating processes is established. Processes can then exchange information by reading and writing data to the shared region. • Message passing: communication takes place by means of messages exchanged between the cooperating processes. Operating System Concepts – 10th Edition 3.30 Silberschatz, Galvin and Gagne ©2018 Communications Models (a) Shared memory. Operating System Concepts – 10th Edition (b) Message passing. 3.31 Silberschatz, Galvin and Gagne ©2018 Multiprocess Architecture – Chrome Browser Many web browsers ran as single process (some still do) • If one web site causes trouble, entire browser can hang or crash Google Chrome Browser is multiprocess with 3 different types of processes: • Browser process manages user interface, disk and network I/O. • Renderer process renders web pages, deals with HTML, Javascript. A new renderer created for each website opened • Plug-in process for each type of plug-in. Operating System Concepts – 10th Edition 3.32 Silberschatz, Galvin and Gagne ©2018 Multiprocess Architecture – Chrome Browser The advantage of the multiprocess approach is that websites run in isolation from one another. If one website crashes, only its renderer process is affected; all other processes remain unharmed. Operating System Concepts – 10th Edition 3.33 Silberschatz, Galvin and Gagne ©2018