Process Concept An operating system executes a variety of programs Batch system – jobs Time-shared systems – user programs or tasks Process – a program in execution A process includes: Text segment Data segment Stack segment Program counter Operating System Concepts 4.1 Silberschatz, Galvin and Gagne 2002 Process State As a process executes, it changes state new: The process is being created. running: Instructions are being executed on the CPU. waiting: The process is waiting for some event to occur. ready: The process is waiting for the CPU. terminated: The process has finished execution. Operating System Concepts 4.2 Silberschatz, Galvin and Gagne 2002 Process Control Block (PCB) Information associated with each process. Process state Program counter CPU register values CPU scheduling information Memory-management information Accounting information I/O status information Operating System Concepts 4.3 Silberschatz, Galvin and Gagne 2002 Process Queues Job queue – set of all PCBs 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 a resource, e.g., an I/O device. PCBs migrate between the various queues. Operating System Concepts 4.4 Silberschatz, Galvin and Gagne 2002 Ready Queue And Various I/O Device Queues Operating System Concepts 4.5 Silberschatz, Galvin and Gagne 2002 Context Switch When the CPU switches to another process, the system must Switch to kernel mode Save the state of the old process Deal with the interrupt Load the saved state for the new process. Hardware (and possibly OS) saves the state of the old process The scheduler selects the new process The dispatcher loads the new process Set the PC Switch to user mode Context-switch time is overhead; the system does no useful work while switching Time dependent on hardware support. Operating System Concepts 4.6 Silberschatz, Galvin and Gagne 2002 CPU Switch From Process to Process Operating System Concepts 4.7 Silberschatz, Galvin and Gagne 2002 Process Creation Parent process create children processes, which, in turn create other processes, forming a tree of processes. Address space (one of) Child duplicate of parent. Child has a program loaded into it. Execution (one of) Parent and child execute concurrently Parent waits until child terminates Resource sharing (one of) Parent and children share all resources. Children share subset of parent’s resources. Parent and child share no resources. Operating System Concepts 4.8 Silberschatz, Galvin and Gagne 2002 Process Termination Process executes last statement and tells the OS Output status from child to parent Process’ resources are (eventually) deallocated by the operating system Parent may terminate execution of children processes Child has exceeded allocated resources. Task assigned to child is no longer required. Parent is exiting With cascading termination, the operating system does not allow child to continue if its parent terminates Operating System Concepts 4.9 Silberschatz, Galvin and Gagne 2002 UNIX PCBs Operating System Concepts 4.10 Silberschatz, Galvin and Gagne 2002 UNIX Shell Operating System Concepts 4.11 Silberschatz, Galvin and Gagne 2002 Threads A thread (or lightweight process) is a basic unit of CPU utilization; it consists of: Program counter Register set Stack space A thread shares with its peer threads its: Code segment Data segment Operating-system resources A traditional or heavyweight process is equal to a task with one thread. Operating System Concepts 4.12 Silberschatz, Galvin and Gagne 2002 Single and Multithreaded Processes Operating System Concepts 4.13 Silberschatz, Galvin and Gagne 2002 Thread Implementations User-level threads library POSIX Pthreads Mach C-threads Solaris threads Kernel Threads Windows 95/98/NT/2000 Solaris Linux Operating System Concepts 4.14 Silberschatz, Galvin and Gagne 2002 Many-to-One Many user-level threads mapped to single kernel thread. Used on systems that do not support kernel threads. Operating System Concepts 4.15 Silberschatz, Galvin and Gagne 2002 One-to-One Each user-level thread maps to kernel thread. Examples Windows 95/98/NT/2000 OS/2 LinuxThreads Operating System Concepts 4.16 Silberschatz, Galvin and Gagne 2002 Many-to-Many Model Allows many user level threads to be mapped to many kernel threads. Allows the operating system to create a sufficient number of kernel threads. Solaris 2 Windows NT/2000 with the ThreadFiber package Operating System Concepts 4.17 Silberschatz, Galvin and Gagne 2002 Pthreads A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization. API specifies behavior of the thread library, implementation is up to development of the library. Common in UNIX operating systems. Operating System Concepts 4.18 Silberschatz, Galvin and Gagne 2002 Benefits Responsiveness In a multiple threaded task, while one thread is blocked and waiting, a second thread in the same task can run. Kernel level threads provide a mechanism that allows sequential processes to make blocking system calls while also achieving parallelism. Resource Sharing Economy Utilization of MP Architectures Operating System Concepts 4.19 Silberschatz, Galvin and Gagne 2002