Processes and Threads. Processes and Threads. Processes (2) Processes (1)

Similar documents
Process Characteristics. Threads Chapter 4. Process Characteristics. Multithreading vs. Single threading

Threads Chapter 4. Reading: 4.1,4.4, 4.5

Threads Chapter 5 1 Chapter 5

Processes. Sanzheng Qiao. December, Department of Computing and Software

Lecture Topics. Announcements. Today: Threads (Stallings, chapter , 4.6) Next: Concurrency (Stallings, chapter , 5.

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

Process a program in execution; process execution must progress in sequential fashion. Operating Systems

Process Characteristics

Chapter 3 Process Description and Control

Processes. Process Management Chapter 3. When does a process gets created? When does a process gets terminated?

operating system Spring 2017 Prof.Dr. Hasan Balik Student Name : Walid.W. Ramadan mansour Student ID :

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Operating Systems: Internals and Design Principles. Chapter 4 Threads Seventh Edition By William Stallings

CSCE 313: Intro to Computer Systems

Announcement. Exercise #2 will be out today. Due date is next Monday

2 Introduction to Processes

What is a Process. Processes. Programs and Processes. System Classification 3/5/2013. Process: An execution stream and its associated state

For use by students enrolled in #71251 CSE430 Fall 2012 at Arizona State University. Do not use if not enrolled.

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Chapter 3: Processes. Operating System Concepts 8 th Edition,

Threads, SMP, and Microkernels

Part V. Process Management. Sadeghi, Cubaleska RUB Course Operating System Security Memory Management and Protection

Processes and Threads

Process Description and Control. Chapter 3

Processes and More. CSCI 315 Operating Systems Design Department of Computer Science

Process Concepts. CSC400 - Operating Systems. 3. Process Concepts. J. Sumey

Chap 4, 5: Process. Dongkun Shin, SKKU

W4118 Operating Systems. Junfeng Yang

Operating System. Chapter 4. Threads. Lynn Choi School of Electrical Engineering

Process Description and Control

CSE 380 Computer Operating Systems. Instructor: Insup Lee. University of Pennsylvania Fall 2003

CSE 153 Design of Operating Systems Fall 2018

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Operating Systems. Lecture 05

Computer Systems II. First Two Major Computer System Evolution Steps

Lecture 3: Processes. CMPUT 379, Section A1, Winter 2014 January 13, 15 and 17

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - III Processes. Louisiana State University. Processes. September 1 st, 2009

Chapter 4: Processes. Process Concept

Chapter 4: Processes

Process Scheduling Queues

Lecture 4: Process Management

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Process Description and Control. Major Requirements of an Operating System

Major Requirements of an Operating System Process Description and Control

What is an Operating System? A Whirlwind Tour of Operating Systems. How did OS evolve? How did OS evolve?

CHAPTER 2: PROCESS MANAGEMENT

Operating Systems. Engr. Abdul-Rahman Mahmood MS, PMP, MCP, QMR(ISO9001:2000) alphapeeler.sf.net/pubkeys/pkey.htm

Chapter 3: Processes. Operating System Concepts 8th Edition,

Process Description and Control. Chapter 3

Roadmap. Tevfik Ko!ar. CSC Operating Systems Spring Lecture - III Processes. Louisiana State University. Virtual Machines Processes

Module 4: Processes. Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess Communication

Module 4: Processes. Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess Communication

Major Requirements of an OS

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

Processes and Threads

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Processes. OS Structure. OS Structure. Modes of Execution. Typical Functions of an OS Kernel. Non-Kernel OS. COMP755 Advanced Operating Systems

Chapter 3: Processes. Operating System Concepts 9 th Edit9on

Processes and Threads

Process Description and Control. Chapter 3

Announcements. Reading. Project #1 due in 1 week at 5:00 pm Scheduling Chapter 6 (6 th ed) or Chapter 5 (8 th ed) CMSC 412 S14 (lect 5)

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

Page 1. Analogy: Problems: Operating Systems Lecture 7. Operating Systems Lecture 7

Lecture 2 Process Management

IT 540 Operating Systems ECE519 Advanced Operating Systems

CSC 539: Operating Systems Structure and Design. Spring 2006

Process Description and Control

Processes. Process Concept

OS Structure, Processes & Process Management. Don Porter Portions courtesy Emmett Witchel

Chapter 3: Processes

Processes. CS3026 Operating Systems Lecture 05

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

What is a Process? Processes and Process Management Details for running a program

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

Processes & Threads. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! More of the same J

Threads. CS3026 Operating Systems Lecture 06

Processes, Threads, SMP, and Microkernels

! The Process Control Block (PCB) " is included in the context,

ECE519 Advanced Operating Systems

Chapter 3: Process Concept

Chapter 3: Process Concept

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

Multithreading. Reading: Silberschatz chapter 5 Additional Reading: Stallings chapter 4

Operating Systems Overview. Chapter 2

REVIEW OF COMMONLY USED DATA STRUCTURES IN OS

Chapter 5: Processes & Process Concept. Objectives. Process Concept Process Scheduling Operations on Processes. Communication in Client-Server Systems

Chapter 3: Process Concept

Threads, SMP, and Microkernels. Chapter 4

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Chapter 3: Process-Concept. Operating System Concepts 8 th Edition,

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Course: Operating Systems Instructor: M Umair. M Umair

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1

CS510 Operating System Foundations. Jonathan Walpole

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

Chapter 3: Processes

Announcements Processes: Part II. Operating Systems. Autumn CS4023

CS 326: Operating Systems. Process Execution. Lecture 5

Mon Sep 17, 2007 Lecture 3: Process Management

CSCI-GA Operating Systems Lecture 3: Processes and Threads -Part 2 Scheduling Hubertus Franke

Transcription:

Processes and Threads (Topic 2-1) 2 홍성수 Processes and Threads Question: What is a process and why is it useful? Why? With many things happening at once in a system, need some way of separating them all out cleanly. Important concept: Decomposition Solve a hard problem by chopping it into several simpler problems that can be solved separately. 서울대학교공과대학전기공학부 Real-Time Operating Systems Laboratory 1 Processes (1) What? Definition of a process: An execution stream in the context of a particular process state. What is an execution stream and what is a process state? Process state is everything that can affect, or be affected by the process: code, data values, open files, etc. Processes (2) Execution stream is a sequence of instructions performed in a process state. Key simplifying feature of a process: Only one thing happens at a time within a process. System classifications: Uniprogramming: Only one process at a time. Mostly personal computers. Makes some parts of OS easier, but others hard. Multiprogramming: Multiple processes at a time. Most systems support multiprogramming. 2 3

Processes (3) Processes (4): PCB With multiprogramming, OS must keep track of the processes: For each process, a process control block (PCB) holds: Execution state (saved registers, etc.). Scheduling information (priority). Accounting and other misc. information (open files). System-wide table of PCB: Process table. Unix: Fixed-size array of PCB s 4 5 Processes (5): State Transition Processes (6): State Transition 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. Ready: The process is waiting to be assigned to CPU. Terminated: The process has finished execution. 6 7

Processes (7): Scheduling Queues Processes (8): Scheduling 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. Process migration between the various queues. 8 9 Processes (9): Scheduling Process Scheduling (1) For several processes to share a CPU, the OS must have: Fair scheduling Make sure each process gets a chance to run. Protection Making sure processes don t trash each other. 10 11

Process Scheduling (2) Process Scheduling (3) Dispatcher: Inner-most portion of the OS that runs processes: loop forever { 1)Run the process for a while. 2)Process and save its state. 3) Load state of another process. } Dispatcher policies and mechanisms: How does the dispatcher keep control? CPU can only be doing one thing at a time. User process running means that dispatcher isn t. Which process is executed next? Need to locate runnable processes efficiently. 12 13 Process Scheduling (4) Process Scheduling (5) How does the dispatcher regain control? (1)Trust the process to wake the dispatcher up. Problem: Sometimes processes misbehave. (2) Provide the dispatcher with an alarm clock. Timer hardware and interrupts. Control returns to OS on: Traps: Events internal to the user processes: System calls. Errors (illegal instructions, address error, etc). Page faults. Interrupts: Events external fro the user process: Character typed at a terminal. Completion of a disk transfer. Timer: to make sure OS eventually gets control. 14 15

Process Scheduling (6) Process Scheduling (7) Once dispatcher gets control how to decide who s next? Possibilities: (1) Scan process table for first runnable process: Might spend much time searching. Results in weird priorities: Small PIDs better. Question: How do you know a process is runnable? (2) Link together the runnable processes into a queue. Dispatcher takes from the head of the queue. Runnable processes are inserted at back of queue. Called Ready list or Run queue. (3) Assign priorities to processes. Keep queue sorted by priority. Separate queue per priority. 16 17 Process Scheduling (8) Who decides priorities and how are priorities chosen? Who? A separate part of the OS: the scheduler Question: Why not by the dispatcher? Concept: Separation of mechanism and policy. How? Subject of the next topic (Topic 3). Context Switching (1) How does the dispatcher save and restore state? Mechanism Context switch What must get saved? Everything that next process could or will damage: Program counter, Processor status word (condition codes, etc.), General purpose registers, floating-point registers. All of memory? 18 19

Context Switching (2) Context Switching (3) Memory could be large so saving it could be expensive Possibilities: (1) Don t save memory Trust next process. Multiprogramming on PC and Mac. Called threads or lightweight-processes. (2) Save all memory to disk. Also, an early personal computer/ workstation. Assume disk transfers at one megabyte/ second. How long does saving a 4 megabyte process take? (3) Protection memory from next process: Memory management Topic 6. 20 21 Context Switching (4) Context Switching (5) Context switching implementation: OSPCBCur StkPtr OS-PCB CPU s SP OSPCBCur StkPtr OS-PCB Machine dependent: Different for MIPS, SPARC, 386, etc. Tricky: OS must execute code to save state without changing the process state. Requires some special hardware support. Example: Save PC and PSR on trap or interrupt. before after PSW SEG task OFF task AX CX DX BX SP BP SI DI ES HICH Memory INT call PUSHA instruction LOW MEMORY after before PSW SEG task OFF task AX CX DX BX SP BP SI DI ES HICH Memory INT call PUSHA instruction LOW MEMORY 22 <Lower priority proc> <Higher priority proc> Stack frames during a context Switch (80186/80188 Small) 23

Context Switching (6) StkPtr Short vs Long Term Scheduler OS-PCB void * data SEG task OFF task PSW SEG task OFF task AX CX DX BX SP BP SI DI ES HICH Memory Simulation of call to far function with an argument PSW = 0x0200 (Interrupt enabled) INT call PUSHA instruction LOW MEMORY Stack frame when procs are created (80186/80188 Small) 24 25 Process Creation (1) Process Creation (2) Creating new processes: Build one from scratch; (e.g. Unix Process 0). Clone an existing one; (e.g. Unix fork() syscall). From scratch: 1) Load code and data into memory. 2) Create (empty) call stack. 3) Create and initialize a process control block. 4) Put process on ready list. 26 27

Process Creation (3) Cloning: Unix fork() system call. Process Creation in Unix (4) Swapper (Process 0) 1) Stop current process and save its state. 2) Make a copy of code, data, stack, and PCB. Init (Process 1) Pagedaemont (Process 2) 3) Add new PCB to ready list. Not quite right. What s missing? Return to the child and the parent. Process creation in Unix with fork() and exec(): /etc/rc /etc/ttys getty login 28 sh 29 Process Creation (5) Shell example: for(;;) { cmd = readcmd(); pid = fork(); if(pid == 0) { // Child Setup environment. exec(cmd); exit(1); } else { //Parent Wait for command to finish. wait(pid); } } Process Termination Process executes last statement and asks the operating system to decide it (exit). Output data from child to parent (via wait). Process resources are deallocated by operating system. Parent may terminate execution of children processes (abort). Child has exceeded allocated resources. Task assigned to child is no longer required. Parent is exiting. Operating system does not allow child to continue if its parent terminates. Cascading termination. 30 31

Process Characteristics (1) Unit of resource ownership - process is allocated: - a virtual address space to hold the process image - control of some resources (files, I/O devices...) Unit of dispatching - process is an execution path through one or more programs - execution may be interleaved with other process - the process has an execution state and a dispatching priority Process Characteristics (2) These two characteristics are treated independently by some recent OS The unit of dispatching is usually referred to a thread or a lightweight process The unit of resource ownership is usually referred to as a process or task 32 33 Multithreading vs. Single threading Threads and Processes Multithreading: when the OS supports multiple threads of execution within a single process Single threading: when the OS does not recognize the concept of thread MS-DOS support a single user process and a single thread UNIX supports multiple user processes but only supports one thread per process Solaris supports multiple threads 34 35

Processes Have a virtual address space which holds the process image Protected access to processors, other processes, files, and I/O resources Threads Has an execution state (running, ready, etc.) Saves thread context when not running Has an execution stack and some per-thread static storage for local variables Has access to the memory address space and resources of its process all threads of a process share this when one thread alters a (non-private) memory item, all other threads (of the process) sees that a file open with one thread, is available to others 36 37 Single Threaded and Multithreaded Process Models 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 Thread Control Block contains a register image, thread priority and thread state information 38 39

Benefits of Threads Example 1: a file server on a LAN It needs to handle several file requests over a short period Hence more efficient to create (and destroy) a single thread for each request On a SMP machine: multiple threads can possibly be executing simultaneously on different processors Example 2: one thread displays menu and reads user input while the other thread execute user commands Application Benefits of Threads Consider an application that consists of several independent parts that do not need to run in sequence Each part can be implemented as a thread Whenever one thread is blocked waiting for an I/O, execution could possibly switch to another thread of the same application (instead of switching to another process) 40 41 Benefits of Threads Since threads within the same process share memory and files, they can communicate with each other without invoking the kernel Therefore necessary to synchronize the activities of various threads so that they do not obtain inconsistent views of the data Example of Inconsistent View 3 variables: A, B, C which are shared by thread T1 and thread T2 T1 computes C = A+B T2 transfers amount X from A to B T2 must do: A = A -X and B = B+X (so that A+B is unchanged) But if T1 computes A+B after T2 has done A = A-X but before B = B+X then T1 will not obtain the correct result for C = A + B 42 43

Threads States User-Level Threads (ULT) Three key states: running, ready, blocked They have no suspend state because all threads within the same process share the same address space Indeed: suspending (ie: swapping) a single thread involves suspending all threads of the same process Termination of a process, terminates all threads within the process 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 44 45 Threads Library Kernel Activity for ULTs Contains code for: creating and destroying threads passing messages and data between threads scheduling thread execution saving and restoring thread contexts 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 46 47

Advantages and Inconveniences of ULT Kernel-Level Threads (KLT) 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 blocked The kernel can only assign processes to processors. Two threads within the same process cannot run simultaneously on two processors All thread management is done by kernel No thread library but an API 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 48 49 Advantages and inconveniences of KLT Combined ULT/KLT Approaches 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 this results in a significant slow down 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 50 51

Solaris Process includes the user s address space, stack, and process control block User-level threads (threads library) invisible to the OS are the interface for application parallelism Kernel threads the unit that can be dispatched on a processor and it s structures are maintain by the kernel Lightweight processes (LWP) each LWP supports one or more ULTs and maps to exactly one KLT each LWP is visible to the application 52 Process 2 is equivalent to a pure ULT approach Process 4 is equivalent to a pure KLT approach We can specify a different degree of parallelism (process 3 and 5) 53 Solaris: Versatility We can use ULTs when logical parallelism does not need to be supported by hardware parallelism (we save mode switching) Ex: Multiple windows but only one is active at any one time Solaris: User-Level Thread Execution Transitions among these states is under the exclusive control of the application a transition can occur only when a call is made to a function of the thread library If threads may block then we can specify two or more LWPs to avoid blocking the whole application It s only when a ULT is in the active state that it is attached to a LWP (so that it will run when the kernel level thread runs) a thread may transfer to the sleeping state by invoking a synchronization primitive and later transfer to the runnable state when the event waited for occurs A thread may force another thread to go to the stop state... 54 55

Solaris: User-Level Thread States Decomposition of User-Level Active State When a ULT is active, it is associated to an LWP and, thus, to a KLT Transitions among the LWP states is under the exclusive control of the kernel (Attached to an LWP) An LWP can be in the following states: running: when the KLT is executing blocked: because the KLT issued a blocking system call (but the ULT remains bound to that LWP and remains active) runnable: waiting to be dispatched to CPU 56 57 Solaris: Lightweight Process States LWP states are independent of ULT states (except for bound ULTs) 58