Operating Systems Practical Session 4Threads
Threads โข Executed within a process. โข Allow multiple independentexecutions under the same process (container). โข Possible states: running, ready, blocked, terminated. โข In most of todayโs operating systems, a process is created with at least one thread but may have more than one thread (multithreading).
Threads - Advantages โข Shareopen files, data structures, global variables, child processes, etc. โข Peer threads can communicate without using System calls. โข Threads are faster to create/terminate/switch than processes (have no resources attached). โข Parallelism which improves overall performance: โข A single core CPU and a substantial amount of computing and I/O โข Multiple cores
Threads - Disadvantages โข Share open files, data structures, global variables, child processes, etc. โข No protection between threads โ one can read/write/wipe out/corrupt the otherโs data. โข Sending some signals (such as SIGSTOP) to a process affects all threads running within it.
Threads vs. Processes(โclassicโ approach โ Linuxโs clone results in some ambiguity) Signal handlers must be shared among all threads of a multithreaded application; however, each thread must have its own mask of pending and blocked signals (POSIX 1003.1).
Threads - motivation โข Dispatcher thread: โข while (TRUE) { โข get_next_request(&buf); โข handoff_work(&buf); โข } Dispatcher โข Worker thread: โข while (TRUE) { โข wait_for_work(&buf); โข look_for_page_in_cache(&buf, &page); โข if (page_not_in_cache(&page)) โข read_page_from_disk(&buf, &page); โข return page(&page); โข } Workers Web page cache Webpage request Why are threads better in this case? Example from โModern Operating Systemsโ, 2nd Edition, pg. 88
Threads โ some known Issues โข Does the fork() command duplicate just the calling thread or all threads of the process? โข POSIX defines that only the calling thread is replicated in the child process. โข Solaris 10 definesย fork1()ย and forkall() which attempt to better define the relation between fork and threads, however, this is not POSIX compliant. โข Many issues arise from using fork in a multithreaded code. โข Unless calling exec immediately after fork, try to avoid it! โข Does the exec() command replace the entire process? โข The entire process is replaced including all its threads.
User-level and Kernel-level Threads Threads Library User space User space Kernel space Kernel space P P (a) Pure user-level (b) Pure kernel-level
User-level and Kernel-level Threads โข Threads โข Library User space Kernel space P P (c) Combined Source: Stallings, Operating Systems: Internals and design principles 7th ed.
User-level threads โข The kernel sees just the main thread of the process (all other threads that run within the processโ context are โinvisibleโ to the OS). โข The user application โ not the kernel โ is responsible for scheduling CPU time for its internal threads within the running time scheduled for it by the kernel.
User-level threads (contโd) โข User-level threads implement in user-level libraries, rather than via systems calls, so thread switching does not need to call operating system and to cause interrupt to the kernel. โข The kernelโs inability to distinguish between user level threads makes it difficult to design preemptive scheduling. โข If a thread makes a blocking system call, the entire process is blocked. โข Will only utilize a single CPU.
Kernel-level threads โข All threads are visible to the kernel. โข The kernel manages the threads. โข The kernel schedules each thread within the time-slice of each process. โข The user cannot define the scheduling policy. โข Context switching is slower for kernel threads than for user-level threads. โข Because the kernel is aware of the threads, in multiple CPU machines, each CPU can run a different thread of the same process, at the same time.
User-level vs. kernel-level threads
A tech. note on POSIX threads โข When the first Unix and POSIX functions were designed, it was assumed that there will be a single thread of execution. โข Hence, the need for reentrant functions. โข While this is supported by many standard functions, the compiler must be aware of the need for re-entrant functions: โข gccโD_REENTRANT โlpthread โฆ
Threads in POSIX (pthreads)
Threads in POSIX (pthreads) โ contโd
Hello World! When compiling a multi-threaded app: gccโD_REENTRANT โo myprogmyprog.c โlpthread #include <pthread.h> #include <stdio.h> void *printme() { printf("Hello World!\n"); return NULL; } void main() { pthread_ttcb; void *status; if (pthread_create(&tcb, NULL, printme, NULL) != 0) { perror("pthread_create"); exit(1); } if (pthread_join(tcb, &status) != 0) { perror("pthread_join"); exit(1); } } What can happen if we remove the join part?
Example A โ Version 1 void *printme(void *id) { int *i; i = (int *)id; printf("Hi. I'm thread %d\n", *i); return NULL; } void main() { int i, vals[4]; pthread_t tids[4]; void *retval; for (i = 0; i < 4; i++) { vals[i] = i; pthread_create(tids+i, NULL, printme, vals+i); } for (i = 0; i < 4; i++) { printf("Trying to join with tid%d\n", i); pthread_join(tids[i], &retval); printf("Joined with tid%d\n", i); } }
Example A โ Version 1possible output Trying to join with tid0 Hi. I'm thread 0 Hi. I'm thread 1 Hi. I'm thread 2 Hi. I'm thread 3 Joined with tid0 Trying to join with tid1 Joined with tid1 Trying to join with tid2 Joined with tid2 Trying to join with tid3 Joined with tid3
Example A โ Version 2 void *printme(void *id) { int*i; i = (int*)id; printf("Hi. I'm thread %d\n", *i); pthread_exit(NULL); } void main() { inti, vals[4]; pthread_ttids[4]; void *retval; for (i = 0; i < 4; i++) { vals[i] = i; pthread_create(tids+i, NULL, printme, vals+i); } for (i = 0; i < 4; i++) { printf("Trying to join with tid%d\n", i); pthread_join(tids[i], &retval); printf("Joined with tid%d\n", i); } pthread_exit(NULL); }
Example A โ Version 2possible output Trying to join with tid0 Hi. I'm thread 0 Hi. I'm thread 1 Hi. I'm thread 2 Hi. I'm thread 3 Joined with tid0 Trying to join with tid1 Joined with tid1 Trying to join with tid2 Joined with tid2 Trying to join with tid3 Joined with tid3
Example A โ Version 3 void *printme(void *id) { int*i; i = (int*)id; printf("Hi. I'm thread %d\n", *i); pthread_exit(NULL); } void main() { inti, vals[4]; pthread_ttids[4]; void *retval; for (i = 0; i < 4; i++) { vals[i] = i; pthread_create(tids+i, NULL, printme, vals+i); } pthread_exit(NULL); for (i = 0; i < 4; i++) { printf("Trying to join with tid%d\n", i); pthread_join(tids[i], &retval); printf("Joined with tid%d\n", i); } }
Example A โ Version 3output Hi. I'm thread 0 Hi. I'm thread 1 Hi. I'm thread 2 Hi. I'm thread 3 If the main thread calls pthread_exit(), the process will continue executing until the last thread terminates or the whole process is terminated
Example A โ Version 4 void *printme(void *id) { int *i = (int *)id; sleep(5); printf("Hi. I'm thread %d\n", *i); pthread_exit(NULL); } int main() { int i, vals[4]; pthread_t tids[4]; void *retval; for (i = 0; i < 4; i++) { vals[i] = i; pthread_create(tids+i, NULL, printme, vals+i); } return 0; }
Example A โ Version 4possible output No Output!
Example A โ Version 5 void *printme(void *id) { int*i; i = (int*)id; printf("Hi. I'm thread %d\n", *i); exit(0); } main() { inti, vals[4]; pthread_ttids[4]; void *retval; for (i = 0; i < 4; i++) { vals[i] = i; pthread_create(tids+i, NULL, printme, vals+i); } for (i = 0; i < 4; i++) { printf("Trying to join with tid%d\n", i); pthread_join(tids[i], &retval); printf("Joined with tid%d\n", i); } pthread_exit(NULL); }
Example A โ Version 5possible output Trying to join with tid0 Hi. I'm thread 0
Midterm โ 2006 ืืขืฅ ืชืืืืืื ืื ืงืืืงืื ืืืืฆื ืชืืืื.ืงืืืงืืg ืืฆืืืข ืขื ืงืืืงืืqืื"ืg ืืื ืืื ืฉื q, ืืืืืจ ืื g ืืฆืจ ืืช q. g q (ื) ืฉืจืืื ืืช ืขืฅ ืืชืืืืืื ืื ืืฆืจ ืข"ื ืืจืฆืช ืืงืื ืืื ืืฉืคืช C. (ืชื ื ืฉืืืช ืฉืจืืจืืชืืื ืืชืืืืืื ืื ืืฆืจืื.) 1. int x; 2. fork(); 3. x = fork(); 4. if(x != 0) โข fork(); โข 7. printf(โpid= %dโ,getpid());
Midterm โ 2006 (contโd) ืคืชืจืื (ื): 1 5 4 2 3 6
Midterm โ 2006 (contโd) ื. ืืื ืืคืื ืฉื ืืจืฆืช ืืชืืื ืืช ืืกืขืืฃ ื'? ืืื ืืื ืืคืื ืืืืื ืืืคืฉืจื? ืืกืืืจื. (ืขื 3 ืฉืืจืืช). ืคืชืจืื (ื):ืฉืืฉื ืืกืคืจืื ืืืืืื ื 0. ืืคืื ืืื ื ืืืื, ืื ืฉืืฉื ืืกืคืจืื ื ืืื ืื. ื. ืื ืืื ืฉืืจืืช 4 ื 6 ื ืืกืืฃ ืืช ืืฉืืจื: 5. kill(x, SIGINT); ืื ืืฉืชื ื ืืขืฅ ืืชืืืืืื ืืืคืื? ืคืชืจืื (ื): ืืชืืืืืื 3 ื 4 ืืืืชื. ืืคืื ืขืฉืื ืืืืฉืืจ ืืื ืื ืฉืืืืคืกื ืจืง 5 ืืกืคืจืื ืื ืจืง 4 ืืกืคืจืื.
Midterm โ 2006 (contโd) ื. ืืื ืืืชืื ืชืกืจืื ืฉืื ืืืืจ ืืฉืื ืื ื ืงืื ืคืื ืืื ืืคืื ืืืชื ืงืืืื ื ืืคื ื ืืฉืื ืื? ืื ืื, ืืื ืชืกืจืื ืื? ืื ืื, ื ืืงื ืืืืข ืื ืืชืื ืื ื ืงืื ืคืื ืืื. ืคืชืจืื (ื): ืื, ืืชืื ืืื ืชืกืจืื. ื ื ืื ืฉืืืชืืื ื ืืชื ืืื ืื ืฉื ืืฆืจ ื- forkืืจืืฅ ืขื ืืฉืจ ืืื ืืกืืื, ืืจื ืฉืื ืืื ืืกืคืืง ืืืืืข ืืฉืืจืช ืืืืคืกื.
Midterm โ 2006 (contโd) ื. ื ื ืื ืื ืชืืืจืฉื ืืืชืื ืชืืื ืืช ืืจืืืช threads, ืฉืชืจืืฅ ืขื ืืขืจืืช ืืคืขืื ืืชืืืืช ืื ื-user threads ืืื ื-kernel threads. ืืืืื ืืคืฉืจืืช ืชืืืจื ืื ื-threads ืืืฆืขืื ืคืขืืืืช I/O ืจืืืช? ืืกืืืจื (ืขื 3 ืฉืืจืืช). ืืกืืืจื ืืืืื ื ืกืืืืช (ืืืืืจ, ืขืืืจ ืืืื ืกืื ืชืืื ืืช) ืืืืชื ืืืืจืื ืืืคืฉืจืืช ืืฉื ืืื. ืคืชืจืื (ื): ืคืขืืืช I/O ืืืจืืช ื user threads ืืืื ืืขืืืจ ื blocking ืฉืื ืืขืจืืช ืืืคืขืื ืื ืืืืขืช ืืงืืืื ืืืื ืื ืกืืืจ ืืืืืจ ืืืืคืฆืื ืื ืืืงืจื ืฉื ืจืืืื ืคืขืืืืช I/O. ืืขืืืช ืืืช, ืืืื ืืืืืจ ื user threads ืืืงืจืื ืืื ืจืืฆืื ืืืฉื ืฉืืืื ืืืื ืขื ืืชืืืื. ืื ืืกืฃ, ืื ืืืืืจ ืืืขืจืืช ืขื ืืืกืืช ืืขื ืืขืืืื ื ืขืืืฃ user threads ืฉืื ืืืืคื ืืื ืืื ืืื ืืืืจื ืืืชืจ.
Thread-specific data Programs often need global or static variables that have different values in different threads:Thread-specific data (TSD). Each thread possesses a private memory block, the TSD area. This area is indexed by TSD keys (Map). TSD keys are common to all threads, but the value associated with a given TSD key can be different in each thread. Defined in POSIX.
Thread-specific data (contโd) โข Question: Why canโt we achieve this by using regular variables? โข Because threads share one memory space. โข Usage examples: โข Separate log for each thread. โข C99 standard defines __thread storage specifier, which specifies that a variable should be distinct per thread. Supported in GCC since version 3.3.
Thread-specific data (contโd)
Thread-specific data (contโd)
TSD Usage example Suppose, for instance, that your application divides a task among multiple threads. For audit purposes, each thread is to have a separate log file, in which progress messages for that thread's tasks are recorded. The thread-specific data area is a convenient place to store the file pointer for the log file for each individual thread.
#include <malloc.h> #include <pthread.h> #include <stdio.h> // The key used to associate a log file pointer with each thread. staticpthread_key_tthread_log_key; // Write MESSAGE to the log file for the current thread. voidwrite_to_thread_log(constchar* message) { FILE* thread_log = (FILE*)pthread_getspecific(thread_log_key); fprintf(thread_log, "%s\n", message); } // Close the log file pointer THREAD_LOG. voidclose_thread_log (void* thread_log) { fclose((FILE*) thread_log); }
void* thread_function (void* args) { charthread_log_filename[20]; FILE* thread_log; sprintf(thread_log_filename,"thread%d.log",(int) pthread_self()); thread_log= fopen (thread_log_filename, "w"); pthread_setspecific(thread_log_key, thread_log); write_to_thread_log ("Thread starting."); /* Do work here... */ return NULL; } int main () { inti; pthread_t threads[5]; pthread_key_create(&thread_log_key, close_thread_log); for(i = 0; i < 5; ++i) pthread_create (&(threads[i]), NULL, thread_function, NULL); for(i = 0; i < 5; ++i) pthread_join (threads[i], NULL); return 0; }