Download presentation
Presentation is loading. Please wait.
1
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
2
Single and Multithreaded Processes
Operating System Concepts
3
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
4
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
5
Multithreading Models (Cont.)
Many-to-one Many-to-many One-to-one Operating System Concepts
6
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
7
Operating System Concepts
Solaris 2 Threads Operating System Concepts
8
Operating System Concepts
Pthreads Posix c 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
9
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
10
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
11
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
12
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
13
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
14
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.