Operating Systems Lecture 13.

Slides:



Advertisements
Similar presentations
Threads Relation to processes Threads exist as subsets of processes Threads share memory and state information within a process Switching between threads.
Advertisements

Copyright ©: Nahrstedt, Angrave, Abdelzaher1 pThreads.
Threads. Readings r Silberschatz et al : Chapter 4.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 TY, Sept 2011.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
Pthreads Operating Systems Hebrew University of Jerusalem Spring 2004.
B.Ramamurthy1 POSIX Thread Programming 2/14/97 B.Ramamurthy.
Unix Threads operating systems. User Thread Packages pthread package mach c-threads Sun Solaris3 UI threads Kernel Threads Windows NT, XP operating systems.
Jonathan Walpole Computer Science Portland State University
Process Concept An operating system executes a variety of programs
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 4: Threads.
Operating Systems Chapter 5 Threads. Benefits Responsiveness Resource Sharing Economy Utilization of MP Architectures.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 T Yang, Sept 2012.
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.
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
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. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
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.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Threads A thread is an alternative model of program execution
POSIX Threads Loren Stroup EEL 6897 Software Development for R-T Engineering Systems.
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
CISC2200 Threads Fall 09. Process  We learn the concept of process  A program in execution  A process owns some resources  A process executes a program.
Thread Programming 김 도 형김 도 형. 2 Table of Contents  What is a Threads?  Designing Threaded Programs  Synchronizing Threads  Managing Threads.
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
Realizing Concurrency using the thread model
Threads in C Caryl Rahn.
CS399 New Beginnings Jonathan Walpole.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Chapter 4: Threads.
PTHREADS These notes are from LLNL Pthreads Tutorial
Chapter 4 Threads.
Threads & multithreading
Realizing Concurrency using Posix Threads (pthreads)
Jonathan Walpole Computer Science Portland State University
Realizing Concurrency using the thread model
Operating Systems Lecture 14.
Operating Systems Lecture 12.
Realizing Concurrency using the thread model
CS510 Operating System Foundations
Jonathan Walpole Computer Science Portland State University
Operating System Concepts
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
Programming with Shared Memory
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 Systems Lecture 13

© Copyright Virtual University of Pakistan Agenda for Today Review of previous lecture Single- and multi-threaded process Thread models User and kernel threads Pthreads library with examples Recap of lecture 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Review of Lecture 12 Process management commands: fg, bg, jobs, ^Z, ^C, kill Issues with processes Thread concept Similarities and differences between processes and threads Pros and cons of threads 16 November 2018 © Copyright Virtual University of Pakistan

Single-Threaded Process main() { … f1(…); f2(…); } f1(…) { … } f2(…) Thread f1 f2 Process Terminated 16 November 2018 © Copyright Virtual University of Pakistan

Multi-Threaded Process main() { … thread(t1,f1); thread(t2,f2); } f1(…) { … } f2(…) Process Address Space main t1 t2 PC PC PC 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan User Threads Thread management done by user-level threads libraries Kernel not aware of threads CPU not interrupted during thread switching A system call by a thread blocks the whole process Fair scheduling: P1 has one thread and P2 has 100 threads 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan User Threads Examples POSIX Pthreads Mach C-threads Solaris 2 threads 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Kernel Threads Thread management done by kernel Kernel aware of threads CPU switched during context switching A system call does not block the whole process Fair scheduling: P1 has one thread and P2 has 100 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Kernel Threads Examples Windows NT/2000 Solaris 2 Linux 16 November 2018 © Copyright Virtual University of Pakistan

Multithreading Models Support for both user and kernel threads Many-to-One: Many user threads per kernel thread; process blocks when a thread makes a system call Solaris Green threads Pthreads 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Many-to-One Model User–level Threads Kernel–level Thread 16 November 2018 © Copyright Virtual University of Pakistan

Multithreading Models One-to-One: One user thread per kernel thread; process does not block when a thread makes a system call Overhead for creating a kernel thread per user thread True concurrency achieved Windows NT/2000, OS/2 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan One-to-One Model P1 P2 User–level Threads Kernel–level Threads 16 November 2018 © Copyright Virtual University of Pakistan

Multithreading Models Many-to-Many: Multiple user threads multiplexed over a smaller or equal number of kernel threads True concurrency not achieved because kernel can only schedule one thread at a time Kernel can schedule another thread when a user thread makes a blocking system call Solaris 2, HP-UX 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Many-to-Many Model P1 P2 P3 User–level Threads Kernel–level Threads 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Solaris 2 Threads Model Solaris 2: threads, light-weight processes (LWPs), and processes At least one LWP per process to allow a user thread to talk to a kernel thread User level threads switched and scheduled among LWPs without kernel’s knowledge 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Solaris 2 Threads Model One kernel thread per LWP; some kernel threads have no LWP (e.g., threads for clock interrupt handler and scheduling) 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Solaris 2 Threads Model 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Pthreads A POSIX standard (IEEE 1003.1c) API for thread creation, termination, and synchronization. API specifies the behavior of the thread library, implementation is up to developers of the library. Common in UNIX operating systems. 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Creating a Thread int pthread_create (pthread_t *threadp, const pthread_attr_t *attr, void* (*routine)(void *), arg *arg); 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Creating a Thread Where: threadp The thread we are trying to create—thread ID (TID) attr Used to modify the thread attributes (stack size, stack address, detached, joinable, priority, etc.) routine The thread function arg Any argument we want to pass to the thread function. This does not have to be a simple native type, it can be a ‘struct’ of whatever we want to pass in. 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Error Handling pthread_create() fails and returns the corresponding value if any of the following conditions is detected: EAGAIN The system-imposed limit on the total number of threads in a process has been exceeded or some system resource has been exceeded (for example, too many LWPs were created). 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Error Handling EINVAL The value specified by attr is invalid. ENOMEM Not enough memory was available to create the new thread. Error handling: #include <errno.h> Error handling code 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Joining a Thread Waiting for a thread int pthread_join(pthread_t aThread, void **statusp); ‘statusp’ get return value of pthread_exit 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Joining a Thread Cannot join with a detached thread Can only join with thread’s in the same process address space Multiple threads can join with one thread but only one returns successfully; others return with an error that no thread could be found with the given TID 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Terminating a Thread Main thread terminates Thread returns void pthread_exit(void *valuep) Returns value pointed to by ‘valuep’ to a joining thread, provided the exiting thread is not detached 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Example 1 #include <stdio.h> #include <stdlib.h> #include <pthread.h> /* Prototype for a function to be passed to our thread */ void* MyThreadFunc(void *arg); int main() { pthread_t aThread; /* Create a thread and have it run the MyThreadFunction */ pthread_create(&aThread, NULL, MyThreadFunc, NULL); /* Parent waits for the aThread thread to exit */ pthread_join(aThread, NULL); printf ("Exiting the main function.\n"); return 0; } 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Example 1 void* MyThreadFunc(void* arg) { printf ("Hello, world! ... The threaded version.\n"); return NULL; } $ gcc hello.c –o hello –lpthread –D_REENTRANT $ hello Hello, world! ... The threaded version. Exiting the main function. $ 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Example 2 #include <pthread.h> #include <stdio.h> #define NUM_THREADS 5 void *PrintHello(void *threadid) { printf("\n%d: Hello World!\n", threadid); pthread_exit(NULL); } 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Example 2 int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc, t; for (t=0; t < NUM_THREADS; t++) { printf("Creating thread %d\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); if (rc) { printf("ERROR; return code is %d\n", rc); exit(-1); } pthread_exit(NULL); 16 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Recap of Lecture Single- and multi-threaded processes User and kernel threads Thread models Pthreads library Sample code Pthread calls: pthread_create, pthread_join, pthread_exit 16 November 2018 © Copyright Virtual University of Pakistan