Operating Systems 1 K. Salah Module 2.0: Processes Process Concept Trace of Processes Process Context Context Switching Threads –ULT –KLT.

Slides:



Advertisements
Similar presentations
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Advertisements

Threads, SMP, and Microkernels
Chapter 3 Process Description and Control
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Computer Systems/Operating Systems - Class 8
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
Threads Irfan Khan Myo Thein What Are Threads ? a light, fine, string like length of material made up of two or more fibers or strands of spun cotton,
Chapter 5 Processes and Threads Copyright © 2008.
Process Description and Control Module 1.0. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor.
Processes CSCI 444/544 Operating Systems Fall 2008.
Figure 2.8 Compiler phases Compiling. Figure 2.9 Object module Linking.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
1 Process Description and Control Chapter 3. 2 Process Management—Fundamental task of an OS The OS is responsible for: Allocation of resources to processes.
CSCE 351: Operating System Kernels
3.5 Interprocess Communication
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
Operating Systems 1 K. Salah Module 2.0: Processes and Threads Process Concept Trace of Processes Process Context Context Switching Threads –ULT –KLT.
UNIX Process Creation Every process, except process 0, is created by the fork() system call fork() allocates entry in process table and assigns a unique.
Process Concept An operating system executes a variety of programs
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Process Description and Control A process is sometimes called a task, it is a program in execution.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
University of Pennsylvania 9/12/00CSE 3801 Multiprogramming CSE 380 Lecture Note 3.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Process Description and Control Chapter 3. Major Requirements of an OS Interleave the execution of several processes to maximize processor utilization.
System Calls 1.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
Threads, SMP, and Microkernels Chapter 4. 2 Outline n Threads n Symmetric Multiprocessing (SMP) n Microkernel n Linux Threads.
CSC 501 Lecture 2: Processes. Process Process is a running program a program in execution an “instantiation” of a program Program is a bunch of instructions.
Chapter 3 Process Description and Control
The Structure of Processes (Chap 6 in the book “The Design of the UNIX Operating System”)
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
OS, , Part II Processes Department of Computer Engineering, PSUWannarat Suntiamorntut.
Threads G.Anuradha (Reference : William Stallings)
11/13/20151 Processes ICS 240: Operating Systems –William Albritton Information and Computer Sciences Department at Leeward Community College –Original.
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
1 Threads, SMP, and Microkernels Chapter Multithreading Operating system supports multiple threads of execution within a single process MS-DOS.
University of Pennsylvania 9/12/00CSE 3801 Processes, Interrupts, and System Calls CSE 380 Lecture Note 2 Insup Lee.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Operating Systems: Internals and Design Principles
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Module 2.0: Threads.
Processes & Threads Introduction to Operating Systems: Module 5.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Chapter 2 Process Management. 2 Objectives After finish this chapter, you will understand: the concept of a process. the process life cycle. process states.
1 Process Description and Control Chapter 3. 2 Process A program in execution An instance of a program running on a computer The entity that can be assigned.
Threads, SMP, and Microkernels Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Multiprogramming. Readings r Chapter 2.1 of the textbook.
Introduction to Operating Systems Concepts
Processes and threads.
Process concept.
Process Management Process Concept Why only the global variables?
OPERATING SYSTEMS CS3502 Fall 2017
Process Management Presented By Aditya Gupta Assistant Professor
Structure of Processes
Process & its States Lecture 5.
Lecture Topics: 11/1 General Operating System Concepts Processes
Threads Chapter 4.
Chapter 3: Processes.
CS510 Operating System Foundations
Presentation transcript:

Operating Systems 1 K. Salah Module 2.0: Processes Process Concept Trace of Processes Process Context Context Switching Threads –ULT –KLT

Operating Systems 2 K. Salah Process Also called a task. Useful and Important Concept: Process = program in execution A process is not the same as a program. Program is a passive entity, whereas process is active. Process consists of an executable program, associated data, and execution context. Modern (multiprogramming) operating systems are structured around the concept of a process. Multiprogramming OS supports execution of many concurrent processes. OS issues tend to revolve around management of processes: –How are processes created/destroyed? –How to manage resource requirements of a process during its execution: cpu time, memory, I/O, communication,... ? –How to avoid interference between processes? –How to achieve cooperation and communication between processes?

Operating Systems 3 K. Salah Program Creation Program (say, C program) is edited It is compiled into assembly language, which may consist of several modules. Assembly language modules are assembled into machine language. External references (i.e., to procedures and data in another module) are resolved. This is called linking, which creates a load module. Load or image module is stored as a file in file system and may be executed at a later time by loading into memory to be executed.

Operating Systems 4 K. Salah Process creation and termination Consider a simple disk operating system (like MS-DOS, typically supports only one process at a time) User types command like “run foo” at Keyboard (I/O device driver for keyboard, screen) Command is parsed by command shell Executable program file (load module) “foo” is located on disk (file system, I/O device driver for disk) Contents are loaded into memory and control transferred ==> process comes alive! (device driver for disk, relocating loader, memory management) During execution, process may call OS to perform I/O: console, disk, printer, etc. (system call interface, I/O device drivers) When process terminates, memory is reclaimed. (memory management)

Operating Systems 5 K. Salah Trace of Processes

Operating Systems 6 K. Salah Trace of processes (cont.)

Operating Systems 7 K. Salah Trace of processes (cont.)

Operating Systems 8 K. Salah Trace of processes (cont.)

Operating Systems 9 K. Salah

Operating Systems 10 K. Salah Process Context The context (or image) of a process can be described by –contents of main memory –contents of CPU registers –other info (open files, I/O in progress, etc.) Main memory -- three logically distinct regions of memory: –code region: contains executable code (typically read-only) –data region: storage area for dynamically allocated data structure, e.g., lists, trees (typically heap data structure) –stack region: run-time stack of activation records CPU registers: general registers, PC, SP, PSW, segmentation registers Other info: –open files table, status of ongoing I/O –process status (running, ready, blocked), user id,...

Operating Systems 11 K. Salah Multiprogramming/Timesharing Systems They provide interleaved execution of several processes to give an illusion of many simultaneously executing processes. Computers can be a single-processor or multi-processor machine. The OS must keep track of the state for each active process and make sure that the correct information is properly installed when a process is given control of the CPU.

Operating Systems 12 K. Salah Multiprogramming (multiple processes) For each process, the O.S. maintains a data structure, called the process control block (PCB). The PCB provides a way of accessing all information relevant to a process: –This data is either contained directly in the PCB, or else the PCB contains pointers to other system tables. Processes (PCBs) are manipulated by two main components of the process subsystem in order to achieve the effects of multiprogramming: –Scheduler: determines the order by which processes will gain access to the CPU. Efficiency and fair-play are issues here. –Dispatcher: actually allocates CPU to process next in line as determined by the scheduler.

Operating Systems 13 K. Salah The Process Control Block (PCB) The PCB typically contains the following types of information: Process status (or state): new, ready to run, user running, kernel running, waiting, halted –Program counter: where in program the process is executing –CPU registers: contents of general-purpose register stack pointer, PSW, index registers –Memory Management info: segment base and limit registers, page table, location of pages on disk, process size –User ID, Group ID, Process ID, Parent PID,... –Event Descriptor: when process is in the “sleep” or waiting state –Scheduling info: process priority, size of CPU quantum, length of current CPU burst

Operating Systems 14 K. Salah PCB (cont.) –List of pending signals –Accounting info: process execution time, resource utilization –Real and Effective User IDs: determine various privileges allowed the process such as file access rights –More timers: record time process has spent executing in user and Kernel mode –Array indicating how process wishes to react to signals –System call info: arguments, return value, error field for current system call –Pending I/O operation info: amount of data to transfer, addr in user memory, file offset,... –Current directory and root: file system environment of process –Open file table: records files process has open

Operating Systems 15 K. Salah Process States & Transitions

Operating Systems 16 K. Salah Context Switching Let us first review the user/system mode distinction. When system starts (during system bootstrapping or boot) it is in system mode. This is process 0 in V.2, which creates process 1 (init). This process execs /etc/init and is then executing in user mode. Process 1, like any user process, continues executing in user mode until one of the following: –interrupt by an asynchronous device like timer, disk, or terminal –the process makes a system call by executing an instruction to cause a software interrupt Occurrence of such an event causes the CPU to switch to system mode and begin execution appropriate interrupt handler.

Operating Systems 17 K. Salah A Tree of Processes On A Typical UNIX System

Operating Systems 18 K. Salah When to context switch Typically, hardware automatically saves the user PC and PSW when interrupt occurs, and takes new PC from interrupt vector. Interrupt handler may simply perform its function and then return to the same process that was interrupted (restoring the PC and PSW from the stack). Alternatively, may no longer be appropriate to resume execution of process that was running because:  process has used up its current CPU quantum  process has requested I/O and must wait for results  process has asked to be suspended (sleep) for some amount of time  a signal or error requires process be destroyed (killed)  a “higher priority” process should be given the CPU In such a situation, a context switch is performed to install appropriate info for running a new process.

Operating Systems 19 K. Salah Mechanics of a Context Switch 1copy contents of CPU registers (general-purpose, SP, PC, PSW, etc.) into a save area in the PCB of running process 2change status of running process from “running” to “waiting” (or “ready”) 3change a system variable running-process to point to the PCB of new process to run 4copy info from register save area in PCB of new process into CPU registers Note:  context switching is pure overhead and should be done as fast as possible  often hardware-assisted - special instructions for steps 1 and 4  determining new process to run accomplished by consulting scheduler queues  step 4 will start execution of new process - known as dispatching.

Operating Systems 20 K. Salah MULTIPROGRAMMING Through CONTEXT SWITCHING

Operating Systems 21 K. Salah Introduction to Threads Multitasking OS can do more than one thing concurrently by running more than a single process Processes can do several things concurrently be running more than a single thread. Each thread is a different stream of control that can execute its instructions independently. A program (e.g. Browser) may consist of the following threads: GUI thread I/O thread computation

Operating Systems 22 K. Salah Processes and Threads A typical process consists of  a running program  a bundle of resources (file descriptor table, address space) A thread, called a lightweight process, has its own  stack  CPU Registers  state  All the other resources are shared by all threads of that process. These include:  open files  virtual address space (code and data segments).  child processes

Operating Systems 23 K. Salah Processes vs. Threads

Operating Systems 24 K. Salah Single Threaded and Multithreaded Process Models  Thread Control Block contains a register image, thread priority and thread state information.

Operating Systems 25 K. Salah Benefits of Threads vs Processes  Takes less time to create a new thread than a process  Less time to terminate a thread than a process  Less time to switch between two threads within the same process  Since threads within the same process share memory and files, they can communicate with each other without invoking the kernel. However, it is necessary to synchronize the activities of various threads so that they do not obtain inconsistent views of the data. Example 1: a file or web server on a LAN. The server needs to handle several file or web requests over a short period. Hence more efficient to create (and destroy) a single thread for each request. Example 2: one thread display menu and read user input while the other thread execute user commands

Operating Systems 26 K. Salah Threads States  Three key states: running, ready, blocked  Termination of a process, terminates all threads within the process

Operating Systems 27 K. Salah User-Level Threads (ULT) The kernel is not aware of the existence of threads All thread management is done by the application by using a thread library Thread switching does not require kernel mode privileges (no mode switch) Scheduling is application specific

Operating Systems 28 K. Salah Threads library Contains code for: –creating and destroying threads –passing messages and data between threads –scheduling thread execution –saving and restoring thread contexts

Operating Systems 29 K. Salah Kernel activity for ULTs The kernel is not aware of thread activity but it is still managing process activity When a thread makes a system call, the whole process will be blocked but for the thread library that thread is still in the running state So thread states are independent of process states

Operating Systems 30 K. Salah Advantages and inconveniences of ULT Advantages –Thread switching does not involve the kernel: no mode switching –Scheduling can be application specific: choose the best algorithm. –ULTs can run on any OS. Only needs a thread library Inconveniences –Most system calls are blocking and the kernel blocks processes. So all threads within the process will be unable to run –The kernel can only assign processes to processors. Two threads within the same process cannot run simultaneously on two processors

Operating Systems 31 K. Salah Kernel-Level Threads (KLT) All thread management is done by kernel No thread library but an API (I.e. system calls) to the kernel thread facility Kernel maintains context information for the process and the threads Switching between threads requires the kernel Scheduling on a thread basis Ex: Windows NT and OS/2

Operating Systems 32 K. Salah Advantages and inconveniences of KLT Advantages –the kernel can simultaneously schedule many threads of the same process on many processors –blocking is done on a thread level –kernel routines can be multithreaded Inconveniences –thread switching within the same process involves the kernel. We have 2 mode switches per thread switch: user to kernel and kernel to user. –this results in a significant slow down

Operating Systems 33 K. Salah Combined ULT/KLT Approaches Thread creation done in the user space Bulk of scheduling and synchronization of threads done in the user space The programmer may adjust the number of KLTs May combine the best of both approaches Example is Solaris