Chapter 4 Threads Chapter 4 Threads n Overview






















































- Slides: 54

Chapter 4: Threads

Chapter 4: Threads n Overview n Multithreading Models n Threading Issues n Pthreads n Windows XP Threads n Linux Threads n Java Threads Operating System Concepts 4. 2 Silberschatz, Galvin and Gagne © 2005

Process and Thread Reationship Operating System Concepts 4. 3 Silberschatz, Galvin and Gagne © 2005

What is Thread ? n A basic unit of CPU utilization. It comprises a thread ID, a program counter, a register set, and a stack. n It is a single sequential flow of control within a program n It shares with other threads belonging to the same process its code section, data section, and other OS resources, such as open files and signals n A traditional (or heavyweight) process has a single thread of control Operating System Concepts 4. 4 Silberschatz, Galvin and Gagne © 2005

n If a process has multiple threads of control, it can perform more than one task at a time. n Threads are a way for a program to split itself into two or more simultaneously running tasks. That is the real excitement surrounding threads Operating System Concepts 4. 5 Silberschatz, Galvin and Gagne © 2005

Why Threads? n A process includes many things: � l An address space (defining all the code and data pages) � l OS descriptors of resources allocated (e. g. , open files) � l Execution state (PC, SP, regs, etc). n Key idea: l separate the concept of a process (address space, OS resources, Execution state) l … from that of a minimal “thread of control” (execution state: stack, stack pointer, program counter, registers) • l Threads are more lightweight, so much faster to create and switch between than processes 6 Operating System Concepts 4. 6 Silberschatz, Galvin and Gagne © 2005

Process vs Thread n Processes do not share resources very well l Why ? n Process context switching cost is very high l Why ? n Communicating between processes is costly l Why ? Operating System Concepts 4. 7 Silberschatz, Galvin and Gagne © 2005

Single and Multithreaded Processes Operating System Concepts 4. 8 Silberschatz, Galvin and Gagne © 2005

Thread Examples l A word processor may have a thread for displaying graphics, another thread for responding to keystrokes from the user, and a third thread for performing spelling and grammar checking in the background l War game software has soldiers, heroes, farmers, enemy, and kings l Web server can be accessed by more > 1000 users l Open web browser tab l Etc. . Operating System Concepts 4. 9 Silberschatz, Galvin and Gagne © 2005

Benefits n Responsiveness n Resource Sharing n Economy n Utilization of MP Architectures Multithread Operating System Concepts 4. 10 Silberschatz, Galvin and Gagne © 2005

Per Thread State n Each Thread has a Thread Control Block (TCB) l Execution State: CPU registers, program counter, pointer to stack l Scheduling info: State (more later), priority, CPU time l Accounting Info l Various Pointers (for implementing scheduling queues) l Pointer to enclosing process (PCB) l Etc Operating System Concepts 4. 11 Silberschatz, Galvin and Gagne © 2005

Operating System Concepts 4. 12 Silberschatz, Galvin and Gagne © 2005

Threads Sharing Ø Resource sharing. Changes made by one thread to shared system resources will be seen by all other threads. - Ø Memory Sharing (Same address, same data). Two pointers having the same value, exist in the same virtual memory and point to the same data. - Ø File open or close at the process level. No copy is needed! Synchronization required. As resources and memory sharing is possible, explicit synchronization is required by the programmer Operating System Concepts 4. 13 1 3 Silberschatz, Galvin and Gagne © 2005

Threads & Process n Suspending a process involves suspending all threads of the process since all threads share the same address space n Termination of a process, terminates all threads within the process Operating System Concepts 4. 14 Silberschatz, Galvin and Gagne © 2005

USER AND KERNEL THREAD Operating System Concepts 4. 15 Silberschatz, Galvin and Gagne © 2005

User Level and Kernel Level Thread Operating System Concepts 4. 16 Silberschatz, Galvin and Gagne © 2005

User Threads n All thread management is done by the application n The kernel is not aware of the existence of threads n Thread switching does not require kernel mode privileges n Scheduling is application specific n Created from three primary thread libraries: l POSIX Pthreads l Win 32 threads l Java threads Operating System Concepts 4. 17 Silberschatz, Galvin and Gagne © 2005

n A user-level library multiplex user threads on top of LWPs and provides facilities for inter-thread scheduling, context switching, and synchronization without involving the kernel. Operating System Concepts 4. 18 Silberschatz, Galvin and Gagne © 2005

Advantages & Disadvantages n Advantages: l User-level threads does not require modification to operating systems. l Thread switching does not involve the kernel -- no mode switching l Scheduling can be application specific -- choose the best algorithm. l User-level threads can run on any OS -- Only needs a thread library n Disadvantages: l Most system calls are blocking and the kernel blocks processes -- So all threads within the process will be blocked l The kernel can only assign processes to processors -- Two threads within the same process cannot run simultaneously on two processors Operating System Concepts 4. 19 Silberschatz, Galvin and Gagne © 2005

Kernel Threads n Kernel threads may not be as heavy weight as processes, but they still suffer from performance problems: l All thread management done by Kernel l Any thread operation still requires a system call. l Kernel threads may be overly general 4 to support needs of different users, languages, etc. l The kernel doesn’t trust the user 4 there must be lots of checking on kernel calls n Examples l Windows XP/2000 l Solaris l Linux l Tru 64 UNIX l Mac OS X Operating System Concepts 4. 20 Silberschatz, Galvin and Gagne © 2005

Advantages & Disadvantages n Advantages: l Scheduler may need more time for a process having large number of threads l Kernel-level threads are especially good for applications that frequently block. n Disadvantages: l The kernel-level threads are slow and inefficient l Since kernel must manage and schedule threads as well as processes, there is significant overhead and increased in kernel complexity. Operating System Concepts 4. 21 Silberschatz, Galvin and Gagne © 2005

Operating System Concepts 4. 22 Silberschatz, Galvin and Gagne © 2005

n Linux kernel thread API functions usually called from linux/kthread. h l kthread_create(threadfn, data, namefmt, arg. . . ) l kthread_run(threadfn, data, namefmt, . . . ) Operating System Concepts 4. 23 Silberschatz, Galvin and Gagne © 2005

MULTITHREADING MODELS Operating System Concepts 4. 24 Silberschatz, Galvin and Gagne © 2005

Many-to-One n Many user-level threads mapped to single kernel thread n Examples: l Solaris Green Threads l GNU Portable Threads Operating System Concepts 4. 25 Silberschatz, Galvin and Gagne © 2005

Many-to-One Model Operating System Concepts 4. 26 Silberschatz, Galvin and Gagne © 2005

One-to-One n Each user-level thread maps to kernel thread n Examples l Windows NT/XP/2000 l Linux l Solaris 9 and later Operating System Concepts 4. 27 Silberschatz, Galvin and Gagne © 2005

One-to-one Model Operating System Concepts 4. 28 Silberschatz, Galvin and Gagne © 2005

Many-to-Many Model n Allows many user level threads to be mapped to many kernel threads n Allows the operating system to create a sufficient number of kernel threads n Solaris prior to version 9 n Windows NT/2000 with the Thread. Fiber package Operating System Concepts 4. 29 Silberschatz, Galvin and Gagne © 2005

Many-to-Many Model Operating System Concepts 4. 30 Silberschatz, Galvin and Gagne © 2005

Two-level Model n Similar to M: M, except that it allows a user thread to be bound to kernel thread n Examples l IRIX l HP-UX l Tru 64 UNIX l Solaris 8 and earlier Operating System Concepts 4. 31 Silberschatz, Galvin and Gagne © 2005

Two-level Model Operating System Concepts 4. 32 Silberschatz, Galvin and Gagne © 2005

Thread libraries n Thread libraries provide programmers with an API for creating and managing threads. n Thread libraries may be implemented either in user space or in kernel space. The former involves API functions implemented solely within user space, with no kernel support. The latter involves system calls, and requires a kernel with thread library support. n There are three main thread libraries in use today: l POSIX Pthreads l Win 32 threads l Java threads. n The following sections will demonstrate the use of threads in all three systems for calculating the sum of integers from 0 to N in a separate thread, and storing the result in a variable "sum". Operating System Concepts 4. 33 Silberschatz, Galvin and Gagne © 2005

Pthreads n A POSIX standard (IEEE 1003. 1 c) API for thread creation and synchronization n API specifies behavior of the thread library, implementation is up to development of the library n Common in UNIX operating systems (Solaris, Linux, Mac OS X) Operating System Concepts 4. 34 Silberschatz, Galvin and Gagne © 2005

n Create a thread l pthread_t t; pthread_create(&t, NULL, func, arg) n Exit a thread l pthread_exit n Join two threads l void *ret_val; pthread_join(t, &ret_val); Operating System Concepts 4. 35 Silberschatz, Galvin and Gagne © 2005

#include <stdio. h> /* Wait till threads are complete before main #include <stdlib. h> continues. Unless we */ #include <pthread. h> /* wait we run the risk of executing an exit which will terminate */ void *print_message_function( void *ptr ); /* the process and all threads before threads have completed. */ pthread_join( thread 1, NULL); main() pthread_join( thread 2, NULL); { pthread_t thread 1, thread 2; printf("Thread 1 returns: %dn", iret 1); char *message 1 = "Thread 1"; printf("Thread 2 returns: %dn", iret 2); char *message 2 = "Thread 2"; exit(0); int iret 1, iret 2; //main } /* Create independent threads each of which will execute function */ void *print_message_function( void *ptr ) iret 1 = pthread_create( &thread 1, NULL, { print_message_function, (void*) char *message; message 1); message = (char *) ptr; iret 2 = pthread_create( &thread 2, NULL, printf("%s n", message); print_message_function, (void*) } message 2); Operating System Concepts 4. 36 Silberschatz, Galvin and Gagne © 2005

n Compile: l C compiler: cc -lpthread 1. c or l C++ compiler: g++ -lpthread 1. c n Run: . /a. out n Results: Thread 1 Thread 2 Thread 1 returns: 0 Thread 2 returns: 0 Operating System Concepts 4. 37 Silberschatz, Galvin and Gagne © 2005

Comparison between Win 32 thread and linux pthread Operating System Concepts 4. 38 Silberschatz, Galvin and Gagne © 2005

THREAD BASED ON : OPERATING SYSTEM Operating System Concepts 4. 39 Silberschatz, Galvin and Gagne © 2005

Windows XP Threads n Implements the one-to-one mapping n Each thread contains l A thread id l Register set l Separate user and kernel stacks l Private data storage area n The register set, stacks, and private storage area are known as the context of the threads n The primary data structures of a thread include: l ETHREAD (executive thread block) l KTHREAD (kernel thread block) l TEB (thread environment block) Operating System Concepts 4. 40 Silberschatz, Galvin and Gagne © 2005

Operating System Concepts 4. 41 Silberschatz, Galvin and Gagne © 2005

Linux Threads n Linux refers to them as tasks rather than threads n Thread creation is done through clone() system call n clone() allows a child task to share the address space of the parent task (process) Operating System Concepts 4. 42 Silberschatz, Galvin and Gagne © 2005

#include <malloc. h> if (stack == 0) { #include <sys/types. h> perror("malloc: could not allocate stack"); #include <sys/wait. h> exit(1); } #include <signal. h> printf("Creating child threadn"); #include <sched. h> #include <stdio. h> // Call the clone system call to create the child thread #include <fcntl. h> pid = clone(&thread. Function, (char*) stack + STACK, // 64 k. B stack SIGCHLD | CLONE_FS | CLONE_FILES | #define STACK 1024*64 CLONE_SIGHAND | CLONE_VM, // The child thread will execute this function (void*)fd); int thread. Function( void* argument ) { printf( "child thread enteringn" ); if (pid == -1) { close((int*)argument); perror("clone"); printf( "child thread exitingn" ); exit(2); } return 0; } // Wait for the child thread to exit int main() pid = waitpid(pid, 0, 0); { if (pid == -1) { perror("waitpid"); void* stack; exit(3); } pid_t pid; int fd; // Attempt to write to file should fail, since our thread has closed the fil fd = open("/dev/null", O_RDWR); if (write(fd, "c", 1) < 0) { if (fd < 0) { printf("Parent: t child closed our file descriptorn"); perror("/dev/null"); } exit(1); } // Free the stack // Allocate the stack free(stack); return 0; stack = malloc(STACK); } Operating System Concepts 4. 43 Silberschatz, Galvin and Gagne © 2005

n gcc demo. c n. /a. out n Result : Creating child thread entering child thread exiting Parent: child closed our file descriptor n int clone (int (*fn) (void *), void *child_stack, int flags, void *arg); l int (*fn) (void *), is the thread function to be executed once a thread starts. l void *child_stack , is a pointer to a stack memory for the child process. l flags, is the most critical. It allows you to choose the resources you want to share with the newly created process l void *arg is the argument to the thread function (thread. Function), and is a file descriprto Operating System Concepts 4. 44 Silberschatz, Galvin and Gagne © 2005

Java Threads n Java threads are managed by the JVM n Java threads may be created by: l Extending Thread class l Implementing the Runnable interface Operating System Concepts 4. 45 Silberschatz, Galvin and Gagne © 2005

Java Thread States Operating System Concepts 4. 46 Silberschatz, Galvin and Gagne © 2005

Tugas 1. Buat program sederhana untuk membangkitkan thread dengan perintah hello word 1. Dengan windows (2 kel) 2. Degan Linux (2 kel) 3. Dengan java (2 kel) Operating System Concepts 4. 47 Silberschatz, Galvin and Gagne © 2005

End of Chapter 4

Operating System Concepts 4. 49 Silberschatz, Galvin and Gagne © 2005

Operating System Concepts 4. 50 Silberschatz, Galvin and Gagne © 2005

Scheduler Activations n Both M: M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application n Scheduler activations, done from Kernel side, will provide upcalls - a communication mechanism from the kernel to the thread library n This communication allows an application to maintain the correct number kernel threads (a) System call from user thread(1) is blocked by kernel (2) (b) New kernel thread is created (3). Kernel thread perform upcall to thread library (4). Thread library creates another user thread to do system call (5) (c) Sistem call 1 is unblocked (1), preempt the current kernel thread (2), do upcall to thread library (3) Preempt the new user thread(4) Resume the last blocked user thread. Operating System Concepts 4. 51 Silberschatz, Galvin and Gagne © 2005

#include <linux/module. h> #include <linux/kernel. h> static struct task_struct *thread_st; // Function executed by kernel thread static int thread_fn(void *unused) { while (1) { printk(KERN_INFO "Thread Runningn"); ssleep(5); } printk(KERN_INFO "Thread Stoppingn"); do_exit(0); return 0; } Operating System Concepts 4. 52 Silberschatz, Galvin and Gagne © 2005

// Module Initialization static int __init_thread(void) { printk(KERN_INFO "Creating Threadn"); //Create the kernel thread with name 'mythread' thread_st = kthread_create(thread_fn, NULL, "mythread"); if (thread_st) printk("Thread Created successfullyn"); else printk(KERN_INFO "Thread creation failedn"); return 0; } // Module Exit static void __exit cleanup_thread(void) { printk("Cleaning Upn"); } Operating System Concepts 4. 53 Silberschatz, Galvin and Gagne © 2005

Lightweight Processes n LWP is a kernel-supported user thread. n It belongs to a user process. n Independently scheduled. n Share the address space and other resources of the process. n LWP should be synchronized on shared data. n Blocking an LWP is expensive. Operating System Concepts 4. 54 Silberschatz, Galvin and Gagne © 2005