Operating System Concepts

Slides:



Advertisements
Similar presentations
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Advertisements

Threads Relation to processes Threads exist as subsets of processes Threads share memory and state information within a process Switching between threads.
Threads. Readings r Silberschatz et al : Chapter 4.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Threads By Dr. Yingwu Zhu. Review Multithreading Models Many-to-one One-to-one Many-to-many.
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading.
CS238 Lecture 5 Threads Dr. Alan R. Davis. Threads Definitions Benefits User and Kernel Threads Multithreading Models Solaris 2 Threads Java Threads.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Pthreads Operating Systems Hebrew University of Jerusalem Spring 2004.
Process Concept An operating system executes a variety of programs
02/02/2004CSCI 315 Operating Systems Design1 Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
Chapter 4 Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Thread. A basic unit of CPU utilization. It comprises a thread ID, a program counter, a register set, and a stack. It is a single sequential flow of control.
Cosc 4740 Chapter 4 & 5 Threads & Scheduling. Motivation Threads run within application (process) Multiple tasks with the application can be implemented.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Operating Systems Chapter 2
Operating Systems CMPSC 473 Multi-threading models Tutorial on pthreads Lecture 10: September Instructor: Bhuvan Urgaonkar.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 4: Threads. 2 Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads.
Shan Gao Fall 2007 Department of Computer Science Georgia State University.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
1 OS Review Processes and Threads Chi Zhang
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Threads A thread is an alternative model of program execution
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Tutorial 4. In this tutorial session we’ll see Threads.
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
Realizing Concurrency using the thread model
CS 537 – Introduction to Operating Systems
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 4: Threads.
Realizing Concurrency using the thread model
Threads in C Caryl Rahn.
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Chapter 4: Threads.
Process Management Presented By Aditya Gupta Assistant Professor
Operating System Concepts
Chapter 5: Threads Overview Multithreading Models Threading Issues
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Realizing Concurrency using the thread model
Thread Programming.
Realizing Concurrency using the thread model
CS510 Operating System Foundations
Programming with Shared Memory
Jonathan Walpole Computer Science Portland State University
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Lecture 9: POSIX Threads Cont.
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Shared Memory Programming with Pthreads
Presentation transcript:

Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU utilization; it consists of: Stack space PCB in the kernel A thread shares with its peer threads its: Text/Code section Data section collectively known as a task. A traditional or heavyweight process is equal to a task with one thread Operating System Concepts

Single and Multithreaded Processes Operating System Concepts

Operating System Concepts Threads (Cont.) In a multiple threaded task, while one server thread is blocked and waiting, a second thread in the same task can run. applications that require sharing a common buffer (i.e., producer-consumer) benefit from thread utilization. Threads provide a mechanism that allows sequential processes to make blocking system calls while also achieving parallelism. Types of threads: Kernel-supported threads. User-level threads; supported above the kernel, via a set of library calls at the user level. Anderson's threads: Like Kernel-supported threads, but sends events that influence user-level scheduling to the task, rather than interpret these events on its own. Operating System Concepts

Multithreading Models Many-to-One Many user-level threads are mapped to a single kernel thread. Used on systems that do not support kernel threads. Example: Project Andrew of CMU. One-to-One Each user-level thread is mapped to a kernel thread. Examples: WIndows95, Windows98 and OS/2. 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. Hybrid approach, implements both user-level and kernel-supported threads. Examples: Solaris 2, Linux, WindowsNT, Windows2000. Operating System Concepts

Multithreading Models (Cont.) Many-to-one Many-to-many One-to-one Operating System Concepts

Threads Support in Solaris 2 Solaris 2 is a version of UNIX with a support of threads at the kernel and user levels. LWP – intermediate level between user-level threads and kernel-level threads. Resource needs of thread types: Thread of the kernel: Small data structure and a stack; thread switching does not require changing memory access information – relatively fast. LWP: PCB with register data, accounting and memory information; switching between LWPs is relatively slow. User-level thread: Only needs stack and program counter; no kernel involvement means fast switching. Kernel only sees the LWPs. Operating System Concepts

Operating System Concepts Solaris 2 Threads Operating System Concepts

Operating System Concepts Pthreads Posix 1003.1c defines a thread interface Called Pthreads (Posix Threads) Defines how threads should be created, managed, and destroyed Unix provides a pthreads library API to create and manage threads. No need to worry about the implementation details. Operating System Concepts

Operating System Concepts Creating Threads Prototype: int pthread_create(pthread_t *tid, const pthread_attr_t *tattr, void*(*start_routine)(void *), void *arg); tid: an unsigned long integer that indicates a threads id tattr: attributes of the thread – usually NULL start_routine: the name of the function the thread starts executing arg: the argument to be passed to the start routine – only one After this function has been executed, a new thread is created and is executing the function indicated by start_routine On success, pthread_create() returns 0. on error, it returns an error number. Operating System Concepts

Operating System Concepts Waiting for a Thread Prototype: int pthread_join(thread_t tid, void **status); tid: identification of the thread to wait for. status: the exit status of the terminating thread – can be NULL the thread that calls this function blocks its own execution until the thread indicated by tid terminates its execution finishes the function it started with. issues a pthread_exit() command. Operating System Concepts

Operating System Concepts Pthread Example #include <stdio.h> #include <pthread.h> void printMsg(char* msg) { printf(“%s\n”, msg); } int main(int argc, char** argv) { pthread_t thrdID; printf(“creating a new thread\n”); pthread_create(&thrdID, NULL, (void*)printMsg, argv[1]); printf(“created thread %d\n”, thrdID); pthread_join(thrdID, NULL); Operating System Concepts

Operating System Concepts Example (Cont.) thread 0 thread 1 create_thread() start of printMsg() end of printMsg() end of program Note: thread 0 is the function that contains main() – only one main() per program. Operating System Concepts

Operating System Concepts Exiting a Thread pthreads exist in user space and are seen by the kernel as a single process. if one thread issues and exit() system call, all the threads are terminated by the OS. if the main() function exits, all of the other threads are terminated as well. To have a thread exit, use pthread_exit() Prototype: void pthread_exit(void *status); status: the exit status of the thread is passed to the status variable in the pthread_join() function of the waiting thread. Operating System Concepts

Operating System Concepts Example Revisited #include <stdio.h> #include <pthread.h> void printMsg(char* msg) { int status = 0; printf(“%s\n”, msg); pthread_exit(&status); } int main(int argc, char** argv) { pthread_t thrdID; int *status = (int*)malloc(sizeof(int)); printf(“creating a new thread\n”); pthread_create(&thrdID, NULL, (void*)printMsg, argv[1]); printf(“created thread %d\n”, thrdID); pthread_join(thrdID, &status); printf(“Thread %d exited with status %d\n”, thrdID, *status); Operating System Concepts