© Janice Regan, CMPT 300, May 2007 0 CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.

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
Operating Systems: Internals and Design Principles
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Chapter 4 Threads, SMP, and Microkernels Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design.
1 Created by Another Process Reason: modeling concurrent sub-tasks Fetch large amount data from network and process them Two sub-tasks: fetching  processing.
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 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Chapter 5 Processes and Threads Copyright © 2008.
1 Threads, SMP, and Microkernels Chapter 4. 2 Process: Some Info. Motivation for threads! Two fundamental aspects of a “process”: Resource ownership Scheduling.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
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.
1 Chapter 4 Threads Threads: Resource ownership and execution.
Threads. Processes and Threads  Two characteristics of “processes” as considered so far: Unit of resource allocation Unit of dispatch  Characteristics.
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.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
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.
Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Chapter 4 Threads, SMP, and Microkernels Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E.
Operating Systems Lecture 09: Threads (Chapter 4)
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.
Operating System 4 THREADS, SMP AND MICROKERNELS
Chapter 4 Threads, SMP, and Microkernels Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design.
1 Threads, SMP, and Microkernels Chapter 4. 2 Focus and Subtopics Focus: More advanced concepts related to process management : Resource ownership vs.
Processes and Threads Processes have two characteristics: – Resource ownership - process includes a virtual address space to hold the process image – Scheduling/execution.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Threads G.Anuradha (Reference : William Stallings)
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
1 Threads, SMP, and Microkernels Chapter 4. 2 Process Resource ownership: process includes a virtual address space to hold the process image (fig 3.16)
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.
1 Lecture 4: Threads Advanced Operating System Fall 2010.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
Operating System 4 THREADS, SMP AND MICROKERNELS.
Department of Computer Science and Software Engineering
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
Module 2.0: Threads.
Processes & Threads Introduction to Operating Systems: Module 5.
CSC 322 Operating Systems Concepts Lecture - 7: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Threads-Process Interaction. CONTENTS  Threads  Process interaction.
Threads. Thread A basic unit of CPU utilization. An Abstract data type representing an independent flow of control within a process A traditional (or.
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
1 Threads, SMP, and Microkernels Chapter 4. 2 Process Resource ownership - process includes a virtual address space to hold the process image Scheduling/execution-
Threads, SMP, and Microkernels Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
Processes Chapter 3. Processes in Distributed Systems Processes and threads –Introduction to threads –Distinction between threads and processes Threads.
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Processes and threads.
CS 6560: Operating Systems Design
Chapter 4 Threads.
Threads & multithreading
Threads, SMP, and Microkernels
Threads Chapter 4.
Lecture 4- Threads, SMP, and Microkernels
Operating System 4 THREADS, SMP AND MICROKERNELS
Threads and Concurrency
Threads Chapter 4.
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads

© Janice Regan, CMPT 300, May What is a process?  A process (active entity) is a program (static entity) in execution, and includes  An address space, an area in memory (memory location x to y) containing List of instructions Any required data The stack for the program  Registers  Other information (includes resources used, status of I/O )

© Janice Regan, CMPT 300, May Multiprocessing shares memory  When multiple processes are sharing the CPU the memory is shared between them  Each process can only access it’s own area of memory (program image, stack, …)  Pointers (stored in registers) give the base address and the length or end address. (shown here in # of words) Process C Process A Process B Dispatcher

© Janice Regan, CMPT 300, May Processes/Threads  A process groups all resources needed to execute a program  Our OS switches between processes and otherwise manages processes, this incurs a significant overhead for each process  Processes are useful for encapsulation and security.  They cannot access each others memory space  The resources they use are protected from other processes  This can be useful when processes are competing for resources (different owners who want optimal turnaround)  However, sometimes we want programs to be able to interact and cooperate and even share memory space when they run, (controlled by the same user, cooperating with each other). The process model does not allow this.

Path of Execution  A path of execution can be thought of as a list of instructions in order they are actually executed when a particular process executes.  Two different processes running the same program can have different threads of execution  A thread follows a particular path of execution through a program © Janice Regan, CMPT 300, May

5 What is a thread?  A “lightweight process”  Each thread has its own program image  Program counter, registers  Stack (path of execution), automatic data  State  Each thread in a particular process shares  Program text, Global variables  Files and other communication connections  State (different from process state)

What is shared 6 REGISTERS STACK Includes Automatic variables User address space Code Global variables Heap Static variables © Janice Regan, CMPT 300, May 2007 Process control block Process Thread 1Thread 2 Thread 3 Thread ocntrol block Thread control block REGSITERS STACK Includes Automatic variables REGISTERS STACK Includes Automatic variables Shared

© Janice Regan, CMPT 300, May Resource ownership  Separate the two main characteristics of a process, Resource ownership/management and Scheduling/execution.  Resource ownership: Address space holding a process image (program, data, state stored in process control block), main memory, connections to I/O devices, other hardware devices and other processes (also stored in process control block)  Owned by a process  Shared by a thread

© Janice Regan, CMPT 300, May Scheduling, execution  Separate the two main characteristics of a process, Resource ownership/management and Scheduling/execution.  Scheduling/dispatching/execution  Program counter, registers, stack, locally declared (automatic variables) variables owned by each thread within a process  Follows an execution path through the program image (trace through the code)  Execution path is unique to each process, and to each thread within a process

© Janice Regan, CMPT 300, May Multithreading: advantages Multithreading: The ability of the operating system to support more than one concurrent thread of execution within any given process  Increases efficiency over using multiple processes because  Fewer process context switches (using shared image)  Less thread context to save (program counter, local thread specific variables, thread state)  Takes less time to create/terminate a thread than a process (less to copy)

© Janice Regan, CMPT 300, May Multithreading: disadvantages  Sharing increases possibility of synchronization problems  Each thread can modify all variables in the memory image (but cannot control order modifications are done)  All files and other communication connections are shared. Reading in one thread, moves read pointer  Suspending a process means suspending several threads to allow the image to be swapped  If you fork a multithreaded process, what happens?

© Janice Regan, CMPT 300, May When threads are useful  Multiprocessor systems (simultaneous execution of different threads on multiple processors)  Foreground (display and input) and background work (using a word processor, spreadsheet …)  Asynchronous processing (timed save in a word processor, I/0 processing in background …)  Efficiency advantages of multiprocessing within on process (one thread blocked, run another)  Modularization: different threads in the process taking care of different modularized tasks (I/0 from/to various sources/destinations …)  Good examples in your text

© Janice Regan, CMPT 300, May Implementing threads  Two main approaches to implementation  User level threads (POSIX Pthreads) Thread management is the responsibility of the user and occurs completely in user mode. Thread scheduling is user controlled, you can choose the best way to schedule your own threads Blocking system calls or page faults block all threads (not just one) must wrapper the call so thread is not blocked if device is busy Cannot take advantages of multiprocessors  Kernel level threads (Windows, newer Linux releases) All thread management is done by the kernel Requires mode switch to kernel to move between threads API to the kernel thread facility provides users access through system calls O/S dependent

Process and Thread States  We have discussed the possible states of a process (running, blocked, ready, … ).  Each thread in a process can have its own state. The states have the same names but we must remember that they refer to only one thread  Consider a process that is in the running state. If that process has multiple threads only one of its threads will be in the running state, the others will be in blocked state or in ready state © Janice Regan, CMPT 300, May

User Level Threads © Janice Regan, CMPT 300, May User Space Kernel Space T1 P T2T3 P Thread Library

© Janice Regan, CMPT 300, May User level threads  The application does the thread management in user space with the help of a thread library  The kernel is not aware that threads are being used  An application begins as a process with a single thread.  The application keeps its own thread table to keep track of all threads it has created using the library routine thread_create()  The application must use thread_yield() to ask the scheduler (library) to switch to the another thread  No context switch or interrupt are needed (fast)  Cannot let the OS scheduler manage sharing between threads because the OS (kernel level) does not know threads exist

Some possible situations  The process is in running state, one thread yields to another thread in the same process  The process is in ready state, one of the process’s threads is in running state  The process is blocked in blocked state, one of the process’s threads is in running state. © Janice Regan, CMPT 300, May

user –level thread multiprocessing  The threaded application starts as a process containing a single thread. The process is in the running state.  The application uses the user-level thread library to create additional threads  When thread A begins to run it will enter the running state. Thread A will continue until it calls thread_yield()  When thread_yield() executes it will place Thread A into the ready state, place thread B into the running state (formerly in waiting state) and start running thread B  The process is still in the running state © Janice Regan, CMPT 300, May

User level threads: timesharing  The threaded application starts as a process containing a single thread. The process is in the running state.  The application uses the user-level thread library to create additional threads  Thread A (in process 1) has been executing for one process timesharing quantum and the timer interrupt has transferred control to the kernel.  The kernel does not know about the threads, it saves the context of process 1 and moves process 1 to ready state, and starts executing process 2  Thread A is still in running state even though process 1 is in ready state © Janice Regan, CMPT 300, May

User level threads: blocking  The threaded application starts as a process containing a single thread. The process is in the running state.  The application uses the user-level thread library to create additional threads  When thread A begins to run it will enter the running state. Thread A makes an I/O system call.  Control is transferred to the kernel which blocks the process  Remember the kernel does not know about the threads  Thread A is still in state running, even though the process is in state blocked  It is not possible to run any part (any thread) of the blocked process © Janice Regan, CMPT 300, May

User level threads: advantages  The process does not have to switch to kernel mode to do thread management  Scheduling is implemented by the application (using the thread library) so scheduling can be application specific  User level thread libraries can be portable (can run on more than one OS) © Janice Regan, CMPT 300, May

User level: disadvantages  When one thread makes a system call which causes it to be blocked, the entire process is blocked (use jacketing to mitigate)  A multithreaded application does not benefit from multiprocessing or time sharing. (these occur on a per process basis)  When a multithreaded process calls fork() should some or all threads be duplicated?  Not a simple question © Janice Regan, CMPT 300, May

Jacketing  Convert blocking system calls to non-blocking system calls  Write a jacket routine that checks to see if the blocking devise is presently busy  If the devise is busy and would block control is passed to a different thread. Otherwise execute the request  When control returns from the other thread repeat the previous step  Must have a system call to check if a blocking device is busy (select) © Janice Regan, CMPT 300, May

Kernel-Level Threads © Janice Regan, CMPT 300, May User Space Kernel Space T1 P T2T3 P

Kernel-level threads  Kernel maintains process control blocks describing each process and thread control blocks describing each thread  Threads from the same process can be scheduled independently. Scheduling can be done for threads rather than just for processes.  If one thread is blocked, the other threads in the process are not blocked  Switching threads requires a system call and is therefore less efficient © Janice Regan, CMPT 300, May

25 Hello world for threads  Process to be executed by each thread that is created. When function returns thread terminates void *hola(void * arg) { int myid=*(int *) arg; printf("Hello, world, I'm %d\n",myid); return arg; } Author: Mark Hays

Creating Threads pthread_t threads[NTHREADS]; /* holds thread info */ int ids[NTHREADS]; /* holds thread args */ for (worker=0; worker<NTHREADS; worker++) { ids[worker]=worker; if ( errcode = pthread_create (&threads[worker], NULL, /* default thread attributes */ hola, /* start routine */ &ids[worker])) { errexit(errcode,"pthread_create"); } 26 Author: Mark Hays

Waiting for threads to terminate for (worker=0; worker<NTHREADS; worker++) { /* wait for thread to terminate */ if (errcode=pthread_join(threads[worker],(void *) &status)) { errexit(errcode,"pthread_join"); } /* check thread's exit status and release its resources */ if (*status != worker) { fprintf(stderr,"thread %d terminated abnormally\n",worker); exit(1); } 27 Author: Mark Hays