





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






















l (^) fork() → exec() can be used to start new program execution l (^) processes are well protected from each other l (^) context-switching between processes is fairly expensive l (^) inter-process communication used for information sharing and co- ordination between processes (^) shared memory (^) message passing
l (^) individual process only offers one thread of control
l (^) processes NOT designed to do this
l (^) all in one application, anyway! l (^) process is an over-kill
l (^) context switching involves a lot of overhead
l (^) processes duplicate entire address space
l (^) do as little as possible to allow execution of a thread of control
l (^) only the necessary context information is re-generated (^) thread-context: PC, registers, stack, other misc. info (^) process-context: also includes data and code regions l (^) threads are executed within a process (^) code and data shared among different threads (^) reduced communication overhead l (^) smaller context (^) faster context switching l (^) a single address space for all threads in a process (^) reduced inter-thread protection
l (^) for an interactive user, if part of the application is blocked
l (^) easier, via memory sharing l (^) be aware of synchronization issues
l (^) sharing reduces creation, context-switching, and space overhead
l (^) can exploit computational resources of a multicore CPU
#include <pthread.h> #include <stdio.h> int sum; /* data shared by all threads */ void runner(void param); / * thread function prototype */ int main (int argc, char argv[]) { pthread_t tid; / thread identifier / pthread_attr_t attr / set of thread attributes / if(atoi(argv[1]) < 0) { fprintf(stderr, “%d must be >=0\n”, atoi(argv[1])); return -1; } / get the default thread attributes / pthread_attr_init(&attr); / create the thread / pthread_create(&tid, &attr, runner, argv[1]); / wait for the thread to exit */ pthread_join(tid, NULL); fprintf(stdout, “sum = %d\n”, sum); }
.... (cont. from previous page...) /* The thread will begin control in this function */ void *runner (void *param) { int i, upper = atoi(param); sum = 0; for(i=1 ; i<=upper ; i++) sum += i; pthread_exit(0); }
l (^) advantages (^) efficient and flexible in space, speed, switching, and scheduling l (^) disadvantages (^) one thread blocked on I/O can block all threads (^) difficult to automatically take advantage of SMP l (^) examples (^) GNU Portable Threads, Free BSD's userland threads, QuickThreads
l (^) Advantages (^) removes disadvantages of user-level threads l (^) Disadvantages (^) greater overhead due to kernel involvement l (^) Examples (^) Pthreads, Windows XP threads, etc.
l (^) Many-to-One l (^) One-to-One l (^) Many-to-Many
l (^) examples – Windows NT/XP/2000, Linux, Solaris 9 and later
l (^) operating system can create a sufficient number of kernel threads l (^) examples – Solaris prior to v9, Windows NT/2000 ThreadFiber package
l (^) some systems provide two versions of fork()
l (^) most systems maintain the semantics of exec()
l (^) exec() called immediately after fork (^) duplicating all threads is unnecessary l (^) exec() not called after fork (^) new process should duplicate all threads