Processes and Threads

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

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

Lecture 2 Process Management

Chapter 3: Processes. Chapter 3: Processes. Process in Memory. Process Concept. Process State. Diagram of Process State

Chapter 3: Processes. Operating System Concepts 8th Edition

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

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

Part Two - Process Management. Chapter 3: Processes

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

Processes. Electrical and Computer Engineering Stephen Kim ECE/IUPUI RTOS & Apps 1

Process Concept. Chapter 4: Processes. Diagram of Process State. Process State. Process Control Block (PCB) Process Control Block (PCB)

Chapter 4: Processes

Chapter 4: Processes. Process Concept

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

Chapter 3: Processes. Operating System Concepts Essentials 2 nd Edition

COP 4610: Introduction to Operating Systems (Spring 2016) Chapter 3: Process. Zhi Wang Florida State University

CHAPTER 2: PROCESS MANAGEMENT

Chapter 3: Processes

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

Chapter 4: Processes. Process Concept

Chapter 4: Threads. Chapter 4: Threads

Chapter 4: Processes

Chapter 4: Multithreaded Programming. Operating System Concepts 8 th Edition,

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

Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads. Operating System Concepts

COP 4610: Introduction to Operating Systems (Spring 2014) Chapter 3: Process. Zhi Wang Florida State University

Diagram of Process State Process Control Block (PCB)

OPERATING SYSTEMS. UNIT II Sections A, B & D. An operating system executes a variety of programs:

Chapter 3: Process Concept

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

The Big Picture So Far. Chapter 4: Processes

Processes. Operating System Concepts with Java. 4.1 Sana a University, Dr aimen

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

The Big Picture So Far. Chapter 4: Processes

Chapter 3: Process Concept

CS 450 Operating System Week 4 Lecture Notes

Chapter 3: Processes. Operating System Concepts 9 th Edition

Chapter 3: Processes

Process Concept Process in Memory Process State new running waiting ready terminated Diagram of Process State

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

Chapter 3: Process Concept

Chapter 3: Process Concept

I.-C. Lin, Assistant Professor. Textbook: Operating System Concepts 8ed CHAPTER 4: MULTITHREADED PROGRAMMING

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

Process. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

Chapter 3: Processes. Operating System Concepts 8th Edition, modified by Stewart Weiss

Chapter 3: Processes

CHAPTER 3 - PROCESS CONCEPT

CS307: Operating Systems

Chapter 4: Threads. Chapter 4: Threads

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

What Is A Process? Process States. Process Concept. Process Control Block (PCB) Process State Transition Diagram 9/6/2013. Process Fundamentals

CSE 4/521 Introduction to Operating Systems

OPERATING SYSTEM. Chapter 4: Threads

Chapter 4: Multithreaded Programming

Chapter 5: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads

Chapter 4: Processes

Chapter 4: Processes. Process Concept. Process State

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

Chapter 3 Processes. Process Concept. Process Concept. Process Concept (Cont.) Process Concept (Cont.) Process Concept (Cont.)

Chapter 4: Threads. Operating System Concepts with Java 8 th Edition

Chapter 4: Threads. Chapter 4: Threads. Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues

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

Chapter 4: Multi-Threaded Programming

Processes and Threads

Chapter 4: Multithreaded Programming Dr. Varin Chouvatut. Operating System Concepts 8 th Edition,

TDIU25: Operating Systems II. Processes, Threads and Scheduling

Chapter 3: Processes. Operating System Concepts 9 th Edition

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

Notice: This set of slides is based on the notes by Professor Perrone of Bucknell and the textbook authors Silberschatz, Galvin, and Gagne

Chapter 4 Multithreaded Programming

Chapter 3: Process Concept

2. PROCESS. Operating System Concepts with Java 8th Edition Silberschatz, Galvin and Gagn

Chapter 4: Multithreaded

Chapter 4: Multithreaded Programming

ICS Principles of Operating Systems

CS420: Operating Systems

Processes. Process Concept

Chapter 3: Processes. Operating System Concepts 9 th Edit9on

Chapter 4: Threads. Operating System Concepts 9 th Edition

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

Multithreaded Programming

Processes. CSE 2431: Introduction to Operating Systems Reading: Chap. 3, [OSC]

CS307 Operating Systems Processes

Processes. Process Concept. The Process. The Process (Cont.) Process Control Block (PCB) Process State

Process. Program Vs. process. During execution, the process may be in one of the following states

Process Concept Process Scheduling Operations On Process Inter-Process Communication Communication in Client-Server Systems

Chapter 4: Threads. Operating System Concepts 9 th Edition

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Outlook. Process Concept Process Scheduling Operations on Processes. IPC Examples

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

Chapter 5: Threads. Single and Multithreaded Processes

Lecture 4 Threads. (chapter 4)

CS370 Operating Systems

CISC2200 Threads Spring 2015

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

Transcription:

TDDI04 Concurrent Programming, Operating Systems, and Real-time Operating Systems Processes and Threads [SGG7] Chapters 3 and 4 Copyright Notice: The lecture notes are mainly based on Silberschatz s, Galvin s and Gagne s book ( Operating System Concepts, 7th ed., Wiley, 2005). No part of the lecture notes may be reproduced in any form, due to the copyrights reserved by Wiley. These lecture notes should only be used for internal teaching purposes at the Linköping University. Acknowledgment: The lecture notes are originally compiled by C. Kessler, IDA. Klas Arvidsson, IDA, Linköpings universitet. Processes and Threads Overview Process Concept Context Switch Scheduling Queues Creation and Termination Cooperating Processes Interprocess Communication Thread Concept Multithreading Models Threading Issues TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.2 Process Concept Process = a program in execution A process includes: text section (loaded program) program counter stack (+ heap) data section (global variables) process in memory 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 ready: The process is waiting to be assigned to a process terminated: The process has finished execution Remark: [SGG7] uses the terms job and process almost interchangeably. TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.3 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.4 Process Control Block (PCB) CPU Switch From Process to Process Information associated with each process Process state Program counter CPU registers CPU scheduling information Memory-management information Accounting information I/O status information TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.5 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.6 1

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 Context-switch time is overhead the system does no useful work while switching time depends on hardware support Process 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 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.7 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.8 Ready Queue And Various I/O Device Queues Schedulers Long-term scheduler (or job scheduler) for batch systems new jobs for execution queued on disk selects which processes should be brought into the ready queue, and loads them into memory for execution controls the degree of multiprogramming invoked very infrequently (seconds, minutes) No long-term scheduler on UNIX and Windows; instead swapping, controlled by medium-term scheduler Short-term scheduler (or CPU scheduler) selects which ready process should be executed next invoked very frequently (milliseconds) must be fast TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.9 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.10 CPU-bound vs I/O-bound processes 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 (or medium-term) scheduler should aim at a good process mix. Scheduling Non-preemptive scheduling: process keeps CPU until it terminates or voluntarily releases it (sleep() step back into ready queue) Preemptive scheduling: OS puts process from CPU back into ready queue after a certain time quantum has passed TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.11 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.12 2

Process Creation Parent process creates children processes, which, in turn create other processes, forming a tree of processes Resource sharing variants: Parent and children share all resources Children share subset of parent s resources Parent and child share no resources Execution variants: Parent and children execute concurrently Parent waits until children terminate Address space variants: Child is a duplicate of parent Child has a program loaded into it TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.13 Example: Process Creation in UNIX fork system call creates new child process exec system call used after a fork to replace int main() { Pid_t pid; /* fork another process: */ pid = fork(); C program forking a separate process if (pid < 0) { /* error occurred */ fprintf ( stderr, "Fork the process memory space Failed ); with a new program exit(-1); wait system call else if (pid == 0) { /* child by parent, suspends parent process */ execution until child process execlp ( "/bin/ls", "ls", has terminated NULL ); else { /* parent process */ /* will wait for child to complete: */ wait (NULL); printf ("Child Complete"); TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.14 exit(0); A typical tree of processes in Solaris Process Termination Process executes last statement and asks the operating system to delete it (exit) Process returns status value to its parent (used in wait) OS de-allocates process s resources Parent may terminate execution of children processes (abort) Child has exceeded allocated resources Task assigned to child is no longer required If parent is exiting: Some OS do not allow child to continue after parent terminates > All children terminated - cascading termination TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.15 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.16 Cooperating Processes Independent process cannot affect or be affected by execution of another process Cooperating process can affect or be affected by execution of another process Advantages of process cooperation: Information sharing Computation speed-up Modularity Convenience Inter-Process Communication (IPC) shared memory message passing TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.17 IPC Models Realization by OS IPC via Message Passing IPC via Shared Memory TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.18 3

Example for IPC: Producer-Consumer Problem Producer-Consumer paradigm for cooperating processes: producer process produces data items that are consumed by a consumer process Realization with shared memory: Shared buffer (queue) of data items unbounded-buffer Producer > places no practical limit on the size of the buffer > Consumer must wait when buffer is empty bounded-buffer > assumes that there is a fixed buffer size > Producer must also wait when buffer is full buffer Consumer Bounded-Buffer Shared-Memory Solution Shared buffer: #define BUFFER_SIZE 10 typedef struct { out in... Producer Consumer item; buffer item buffer[buffer_size]; int in = 0; int out = 0; buffer empty when in == out buffer full when ((in+1) % BUFFER_SIZE) == out can hold at most BUFFER_SIZE 1 elements TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.19 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.20 Bounded-Buffer Producer and Consumer IPC with Message Passing while (true) { /*... produce an item */ while (((in + 1) % BUFFER SIZE) == out) while (true) { ; /* do nothing -- no free buffers */ buffer[in] = item; in = (in + 1) % BUFFER SIZE; while (in == out) ; // do nothing -- nothing to consume item = buffer[out]; out = (out + 1) % BUFFER SIZE; //... now use the item; Producer Producer code buffer Consumer Consumer code TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.21 Message system processes communicate with each other without resorting to shared variables provides two basic operations: > send( receiverpid, message) > receive( senderpid, message) In order to communicate, two processes establish a communication link between them exchange messages via send/receive [SGG7] 3.4.2. More about message passing variants and programming in TDDC78 Programming of Parallel Computers TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.22 Client-Server Communication Message passing variant for client-server systems Sockets Endpoint for IPC between clients and servers addressed by (IP address, port number) instead of PID Remote Procedure Calls Client calls function of (maybe remote) server process by sending a RPC request to a server socket address Server listens on socket port for incoming RPC requests In Java: Remote Method Invocation (RMI) [SGG7] 3.6 Threads Overview Thread Concept Multithreading Models Threading Issues Thread libraries Pthreads [SGG7] 4.3.1 Win32 Threads [SGG7] 4.3.2 Java Threads [SGG7] 4.3.3 OS thread implementations Windows XP Threads [SGG7] 4.5.1 Linux Threads [SGG7] 4.5.2 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.23 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.24 4

Single- and Multithreaded Processes Benefits of Multithreading Responsiveness Interactive application can continue even when part of it is blocked A thread is a basic unit of CPU utilization: Thread ID, program counter, register set, stack. Resource Sharing Threads of a process share its memory by default. Economy Light-weight Creation, management, context switching for threads is much faster than for processes Utilization of Multiprocessor Architectures May be represented in a Thread Control Block (TCB) A process may have one or several threads. TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.25 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.26 User Threads Thread management (scheduling, dispatch) done by user-level threads library, without kernel support. Kernel views all user threads of a multithreaded process as a single thread of control. process dispatched as a unit +: user control of scheduling algorithm; less overhead : user threads do not scale well to multiprocessor systems Three primary user-level thread libraries: POSIX Pthreads Win32 threads Java threads TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.27 Kernel Threads Managed by the OS kernel (Kernel-specific thread API) Each kernel thread services (executes) one or several user threads +: Flexible: OS can dispatch ready threads of a multithreaded process even if some other thread is blocked. : Kernel invocation overhead; less portable Supported in Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.28 Multithreading Models Relationship user threads kernel threads: Many-to-One (M:1) One-to-One (1:1) Many-to-One Many user-level threads mapped to single kernel thread Low overhead, but not scalable to multiprocessors Examples: Solaris Green Threads GNU Portable Threads Many-to-Many (M:N) Variations: Two-Level Model Light-Weight Processes [SGG7] 4.4.6 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.29 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.30 5

One-to-One Each user-level thread maps to one kernel thread +: more concurrency; scalable to multiprocessors : overhead of creating a kernel thread for each user thread Examples Windows NT/XP/2000 Linux Solaris 9 and later Many-to-Many Model Allows many user level threads to be mapped to many kernel threads Allows the OS to create a sufficient number of kernel threads Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.31 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.32 Two-level Model Similar to Many-to-Many, except that it allows a user thread to be bound to a kernel thread Examples Solaris 8 and earlier IRIX HP-UX Tru64 UNIX Threading Issues Semantics of fork() and exec() system calls Thread cancellation Signal handling [SGG7] 4.4.3 Thread pools Thread specific data [SGG7] 4.4.5 Scheduler activations [SGG7] 4.4.6 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.33 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.34 Semantics of fork() and exec() Does fork() duplicate only the calling thread or all threads? UNIX supports both variants (2 versions of fork()). If exec() used after fork() to replace process no need to duplicate all threads. Thread Cancellation Terminating a thread before it has finished Two general approaches: Asynchronous cancellation terminates the target thread immediately Deferred cancellation allows the target thread to periodically check if it should be cancelled Mechanism: e.g., UNIX signal handling [SGG7] 4.4.3 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.35 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.36 6

Thread Pools Create a number of threads in a pool where they await work Advantages: Faster to service a request with an existing thread than to create a new thread Allows the number of threads in the application(s) to be bound to the size of the pool Win32 API OpenMP User-level thread library example: Pthreads A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization API specifies behavior, not implementation, of the thread library C interface, e.g. int pthread_create ( pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine, void*), void *arg); Common in UNIX operating systems (Solaris, Linux, Mac OS X) [SGG7] 4.3.1 In High-Performance Computing domain replaced by OpenMP (www.openmp.org) TDDC78 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.37 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.38 OS Thread Implementation Example: Linux Threads Linux does not distinguish between processes and threads. Linux refers to them as tasks rather than threads Degree of sharing can be controlled flexibly Java Threads Java threads are managed by the JVM Java threads may be created by: Extending Thread class Implementing the Runnable interface Java thread states: Creation through clone() system call allows a child task to share the address space etc. of the parent task (process) TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.39 [SGG7] 4.3.3 TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.40 What have we learned? Processes versus Threads Process control block Context switch Ready queue and other queues used for scheduling Long-/Mid-term versus Short-term scheduler Process creation and termination Process tree Inter-Process Communication Basics of Synchronization User threads versus Kernel threads Threading models: M:1, 1:1, M:N, two-level Examples: Pthreads API, Linux tasks TDDI04, K. Arvidsson, IDA, Linköpings universitet. 4.41 7