Chapter 3: Processes Rev. by Kyungeun Park, 2015 Operating System Concepts Essentials – 8th Edition Silberschatz, Galvin and Gagne ©2011 Chapter 3: Processes Process Concept Process Scheduling Operations on Processes: creation, termination, scheduling, communication Inter Process Communication (IPC) Examples of IPC Systems Communication in Client-Server (C/S) Systems Operating System Concepts Essentials – 8th Edition 3.2 Silberschatz, Galvin and Gagne ©2011 Objectives To introduce the notion of a process -- a program in execution, which forms the basis of all computation To describe the various features of processes, including scheduling, creation and termination, and communication To describe communication in client-server systems Operating System Concepts Essentials – 8th Edition 3.3 Silberschatz, Galvin and Gagne ©2011 The notion of a process A system consists of a collection of processes: operating system processes for system code user processes for user code Concurrent execution of these processes with the CPU multiplexed among them by switching the CPU between them Making the computer more productive Operating System Concepts Essentials – 8th Edition 3.4 Silberschatz, Galvin and Gagne ©2011 Process Concept An operating system executes a variety of programs: Batch system – jobs Time-shared systems – user programs or tasks Textbook uses the terms job and process almost interchangeably Process – a program in execution; process execution must progress in sequential fashion Operating System Concepts Essentials – 8th Edition 3.5 Silberschatz, Galvin and Gagne ©2011 The Process Multiple parts of a process The program code, also called text section Current activity represented by program counter and processor registers Stack containing temporary data Function parameters, return addresses, local variables Data section containing global variables Heap for dynamically allocated memory during run time A program by itself is not a process: program is a passive entity, a file containing a list of instructions stored on disk process is an active entity with a program counter. Program becomes process when executable file loaded into memory Execution of program started via GUI mouse clicks and command line entry of its name, etc. One program can be several processes Consider multiple users executing the same program (email) Many copies of the Web browser program by a user can be separate processes Operating System Concepts Essentials – 8th Edition 3.6 Silberschatz, Galvin and Gagne ©2011 Process in Memory temporary data: function parameters, return addresses, local variables dynamically allocated memory global variables program code 7 Operating System Concepts Essentials – 8th Edition 3.7 Silberschatz, Galvin and Gagne ©2011 Process State Transition As a process executes, it changes state. new: The process is being created ready: The process 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 Essentials – 8th Edition 3.8 Silberschatz, Galvin and Gagne ©2011 Process Control Block (PCB) Information associated with each process, described by a process control block, varies from process to process Process state: new, ready, running, waiting, halted, etc. Program counter: address of next instruction CPU registers: accumulators, index registers, stack pointers, general-purpose registers, condition code, … CPU scheduling information: a process priority, pointers to scheduling queues, scheduling parameters Memory-management information: the base and limit registers, the page tables, the segment tables Accounting information: CPU time usage, time limits, account numbers, job or process numbers I/O status information: the list of I/O devices per process, a list of open files, etc. Operating System Concepts Essentials – 8th Edition 3.9 Silberschatz, Galvin and Gagne ©2011 CPU Switch From Process to Process Operating System Concepts Essentials – 8th Edition 3.10 Silberschatz, Galvin and Gagne ©2011 Threads So far, process has a single thread of execution A process with multiple threads of execution Beneficial on multicore systems PCB extension needed: Consider having multiple program counters per process Multiple locations can execute at once Multiple threads of control -> threads Must then have storage for thread details, multiple program counters in PCB Operating System Concepts Essentials – 8th Edition 3.11 Silberschatz, Galvin and Gagne ©2011 PCB in Linux Represented by the C structure: task_struct founded in <linux/sched.h> pid t_pid; long state; struct sched_entity se; struct task_struct *parent; struct list_head children; struct files_struct *files; struct mm_struct *mm; Operating System Concepts Essentials – 8th Edition /* /* /* /* /* /* /* process identifier */ state of the process */ scheduling information */ this process’s parent */ this process’s children */ list of open files */ address space of this process */ 3.12 Silberschatz, Galvin and Gagne ©2011 Process Scheduling Maximize CPU utilization, switch the CPU among processes so frequently that users can interact with each program while running Time sharing Process scheduler selects a process among available processes for next execution on the CPU 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 Operating System Concepts Essentials – 8th Edition 3.13 Silberschatz, Galvin and Gagne ©2011 Ready Queue And Various I/O Device Queues Operating System Concepts Essentials – 8th Edition 3.14 Silberschatz, Galvin and Gagne ©2011 Queueing-Diagram Representation of Process Scheduling Operating System Concepts Essentials – 8th Edition 3.15 Silberschatz, Galvin and Gagne ©2011 Schedulers Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU Sometimes the only scheduler in a system Short-term scheduler is invoked frequently (milliseconds) (must be fast) Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue Long-term scheduler is invoked 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 Long-term scheduler strives for good process mix Operating System Concepts Essentials – 8th Edition 3.16 Silberschatz, Galvin and Gagne ©2011 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 Operating System Concepts Essentials – 8th Edition 3.17 Silberschatz, Galvin and Gagne ©2011 Multitasking in Mobile Systems Some mobile systems (e.g., early version of iOS) allow only one process to run, others suspended Due to screen real estate, user interface limits iOS provides for a Single foreground process- controlled via user interface Multiple background processes– in memory, running, but not on the display, and with limits 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 uses a service, a separate application component, to perform tasks Service can keep running on behalf of the background process, even if background process is suspended Service has no user interface, small memory use Provide an efficient technique for multitasking in a mobile environment Operating System Concepts Essentials – 8th Edition 3.18 Silberschatz, Galvin and Gagne ©2011 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 The more complex the OS and the PCB -> the longer the context switch Advanced memory-management techniques require extra data to be switched with each context Context-switch time is highly dependent on hardware support Some hardware provides multiple sets of registers per CPU -> multiple contexts loaded at once by changing the pointer to the current register set Operating System Concepts Essentials – 8th Edition 3.19 Silberschatz, Galvin and Gagne ©2011 Process Creation Parent process creates children processes, which, in turn create other processes, forming a tree of processes The new child process is a complete copy of the executing program Generally, process identified and managed via a process identifier (pid) getpid () The new child process has a new process identifier. fork () returns the child’s PID to the parent, 0 to the child. When calling exec (), all data in the original program is lost, and replaced with a running copy of the new program: Overlaying 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 Operating System Concepts Essentials – 8th Edition 3.20 Silberschatz, Galvin and Gagne ©2011 Process Creation After fork(), waits on the ready queue until termination of the child process getpid () = 7111 pid = 7112 pid = fork(); getpid () = 7112 pid = 0 Operating System Concepts Essentials – 8th Edition exec() call by either parent or child process, Loads a binary file into memory. 3.21 Silberschatz, Galvin and Gagne ©2011 Choice of Options 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 Address space options Child duplicate of parent Child has a program loaded into it Operating System Concepts Essentials – 8th Edition 3.22 Silberschatz, Galvin and Gagne ©2011 Process Tree for the Linux init pid = 1 login pid = 8415 khelper pid = 6 bash pid = 8416 ps pid = 9298 Operating System Concepts Essentials – 8th Edition sshd pid = 3028 kthreadd pid = 2 pdflush pid = 200 sshd pid = 3610 tcsch pid = 4005 emacs pid = 9204 3.23 Silberschatz, Galvin and Gagne ©2011 C Program Forking Separate Process #include <sys/types.h> #include <stdio.h> #include <unistd.h> int main() { pid_t pid; /* fork another process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); return 1; } else if (pid == 0) { /* child process */ execlp("/bin/ls", "ls", NULL); /*execlp("./pgms/printfibo", "printfibo", 5) */ } else { /* parent process, if (pid > 0) pid holds child process’s id */ /* parent will wait for the child */ wait (NULL); printf ("Child Complete"); } return 0; } Operating System Concepts Essentials – 8th Edition 3.24 Silberschatz, Galvin and Gagne ©2011 Creating a Separate Process via Windows API Operating System Concepts Essentials – 8th Edition 3.25 Silberschatz, Galvin and Gagne ©2011 A Tree of Processes on Solaris Top process in Solaris Root parent process for all user process User login screen Networking services Operating System Concepts Essentials – 8th Edition 3.26 Silberschatz, Galvin and Gagne ©2011 Process Termination (1) Process terminates when it finishes executing the last statement and asks the operating system to delete it by using exit() system call Returns status data from child to parent (via wait() ) Child process’ resources (physical and virtual memory, open files, and I/O buffers) are deallocated by operating system Parent may terminate 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 If parent is exiting Some operating system does not allow a child to continue if its parent terminates – All children terminated - cascading termination Cascading termination: All children, grandchildren, etc. are terminated. The termination is initiated by the operating system. Operating System Concepts Essentials – 8th Edition 3.27 Silberschatz, Galvin and Gagne ©2011 Process Termination (2) Process termination by calling exit() system call. exit(1); /* exit with status 1 */ 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 a process has terminated and no parent waiting (did not invoke wait()), the process is a zombie process If parent terminated without invoking wait(), the process is an orphan process Operating System Concepts Essentials – 8th Edition 3.28 Silberschatz, Galvin and Gagne ©2011 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 supports a multiprocess architecture with 3 different types of processes: Only one Browser process manages user interface, disk and network I/O Many Chromium Renderer processes render web pages for individual websites, deals with HTML, JavaScript as sandboxed processes. If one website crashes, only its renderer process is affected; all other processes remain unharmed. Runs in sandbox (a C++ library that allows the creation of sandboxed processes), restricting disk and network I/O, minimizing effect of security exploits A Plug-in process is created for each type of plug-in in use. Operating System Concepts Essentials – 8th Edition 3.29 Silberschatz, Galvin and Gagne ©2011 Interprocess Communication Processes within a system may be independent or cooperating Independent process cannot affect or be affected by the execution of another process Cooperating process can affect or be affected by other processes by sharing data Reasons for providing process cooperation environment: To allow information sharing Computation speedup by breaking a task into small pieces and allowing cooperation Modularity support by dividing the system functions into separate processes or threads Convenience when working on in parallel Cooperating processes require an Inter-Process Communication (IPC) Two models of IPC Shared memory Message passing Operating System Concepts Essentials – 8th Edition 3.30 Silberschatz, Galvin and Gagne ©2011 Two IPC Models Shared memory Faster than message passing After establishing shared memory, treated as routine memory accesses Message passing Useful for exchanging smaller amounts of data Easy to implement, but more time-consuming task of kernel intervention Operating System Concepts Essentials – 8th Edition 3.31 Silberschatz, Galvin and Gagne ©2011 Shared Memory Systems: Producer-Consumer Problem Establishing a region of shared memory in the address space of the process creating the shared-memory segment Simultaneous access to the shared-memory should be controlled by the user processes other than the operating system Producer-Consumer Problem with common paradigm: Producer process produces information that is consumed by a consumer process (consider Client-Server architecture or Web server vs. Web browser relationship) Two types of buffers unbounded-buffer places no practical limit on the size of the buffer bounded-buffer assumes that there is a fixed buffer size Operating System Concepts Essentials – 8th Edition 3.32 Silberschatz, Galvin and Gagne ©2011 Bounded-Buffer – Shared-Memory Solution Shared buffer: a circular array with two logical pointers: in and out #define BUFFER_SIZE 10 typedef struct { ... } item; item buffer[BUFFER_SIZE]; int in = 0; /* the next free position in the buffer */ int out = 0; /* the first full position in the buffer */ in Initial Empty Buffer (size 10) 0 1 2 9 out Operating System Concepts Essentials – 8th Edition 3.33 Silberschatz, Galvin and Gagne ©2011 Bounded-Buffer – Producer item nextProduced; while (true) { /* Produce an item */ while (((in + 1) % BUFFER SIZE) == out) ; /* do nothing -- no free buffers */ buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; } Buffer (size 10) 0 1 out Buffer full 0 9 in 1 out Operating System Concepts Essentials – 8th Edition 2 3.34 2 9 in Silberschatz, Galvin and Gagne ©2011 Bounded Buffer – Consumer item nextConsumed; while (true) { Buffer (size 10) while (in == out) 0 ; /* do nothing: empty buffer */ 1 /* remove an item from the buffer */ nextConsumed = buffer[out]; 9 out in Empty buffer out = (out + 1) % BUFFER_SIZE; return item; 2 0 in 1 2 9 out } Note: Solution is correct, but can only use BUFFER_SIZE-1 elements. Exercise for a solution where BUFFER_SIZE items can be in the buffer at the same time Operating System Concepts Essentials – 8th Edition 3.35 Silberschatz, Galvin and Gagne ©2011 IPC - Shared Memory Major issues is to provide mechanism that will allow the user processes to synchronize their actions when they access shared memory. Synchronization is discussed in great details in Chapter 5. Operating System Concepts Essentials – 8th Edition 3.36 Silberschatz, Galvin and Gagne ©2011 Interprocess Communication – Message Passing Mechanism for processes to communicate and to synchronize their actions chat program in a distributed environment like the WWW Message system – processes communicate with each other without sharing the same address space, particularly useful in a distributed environment IPC facility provides two operations: If process P and Q wish to communicate, they need to: establish a communication link between them exchange messages via send/receive Implementation of communication link send(message) – message size fixed or variable receive(message) Here, highlight the link’s logical implementation (e.g., logical properties) c.f. physical implementation (e.g., shared memory, hardware bus, or network) Several methods for implementing logical link Direct or indirect communication Synchronous or asynchronous communication Automatic or explicit buffering Operating System Concepts Essentials – 8th Edition 3.37 Silberschatz, Galvin and Gagne ©2011 Implementation Questions How are links established? Can a link be associated with more than two processes? How many links can there be between every pair of communicating processes? What is the capacity of a link? Is the size of a message that the link can accommodate fixed or variable? Is a link unidirectional or bi-directional? Operating System Concepts Essentials – 8th Edition 3.38 Silberschatz, Galvin and Gagne ©2011 Direct Communication Processes must name each other (sender or recipient) explicitly: send (P, message) – send a message to process P receive(Q, message) – receive a message from process Q Properties of communication link Links are established automatically A link is associated with exactly one pair of communicating processes Between each pair, there exists exactly one link The link may be unidirectional, but is usually bi-directional Operating System Concepts Essentials – 8th Edition 3.39 Silberschatz, Galvin and Gagne ©2011 Indirect Communication Messages are directed and received from mailboxes (also referred to as ports) Each mailbox has a unique id Processes can communicate only if they share a mailbox Enhances the modularity Properties of communication link Link established only if processes share a common mailbox A link may be associated with many processes Each pair of processes may share several communication links Link may be unidirectional or bi-directional Operating System Concepts Essentials – 8th Edition 3.40 Silberschatz, Galvin and Gagne ©2011 Indirect Communication Operating System provides a mechanism so that a process as an owner does: create a new mailbox (port) send and receive messages through mailbox destroy a mailbox Primitives are defined as: send(A, message) – send a message to mailbox A receive(A, message) – receive a message from mailbox A Operating System Concepts Essentials – 8th Edition 3.41 Silberschatz, Galvin and Gagne ©2011 Indirect Communication Mailbox sharing P1, P2, and P3 share mailbox A P1, sends; P2 and P3 receive Who gets the message? Initially, the owner is the only process that can receive messages through mailbox. Now, through appropriate system calls, multiple receivers for each mailbox are possible. Solutions Allow a link to be associated with at most two processes Allow only one process at a time to execute a receive operation Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was. Operating System Concepts Essentials – 8th Edition 3.42 Silberschatz, Galvin and Gagne ©2011 Synchronization Message passing may be either blocking or non-blocking Blocking is considered synchronous Blocking send : the sender is blocked until the message is received Blocking receive : the receiver is blocked until a message is available Non-blocking is considered asynchronous Non-blocking send : the sender sends the message and continues Non-blocking receive : the receiver receives a valid message or null message Different combinations possible If both send and receive are blocking, we have a rendezvous Operating System Concepts Essentials – 8th Edition 3.43 Silberschatz, Galvin and Gagne ©2011 Synchronization (Cont.) Producer-consumer becomes trivial when using blocking send() and receive() message next_produced; while (true) { /* produce an item in next produced */ send(next_produced); } message next_consumed; while (true) { receive(next_consumed); /* consume the item in next consumed */ } Operating System Concepts Essentials – 8th Edition 3.44 Silberschatz, Galvin and Gagne ©2011 Buffering Queue of messages attached to the link; Implemented in one of three ways 1. Zero capacity – no messages are queues on a link Sender must wait for receiver (rendezvous) 2. Bounded capacity – finite length of n messages Sender must wait if link full 3. Unbounded capacity – infinite length Sender never waits Operating System Concepts Essentials – 8th Edition 3.45 Silberschatz, Galvin and Gagne ©2011 Examples of IPC Systems - POSIX POSIX Shared Memory Process first creates shared memory object as a file shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666); Also used to open an existing segment to share it Set the size of the object in bytes ftruncate(shm_fd, 4096); Establish a memory-mapped file containing the shared-memory object ptr = mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0) Now the process could write to the shared memory /*ptr points to the shared memory */ sprintf(ptr, “Writing to shared memory”); Operating System Concepts Essentials – 8th Edition 3.46 Silberschatz, Galvin and Gagne ©2011 IPC POSIX Producer Operating System Concepts Essentials – 8th Edition 3.47 Silberschatz, Galvin and Gagne ©2011 IPC POSIX Consumer Operating System Concepts Essentials – 8th Edition 3.48 Silberschatz, Galvin and Gagne ©2011 Examples of IPC Systems - Mach Mach communication is message based Even system calls are made by messages Each task gets two mailboxes at creation- Kernel mailbox and Notify mailbox Only three system calls needed for message transfer msg_send() msg_receive() msg_rpc(): remote procedure call, which sends a message and waits for one return message from the sender, works like a typical subroutine procedure call running on remote system Mailboxes needed for communication, created via port_allocate(): creates a new mailbox and allocates space for its queue of messages Send and receive are flexible, for example, if mailbox full, the sending thread has four options: Wait indefinitely until there is room in the mailbox Wait at most n milliseconds Return immediately without waiting Temporarily cache a message by giving it to the operating system to keep (Only one message to a full mailbox can be pending) Operating System Concepts Essentials – 8th Edition 3.49 Silberschatz, Galvin and Gagne ©2011 Examples of IPC Systems – Windows Message-passing scheme in Windows is called the advanced local procedure call (LPC) facility Only works between processes on the same system Uses ports (like mailboxes) to establish and maintain communication channels Communication works as follows: The client opens a handle to the subsystem’s connection port object. The client sends a connection request. The server creates two private communication ports and returns the handle to one of them to the client. The client and server use the corresponding port handle to send messages or callbacks and to listen for replies. Operating System Concepts Essentials – 8th Edition 3.50 Silberschatz, Galvin and Gagne ©2011 Communications in Client-Server Systems Sockets Remote Procedure Calls Pipes Remote Method Invocation (Java) Operating System Concepts Essentials – 8th Edition 3.51 Silberschatz, Galvin and Gagne ©2011 Sockets A socket is defined as an endpoint for communication Generally, sockets use a client-server architecture. Server waits for incoming client requests by listening to a specified port. After receiving a request, the server accepts a connection from the client socket to complete the connection. Concatenation of IP address and port – a number included at start of message packet to differentiate network services on a host The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8 Communication consists between a pair of sockets All ports below 1024 are well known, used for standard services Special IP address 127.0.0.1 (loopback) to refer to system on which process is running Operating System Concepts Essentials – 8th Edition 3.52 Silberschatz, Galvin and Gagne ©2011 Socket Communication Assigned by host X Connection Well-known port for a Web/HTTP Client Server Operating System Concepts Essentials – 8th Edition 3.53 Silberschatz, Galvin and Gagne ©2011 Sockets in Java Three types of sockets in Java Connection-oriented (TCP) : Socket class Connectionless (UDP) : DatagramSocket class MulticastSocket class– data can be sent to multiple recipients Consider both “Date” server and “Date” client Refer to the textbook pg 138139 Operating System Concepts Essentials – 8th Edition 3.54 Silberschatz, Galvin and Gagne ©2011 Remote Procedure Calls Remote procedure call (RPC) abstracts procedure call mechanism for use between systems with network connections Assume an environment in which the processes are executing on separate systems a message-based communication scheme to provide remote service port number at the start of a message packet Stubs client-side proxy for the actual procedure on the server hides the communication details a separate stub per each separate remote procedure RPC procedure The client-side stub locates the port on the server and marshalls (packages) the parameters The stub transmits a message to the server A similar server-side stub receives this message, unpacks the marshalled parameters, and invokes the procedure on the server Operating System Concepts Essentials – 8th Edition 3.55 Silberschatz, Galvin and Gagne ©2011 Remote Procedure Calls (cont) On Windows, stub code is compiled from a specification written in Microsoft Interface Definition Language (MIDL) MIDL is used for the interfaces between client and server programs. Operating System Concepts Essentials – 8th Edition 3.56 Silberschatz, Galvin and Gagne ©2011 Execution of RPC How to know the port numbers on the server? 1. Fixed port addresses : binding at compile time 2. Dynamic rendezvous mechanism : rendezvous daemon on a fixed RPC port (matchmaker) Operating System Concepts Essentials – 8th Edition 3.57 Silberschatz, Galvin and Gagne ©2011 Pipes Acts as a conduit allowing two processes to communicate Implementation issues: Is communication unidirectional or bidirectional? In the case of two-way communication, is it half or full-duplex? Must there exist a relationship (i.e., parent-child) between the communicating processes? Can the pipes be used over a network? Ordinary pipes – cannot be accessed from outside the process that created it. Typically, a parent process creates a pipe and uses it to communicate with a child process that it created. Named pipes – can be accessed without a parent-child relationship. bidirectional several processes can use it: several writers referred to as FIFOs in Unix Operating System Concepts Essentials – 8th Edition 3.58 Silberschatz, Galvin and Gagne ©2011 Ordinary Pipes Ordinary Pipes allow communication in standard producer-consumer style Producer writes to one end (the write-end of the pipe) Consumer reads from the other end (the read-end of the pipe) Ordinary pipes are therefore unidirectional (one-way communication) Ordinary pipe with parent-child relationship between communicating processes Windows calls these anonymous pipes See Unix and Windows code samples in textbook (pg 143-145) Operating System Concepts Essentials – 8th Edition 3.59 Silberschatz, Galvin and Gagne ©2011