CSCE 313: Intro to Computer Systems

Similar documents
CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Processes and Threads

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

Chapter 4: Threads. Chapter 4: Threads

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

Chapter 3: Processes. Operating System Concepts 8th Edition

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

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

Lecture 2 Process Management

CHAPTER 2: PROCESS MANAGEMENT

CS307: Operating Systems

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

OPERATING SYSTEM. Chapter 4: Threads

CS 450 Operating System Week 4 Lecture Notes

Chapter 4: Multithreaded Programming

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

Chapter 4: Multi-Threaded Programming

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

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

Chapter 4: Threads. Chapter 4: Threads

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

Processes and Threads

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

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

Chapter 4: Multithreaded

CSE 4/521 Introduction to Operating Systems

Chapter 4: Multithreaded Programming

Chapter 4: Multithreaded Programming

Chapter 4: Threads. Operating System Concepts 9 th Edition

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

Chapter 4: Threads. Operating System Concepts 9 th Edition

Lecture 4 Threads. (chapter 4)

Multithreaded Programming

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

Chapter 4: Multithreaded Programming

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

Part Two - Process Management. Chapter 3: Processes

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

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

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

CISC2200 Threads Spring 2015

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

Chapter 4 Multithreaded Programming

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

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

Chapter 5: Threads. Outline

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

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

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

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

Operating Systems. Lecture 05

CS420: Operating Systems

Chapter 5: Threads. Single and Multithreaded Processes

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

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

Processes. Process Concept

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

Chapter 3: Processes

Operating Systems Prof. Ashok K Agrawala

Chapter 3: Process Concept

Chapter 3: Process Concept

CSC 539: Operating Systems Structure and Design. Spring 2006

Che-Wei Chang Department of Computer Science and Information Engineering, Chang Gung University

Chapter 3: Process Concept

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

Process Description and Control

Killing Zombies, Working, Sleeping, and Spawning Children

Chapter 4: Processes. Process Concept

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

Chapter 4: Processes

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

Threads. CS3026 Operating Systems Lecture 06

CHAPTER 3 - PROCESS CONCEPT

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

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

Chapter 3: Process Concept

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

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

Chap 4, 5: Process. Dongkun Shin, SKKU

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

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

CSMC 412. Operating Systems Prof. Ashok K Agrawala Ashok Agrawala Set 4. September 2006 CMSC 412 Set 4

Operating Systems & Concurrency: Process Concepts

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

Chapter 3: Processes. Operating System Concepts 9 th Edit9on

ICS Principles of Operating Systems

COP 4610: Introduction to Operating Systems (Spring 2015) Chapter 4: Threads. Zhi Wang Florida State University

Processes and Non-Preemptive Scheduling. Otto J. Anshus

TDIU25: Operating Systems II. Processes, Threads and Scheduling

Chapter 4: Threads. Operating System Concepts 8 th Edition,

Definition Multithreading Models Threading Issues Pthreads (Unix)

4.8 Summary. Practice Exercises

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

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

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads

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

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

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

Chapter 3: Processes

Transcription:

CSCE 313 Introduction to Computer Systems Instructor: Dr. Guofei Gu http://courses.cse.tamu.edu/guofei/csce313/ Programs, Processes, and Threads Programs and Processes Threads 1

Programs, Processes, and Threads Programs and Processes Threads Processes Management What is a process? How to control processes. How to allocate the available resources to the execution of the processes (scheduling) How to coordinate processes among themselves (synchronization) 2

Processes and Process Control Q: What is a process? Process as execution of a Program We can trace the execution of a process Process as minimal entity for resource allocation (for example memory). Simple Memory Layout of a Running Program high address command-line arguments and environment variables stack heap uninitialized static data initialized static data low address program text 3

The Execution Trace of Processes Two processes and a dispatcher b a d program B program A dispatcher Traces of processes A and B a a+1 a+2 a+3 a+4 a+5 a+6 a+7 a+8 a+9 a+10 a+11 d d+1 d+2 d+3 d+4 b b+1 b+2 b+3 b+4 b+5 b+6 b+7 b+8 b+9 b+10 b+11 Trace of dispatcher b b+1 b+2 b+3 b+4 d d+1 d+2 d+3 d+4 a a+1 a+2 a+3 a+4 d d+1 d+2 d+3 d+4 b+5 b+6 b+7... States of a Process User view: A process is executing continuously In reality: Several processes compete for the CPU and other resources A process may be running: it holds the CPU and is executing instructions Blocked(waiting): it is waiting for some I/O event to occur ready: it is waiting to get back on the CPU preempt Create (new) ready I/O or Event complete dispatch blocked running I/O or event wait Exit (terminated) 4

Mechanism of a process switch: Process Switch Process A Process B (idle) Preempt Process A and store all relevant information. Load information about Process B and continue execution Preempt Process B and store all relevant information. Load information about Process A and continue execution (idle) (idle) process identification processor state information process control information Process Control Block The PCB contains all information specific to a process. 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 Time dependent on hardware support 5

Process Control Block (PCB) process identification processor state information process id parent process id user id etc register set condition codes processor status process control information process state scheduling information event (wait-for) memory-mgmt information owned resources (e.g., list of opened files) 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 6

Example for the Use of PCBs: Process Queues ready running ready queue waiting disk 1 disk 2 executing process I/O device queues serial I/O Scheduler Long-term scheduler (or job scheduler) selects which processes should be brought into the ready queue controls degree of multiprogramming must select a good process mix of I/O-bound and CPU-bound processes Short-term scheduler (or CPU scheduler) selects which process should be executed next and allocates CPU executes at least every 100ms, therefore must be very fast Medium-term scheduler (swapper) in some Oss sometimes good to temporarily remove processes from memory (suspended) 7

Suspended Processes start suspended ready ready running suspended blocked blocked Process Creation When? Submission of a batch job User logs on Create process to provide service such as printing Spawned by existing processes How? In UNIX: all processes created by fork() system call 8

Process Creation (Cont) Parent process create children processes, which, in turn create other processes, forming a tree of processes Generally, process identified and managed via a process identifier (pid) Resource sharing Parent and children share all resources Children share subset of parent s resources Parent and child share no resources Execution Parent and children execute concurrently Parent waits until children terminate Process Creation (Cont) Address space Child duplicate of parent Child has a program loaded into it 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 9

Process Creation C Program Forking Separate Process int main() { pid_t pid; /* fork another process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); exit(-1); } else if (pid == 0) { /* child process */ execlp("/bin/ls", "ls", NULL); } else { /* parent process */ /* parent will wait for the child to complete */ wait (NULL); printf ("Child Complete"); exit(0); } } 10

Program Example 1 (chain) int main (int argc, char *argv[]) { pid_t childpid = 0; int i, n; if (argc!= 2){ /* check for valid number of command-line arguments */ fprintf(stderr, "Usage: %s processes\n", argv[0]); return 1; } n = atoi(argv[1]); for (i = 1; i < n; i++) if (childpid = fork()) break; fprintf(stderr, "i:%d process ID:%ld parent ID:%ld child ID:%ld\n", i, (long)getpid(), (long)getppid(), (long)childpid); return 0; } Program Example 2 (fan) int main (int argc, char *argv[]) { pid_t childpid = 0; int i, n; if (argc!= 2){ /* check for valid number of command-line arguments */ fprintf(stderr, "Usage: %s processes\n", argv[0]); return 1; } n = atoi(argv[1]); for (i = 1; i < n; i++) if ((childpid = fork()) <= 0) break; fprintf(stderr, "i:%d process ID:%ld parent ID:%ld child ID:%ld\n", i, (long)getpid(), (long)getppid(), (long)childpid); return 0; } 11

Some work for you How to create the following process tree? 0 1 2a 2b 3a 3b 3c 3d A tree of processes on a typical Solaris 12

Process Termination Process executes last statement and asks the operating system to delete 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 If parent is exiting Some operating system do not allow child to continue if its parent terminates All children terminated - cascading termination Processes in UNIX fork() created preempted not enough memory ready swapped swap in swap out enough memory ready reschedule process preempt kernel running interrupt system call return return to user user running wakeup wakeup sleep exit sleep swapped swap out sleep in memory zombie 13

Programs, Processes, and Threads Programs and Processes Threads Threads Traditionally, processes interact very little: processes as jobs in batch queue user processes kernel This is not true in modern systems: Some applications may want to have multiple, tightly-coupled processes. 14

Single and Multithreaded Processes A thread is a flow of control within a process Problems with traditional (heavy-weight) processes Process user stack kernel stack Heavy-weight processes have separate address spaces: Process creation is expensive Process switch is expensive Sharing memory areas among processes non-trivial PCB data segment 15

Threads thread TCB user stack kernel stack... thread TCB user stack kernel stack Threads share address space: Thread creation much simpler than process creation (no need to create and initialize address space, etc.) Thread switch simple Threads fully share the address space Convenience communication between threads Efficiency multiprogramming within a process multiprocessors PCB data segment Benefits Responsiveness Increase responsiveness for a multithreaded interactive application (e.g., web browser) Resource Sharing Share memory and resources (e.g., code, data) Economy (creation and context-switch) In Solaris, creating a process is about 30 times slower than creating a thread; context switching is 5 times slower Utilization of multiprocessor architecture (Scalability) 16

Multithreaded Server Architecture Concurrent Execution on a Single-core System 17

Parallel Execution on a Multicore System User-Level vs. Kernel-Level Threads User-level: kernel not aware of threads Kernel-level: all thread-management done in kernel threads library P P 18

User Threads Thread management done by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads Supported by the Kernel Examples Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X Kernel Threads 19

Multithreading Models Many-to-One Many user-level threads mapped to single kernel thread Examples: Solaris Green Threads GNU Portable Threads One-to-One Each user-level thread maps to kernel thread Examples Windows NT/XP/2000 Linux Solaris 9 and later Many-to-Many Potential Problems with Threads (many-to-one) General: Several threads run in the same address space: Protection must be explicitly programmed (by appropriate thread synchronization) Effects of misbehaving threads limited to task User-level threads: Some problems at the interface to the kernel: With a single-threaded kernel, as system call blocks the entire task. thread is blocked in kernel (e.g. waiting for I/O) system call task kernel 20

Singlethreaded vs. Multithreaded Kernel Protection of kernel data structures is trivial, since only one process is allowed to be in the kernel at any time. Special protection mechanism is needed for shared data structures in kernel. Concurrency? Concurrency? 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 Most efficient, also most complex Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package 21

Two-level Model Similar to M:M, except that it allows a user thread to be bound to a kernel thread Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier Two-Level Model: Threads in Solaris 2.x processes user-level threads light-weight processes kernel threads kernel CPUs 22

Threading Issues Semantics of fork() and exec() system calls Thread cancellation of target thread Asynchronous or deferred Thread pools Thread-specific data Semantics of fork() and exec() Does fork() duplicate only the calling thread or all threads? Two versions of fork Duplicates all threads Duplicates only the calling thread Exec() will replace all the process including all threads 23

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 Thread Pools Create a number of threads in a pool where they await work Advantages: Usually slightly faster to service a request with an existing thread than create a new thread Allows the number of threads in the application(s) to be bound to the size of the pool 24

Thread Specific Data Allows each thread to have its own copy of data Useful when you do not have control over the thread creation process (i.e., when using a thread pool) Operating System Examples Windows XP Threads Linux Thread 25

Windows XP Threads Implements the one-to-one mapping, kernel-level Each thread contains A thread id Register set Separate user and kernel stacks Private data storage area The register set, stacks, and private storage area are known as the context of the threads The primary data structures of a thread include: ETHREAD (executive thread block) KTHREAD (kernel thread block) TEB (thread environment block) Windows XP Threads 26

Linux Threads Linux typically refers to threads as tasks Thread creation is done through clone() system call clone() allows a child task to share the address space of the parent task (process) Linux Threads 27