Operating Systems Lecture 13 Agenda for Today n

  • Slides: 31
Download presentation
Operating Systems Lecture 13

Operating Systems Lecture 13

Agenda for Today n Review of previous lecture n Single- and multi-threaded process n

Agenda for Today n Review of previous lecture n Single- and multi-threaded process n Thread models n User and kernel threads n Pthreads library with examples n Recap of lecture

Review of Lecture 12 n Process management commands: fg, bg, jobs, ^Z, ^C, kill

Review of Lecture 12 n Process management commands: fg, bg, jobs, ^Z, ^C, kill n Issues with processes n Thread concept n Similarities and differences between processes and threads n Pros and cons of threads

Single-Threaded Process main() { … f 1(…); … f 2(…); … } Thread f

Single-Threaded Process main() { … f 1(…); … f 2(…); … } Thread f 1 f 2 f 1(…) { … } f 2(…) { … } Process Terminated

Multi-Threaded Process main() { … thread(t 1, f 1); … thread(t 2, f 2);

Multi-Threaded Process main() { … thread(t 1, f 1); … thread(t 2, f 2); … } f 1(…) { … } f 2(…) { … } Process Address Space main t 1 t 2 PC PC PC

User Threads § Thread management done by userlevel threads libraries § Kernel not aware

User Threads § Thread management done by userlevel 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: P 1 has one thread and P 2 has 100 threads

User Threads § Examples §POSIX Pthreads §Mach C-threads §Solaris 2 threads

User Threads § Examples §POSIX Pthreads §Mach C-threads §Solaris 2 threads

Kernel Threads § Thread management done by kernel § Kernel aware of threads §

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: P 1 has one thread and P 2 has 100

Kernel Threads § Examples §Windows NT/2000 §Solaris 2 §Linux

Kernel Threads § Examples §Windows NT/2000 §Solaris 2 §Linux

Multithreading Models § Support for both user and kernel threads § Many-to-One: Many user

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

Many-to-One Model User–level Threads Kernel–level Thread

Many-to-One Model User–level Threads Kernel–level Thread

Multithreading Models One-to-One: One user thread per kernel thread; process does not block when

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 §

One-to-One Model P 1 User–level Threads Kernel–level Threads P 2

One-to-One Model P 1 User–level Threads Kernel–level Threads P 2

Multithreading Models § § Many-to-Many: Multiple user threads multiplexed over a smaller or equal

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

Many-to-Many Model P 1 User–level Threads Kernel–level Threads P 2 P 3

Many-to-Many Model P 1 User–level Threads Kernel–level Threads P 2 P 3

Solaris 2 Threads Model § Solaris 2: threads, light-weight processes (LWPs), and processes §

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

Solaris 2 Threads Model § One kernel thread per LWP; some kernel threads have

Solaris 2 Threads Model § One kernel thread per LWP; some kernel threads have no LWP (e. g. , threads for clock interrupt handler and scheduling)

Solaris 2 Threads Model

Solaris 2 Threads Model

Pthreads A POSIX standard (IEEE 1003. 1 c) API for thread creation, termination, and

Pthreads A POSIX standard (IEEE 1003. 1 c) 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. §

Creating a Thread § int pthread_create (pthread_t *threadp, const pthread_attr_t *attr, void* (*routine)(void *),

Creating a Thread § int pthread_create (pthread_t *threadp, const pthread_attr_t *attr, void* (*routine)(void *), arg *arg);

Creating a Thread Where: threadp attr The thread we are trying to create—thread ID

Creating a Thread Where: threadp attr The thread we are trying to create—thread ID (TID) Used to modify the thread attributes (stack size, stack address, detached, joinable, priority, etc. ) routine arg The thread function 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.

Error Handling § pthread_create() fails and returns the corresponding value if any of the

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).

Error Handling EINVAL The value specified by attr is invalid. § ENOMEM Not enough

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 §

Joining a Thread Waiting for a thread § int pthread_join(pthread_t a. Thread, void **statusp);

Joining a Thread Waiting for a thread § int pthread_join(pthread_t a. Thread, void **statusp); § ‘statusp’ get return value of pthread_exit §

Joining a Thread Cannot join with a detached thread § Can only join with

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 §

Terminating a Thread § Main thread terminates § Thread returns § void pthread_exit(void *valuep)

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

Example 1 #include <stdio. h> #include <stdlib. h> #include <pthread. h> /* Prototype for

Example 1 #include <stdio. h> #include <stdlib. h> #include <pthread. h> /* Prototype for a function to be passed to our thread */ void* My. Thread. Func(void *arg); int main() { pthread_t a. Thread; /* Create a thread and have it run the My. Thread. Function */ pthread_create(&a. Thread, NULL, My. Thread. Func, NULL); /* Parent waits for the a. Thread to exit */ pthread_join(a. Thread, NULL); printf ("Exiting the main function. n"); return 0; }

Example 1 void* My. Thread. Func(void* arg) { printf ("Hello, world!. . . The

Example 1 void* My. Thread. Func(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. $

Example 2 #include <pthread. h> #include <stdio. h> #define NUM_THREADS 5 void *Print. Hello(void

Example 2 #include <pthread. h> #include <stdio. h> #define NUM_THREADS 5 void *Print. Hello(void *threadid) { printf("n%d: Hello World!n", threadid); pthread_exit(NULL); }

Example 2 int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc, t;

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 %dn", t); rc = pthread_create(&threads[t], NULL, Print. Hello, (void *)t); if (rc) { printf("ERROR; return code is %dn", rc); exit(-1); } } pthread_exit(NULL); }

Recap of Lecture n. Single- and multi-threaded processes n. User and kernel threads n.

Recap of Lecture n. Single- and multi-threaded processes n. User and kernel threads n. Thread models n. Pthreads library n. Sample code n. Pthread calls: pthread_create, pthread_join, pthread_exit