Exercise Eratosthenes Sieve Eratosthenes sieve n An algorithm





![Exercise: Eratosthenes Sieve void filter(int fd) { int pipes[2], pid, p, val; read(fd, &p); Exercise: Eratosthenes Sieve void filter(int fd) { int pipes[2], pid, p, val; read(fd, &p);](https://slidetodoc.com/presentation_image_h/22db432940fc1e344e3df04c562be177/image-6.jpg)

![Exercise: Eratosthenes Sieve void filter(int fd) { int pipes[2], pid, p, val; read(fd, &p); Exercise: Eratosthenes Sieve void filter(int fd) { int pipes[2], pid, p, val; read(fd, &p);](https://slidetodoc.com/presentation_image_h/22db432940fc1e344e3df04c562be177/image-8.jpg)































![Let’s go! pthread_t tid[6]; pthread_attr_t attr; int i; pthread_init_attr(&attr); for(i=0; i<6; i++) /* create Let’s go! pthread_t tid[6]; pthread_attr_t attr; int i; pthread_init_attr(&attr); for(i=0; i<6; i++) /* create](https://slidetodoc.com/presentation_image_h/22db432940fc1e344e3df04c562be177/image-40.jpg)



















- Slides: 59
Exercise: Eratosthenes Sieve Eratosthenes sieve: n An algorithm for computing prime numbers: n Starts with a sequence 2, 3, 4, … N, finds all prime numbers <= N n 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 n 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25 n 5, 7, 11, 13, 17, 19, 23, 25 n … Operating System Concepts 4. 1 Silberschatz, Galvin and Gagne © 2005
Exercise: Eratosthenes Sieve Idea: Speed-up the computation of the Eratosthenes sieve on a multi. CPU computer by doing the work on each line in a separate process. Process Start (core): for(i=2; i<n; i++) write(i); // write(1, &i, sizeof(int)); Process Filter (core): read(&p); // read(0, &p, sizeof(int)); Output. Prime(p); while (read(&val) >0) if (val %p != 0) write(val); Launched from shell: Start 25 | Filter | … Operating System Concepts 4. 2 Silberschatz, Galvin and Gagne © 2005
Exercise: Eratosthenes Sieve Problems: l How many filter processes to start? l Very tedious to launch it this way Solution: Create filter processes automatically Question: Who creates the filter processes? Answer: l Start process forks the first filter process l i-th filter process forks (i+1)th filter process l No need to have separate program files, the filter process can be programmed as a procedure Operating System Concepts 4. 3 Silberschatz, Galvin and Gagne © 2005
Exercise: Eratosthenes Sieve Process Start (core): pid = fork(); if (pid == 0) filter(); else { for(i=2; i<n; i++) write(i); // write(1, &i, sizeof(int)); } void filter() { read(&p); // read(0, &p, sizeof(int)); Output. Prime(p); pid = fork(); if (pid == 0) filter(); else { while (read(&val) >0) if (val %p != 0) write(val); } } Operating System Concepts 4. 4 Silberschatz, Galvin and Gagne © 2005
Exercise: Eratosthenes Sieve What is the problem now? l Everybody reads from stdin and writes to stdout Solution: l Connect the processes using pipes Process Start (core): pipe(pipes); pid = fork(); if (pid == 0) { close(pipes[1]); filter(pipes[0]); } else { close(pipes[0]); for(i=2; i<n; i++) write(pipes[1], i); // write(1, &i, sizeof(int)); close(pipes[1]); } Operating System Concepts 4. 5 Silberschatz, Galvin and Gagne © 2005
Exercise: Eratosthenes Sieve void filter(int fd) { int pipes[2], pid, p, val; read(fd, &p); // read(0, &p, sizeof(int)); Output. Prime(p); pipe(pipes); pid = fork(); if (pid == 0) { close(pipes[1]); filter(pipes[0]); } else { close(pipes[0]); while (read(fd, &val) >0) if (val %p != 0) write(pipes[1], val); close(pipes[1]); } } Operating System Concepts 4. 6 Silberschatz, Galvin and Gagne © 2005
Exercise: Eratosthenes Sieve Does it work now? l no, each filter will launch a new filter l need to know when to terminate 4 when the input of the filter contains single value – its prime number p 4 when Operating System Concepts p 2 >=n, all the remaining numbers are primes as well 4. 7 Silberschatz, Galvin and Gagne © 2005
Exercise: Eratosthenes Sieve void filter(int fd) { int pipes[2], pid, p, val; read(fd, &p); // read(0, &p, sizeof(int)); Output. Prime(p) if (p*p>=n) { while(read(fd, &p)) Output. Prime(p); } else { pipe(pipes); pid = fork(); if (pid == 0) { close(pipes[1]); filter(pipes[0]); } else { close(pipes[0]); while (read(fd, &val) >0) if (val %p != 0) write(pipes[1], val); close(pipes[1]); } } } Operating System Concepts 4. 8 Silberschatz, Galvin and Gagne © 2005
Exercise: Eratosthenes Sieve Does it work now? l Yes! But, can we make Start and Filter standalone processes communicating through stdin/stdout? l Yes, but we need to learn how to duplicate/redirect file pointers l System call dup 2(int newfp, int oldfp) wil replace the oldfp with the newfp l Let’s see how it works: Operating System Concepts 4. 9 Silberschatz, Galvin and Gagne © 2005
Exercise: Eratosthenes Sieve Process Start (core): itoa(n, n. String, 10); // convert integer n into string n. String pid = fork(); if (pid == 0) { close(pipes[1]); dup 2(pipes[0], 0); // pipes[0] will be now my stdin Execl(“filter”, n. String, NULL); } else { close(pipes[0]); dup 2(pipes[1], 1); // pipes[1] will be now my stdout for(i=2; i<n; i++) write(1, i); // write(1, &i, sizeof(int)); } Operating System Concepts 4. 10 Silberschatz, Galvin and Gagne © 2005
Exercise: Eratosthenes Sieve filter. c: } else { pipe(pipes); pid = fork(); if (pid == 0) { close(pipes[1]); dup 2(pipes[0], 0); execl(“filter”, argv[1]); } else { close(pipes[0]); dup 2(pipes[1], 1); while (read(fd, &val) >0) if (val %p != 0) write(pipes[1], val); close(pipes[1]); } } int main(int argc, int argv) { int pipes[2], pid, p, val, n; n = atoi(argv[1]); read(0, &p); // read(0, &p, sizeof(int)); Output. Prime(p) if (p*p>=n) { while(read(0, &p)) Output. Prime(p); } else { } Operating System Concepts 4. 11 Silberschatz, Galvin and Gagne © 2005
Exercise: Eratosthenes Sieve Are we done now? l Yes, 4 for l a while… What about multithreaded Eratosthenes? 4 Wait Operating System Concepts till we learn about threads. 4. 12 Silberschatz, Galvin and Gagne © 2005
Chapter 4: Threads
Threads vs Processes Process n A unit/thread of execution, together with code, data and other resources to support the execution. Idea n Make distinction between the resources and the execution threads n Could the same resources support several threads of execution? l Code, data, . . , - yes l CPU registers, stack - no Operating System Concepts 4. 14 Silberschatz, Galvin and Gagne © 2005
Single vs Multithreaded Processes Operating System Concepts 4. 15 Silberschatz, Galvin and Gagne © 2005
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. 16 Silberschatz, Galvin and Gagne © 2005
Why Threads? n Responsiveness l One thread handles user interaction l Another thread does the background work (i. e. load web page) n Utilization of multiprocessor architectures l One process/thread can utilize only one CPU n Well, but all this applies to one thread per process as well, just use processes n Efficiency and convenience l Resource sharing (shared memory, open files, …) l Creating a thread, context switch between threads, can be much cheaper then for full processes l Why? Operating System Concepts 4. 17 Silberschatz, Galvin and Gagne © 2005
Thread Library n Provides the programmer an API for creating and managing threads n User thread l The unit of execution managed by the thread library l This is what is presented to the programmer n Kernel thread l The unit of execution scheduled and managed by the kernel n Thread libraries: l POSIX Pthreads l Java threads l Win 32 threads n Essentially all modern OSs support kernel threads Operating System Concepts 4. 18 Silberschatz, Galvin and Gagne © 2005
User Space Thread Library n All code and data structures for thread management in user space n No system calls involved, no OS support needed n Many(all) user threads mapped to single kernel thread Operating System Concepts 4. 19 Silberschatz, Galvin and Gagne © 2005
Many to One Mapping Properties: l Cheap/fast, but runs as one process to the OS scheduler l What happens if one thread blocks on an I/O? 4 All l other threads block as well How to make use of multiple CPUs? 4 Not possible Examples l Solaris Green Threads l GNU Portable Threads Operating System Concepts 4. 20 Silberschatz, Galvin and Gagne © 2005
Kernel Space Thread Library n Code and data structures in kernel space n Need support by the OS n Calling a thread library typically results in system call n Each user thread is mapped to a kernel thread Operating System Concepts 4. 21 Silberschatz, Galvin and Gagne © 2005
One to One Mapping Properties l Usually, limited number of threads l Thread management relatively costly l But provides full benefits of multithreading Examples l Windows NT/XP/2000 l Linux l Solaris 9 and later Operating System Concepts 4. 22 Silberschatz, Galvin and Gagne © 2005
Many-to-Many Model n Allows many user level threads to be mapped to many kernel threads n The thread library cooperates with the OS to dynamically map user threads to kernel threads n Intermediate costs and most of the benefits of multithreading n Examples: l Solaris prior to version 9 l Windows NT/2000 with the Thread. Fiber package Operating System Concepts 4. 23 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. 24 Silberschatz, Galvin and Gagne © 2005
Single vs Multithreaded Processes Operating System Concepts 4. 25 Silberschatz, Galvin and Gagne © 2005
Thread Library n Provides the programmer an API for creating and managing threads n User thread l The unit of execution managed by the thread library l This is what is presented to the programmer n Kernel thread l The unit of execution scheduled and managed by the kernel n Thread libraries: l POSIX Pthreads l Java threads l Win 32 threads n Essentially all modern OSs support kernel threads Operating System Concepts 4. 26 Silberschatz, Galvin and Gagne © 2005
User to Kernel Thread Mapping Operating System Concepts 4. 27 Silberschatz, Galvin and Gagne © 2005
Threading Issues Ha! It is nice to have threads, but what does that mean when we think through all consequences? Issues: l Semantics of fork() and exec() system calls l Thread cancellation l Signal handling l Thread pools l Thread specific data l Scheduler activations Operating System Concepts 4. 28 Silberschatz, Galvin and Gagne © 2005
Threading Issues Ha! It is nice to have threads, but what does that mean when we think through all consequences? Issues: l Semantics of fork() and exec() system calls l Thread cancellation l Signal handling l Thread pools l Thread specific data l Scheduler activations Operating System Concepts 4. 29 Silberschatz, Galvin and Gagne © 2005
Semantics of fork() and exec() n Does fork() duplicate only the calling thread or all threads? l Often two versions provided l Which one to use? n What does exec() do? l Well, it replaces the address space, so all threads must go Operating System Concepts 4. 30 Silberschatz, Galvin and Gagne © 2005
Thread Cancellation n Terminating a thread before it has finished n Two general approaches: l l Asynchronous cancellation terminates the target thread immediately 4 Might leave the shared data in corrupt state 4 Some resources may not be freed Deferred cancellation 4 Set a flag which the target thread periodically checks to see if it should be cancelled 4 Allows Operating System Concepts graceful termination 4. 31 Silberschatz, Galvin and Gagne © 2005
Signal Handling n Signals are used in UNIX systems to notify a process that a particular event has occurred n Essentially software interrupt n A signal handler is used to process signals n 1. Signal is generated by particular event 2. Signal is delivered to a process 3. Signal is handled Options: l Deliver the signal to the thread to which the signal applies l Deliver the signal to every thread in the process l Deliver the signal to certain threads in the process l Assign a specific thread to receive all signals for the process Operating System Concepts 4. 32 Silberschatz, Galvin and Gagne © 2005
Thread Pools n A server process might service requests by creating a thread for each request that needs servicing l But thread creation costs are wasted time l No control over the number of threads, possibly overwhelming the system n Solution l Create a number of threads in a pool where they await work l Advantages: 4 The overhead for creating threads is paid only at the beginning 4 Allows the number of threads in the application(s) to be bound to the size of the pool Operating System Concepts 4. 33 Silberschatz, Galvin and Gagne © 2005
Thread Specific Data n Allows each thread to have its own copy of data n Useful when you do not have control over the thread creation process (i. e. , when using a thread pool) Operating System Concepts 4. 34 Silberschatz, Galvin and Gagne © 2005
Scheduler Activations n Both M: M and Two-level models require communication from the kernel to inform the thread library when a user thread is about to block, and when it again becomes ready for execution n When such event occurs, the kernel makes an upcall to the thread library n The thread library’s upcall handler handles the event (i. e. save the user thread’s state and mark it as blocked) Operating System Concepts 4. 35 Silberschatz, Galvin and Gagne © 2005
Chapter 4: Threads n Overview n Multithreading Models n Threading Issues n Specific thread libraries l Pthreads l Win 32 l Java threads n Thread implementations l Windows XP l Linux l Java Operating System Concepts 4. 36 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 the developer of the library n Common in UNIX operating systems (Solaris, Linux, Mac OS X) n Typical functions: l pthread_create (thread, attr, start_routine, arg) l pthread_exit (status) l pthread_join (threadid, status) l pthread_attr_init (attr) Operating System Concepts 4. 37 Silberschatz, Galvin and Gagne © 2005
Thread Programming Exercise Goal: Write multithreaded matrix multiplication algorithm, in order to make use of several CPUs. Single threaded algorithm for multiplying n x n matrices A and B : for(i=0; i<n; i++) for(j=0; j<n; j++) { C[i, j] = 0; for(k=0; k<n; k++) C[i, j] += A[i, k] * B[k, j]; } Just to make our life easier: Assume you have 6 CPUs and n is multiple of 6. How to start? Any ideas? Operating System Concepts 4. 38 Silberschatz, Galvin and Gagne © 2005
Multithreaded Matrix Multiplication Idea: • create 6 threads • have each thread compute 1/6 of the matrix C • wait until everybody finished • the matrix can be used now Thread 0 Thread 1 Thread 2 Thread 3 Thread 4 Thread 5 Operating System Concepts 4. 39 Silberschatz, Galvin and Gagne © 2005
Let’s go! pthread_t tid[6]; pthread_attr_t attr; int i; pthread_init_attr(&attr); for(i=0; i<6; i++) /* create the working threads */ pthread_create( &tid[i], &attr, worker, &i); for(i=0; i<6; i++) /* now wait until everybody finishes */ pthread_join(tid[i], NULL); /* the matrix C can be used now */ … Operating System Concepts 4. 40 Silberschatz, Galvin and Gagne © 2005
Let’s go! void *worker(void *param) { int i, j, k; int id = *((int *) param); /* take param to be pointer to integer */ int low = id*n/6; int high = (id+1)*n/6; for(i=low; i<high; i++) for(j=0; j<n; j++) { C[i, j] = 0; for(k=0; k<n; k++) C[i, j] = A[i, k]*B[k, j]; } pthread_exit(0); } Operating System Concepts 4. 41 Silberschatz, Galvin and Gagne © 2005
Let’s go! Would it work? • do we need to pass A, B, C and n in the parameters? • no, they are in the shared memory, we are fine • did we pass IDs properly? • not really, all threads get the same pointer for(i=0; i<6; i++) /* create the working threads */ { id[i] = i; pthread_create( &tid[i], &attr, worker, &id[i]); } Would it work now? • should, … Operating System Concepts 4. 42 Silberschatz, Galvin and Gagne © 2005
Win 32 Thread API // thread creation: Thread. Handle = Create. Thread( NULL, // default security attributes 0, // default stack size Summation, // function to execute &Param, // parameter to thread function 0, // default creation flags &Thread. Id); // returns the thread ID Simple programming example: Operating System Concepts 4. 43 Silberschatz, Galvin and Gagne © 2005
Java Threads n Java threads are created by calling a start() method of a class that l extends Thread class, or l implements the Runnable interface: public interface Runnable { public abstract void run(); } n Java threads are inherent and important part of the Java language, with rich API available Operating System Concepts 4. 44 Silberschatz, Galvin and Gagne © 2005
Extending the Thread Class class Worker 1 extends Thread { public void run() { System. out. println("I Am a Worker Thread"); } } public class First { public static void main(String args[]) { Worker 1 runner = new Worker 1(); runner. start(); System. out. println("I Am The Main Thread"); } } Operating System Concepts 4. 45 Silberschatz, Galvin and Gagne © 2005
Implementing the Runnable Interface class Worker 2 implements Runnable { public void run() { System. out. println("I Am a Worker Thread "); } } public class Second { public static void main(String args[]) { Runnable runner = new Worker 2(); Thread thrd = new Thread(runner); thrd. start(); System. out. println("I Am The Main Thread"); } } Operating System Concepts 4. 46 Silberschatz, Galvin and Gagne © 2005
Joining Threads class Joinable. Worker implements Runnable { public void run() { System. out. println("Worker working"); } } public class Join. Example { public static void main(String[] args) { Thread task = new Thread(new Joinable. Worker()); task. start(); try { task. join(); } catch (Interrupted. Exception ie) { } System. out. println("Worker done"); } } Operating System Concepts 4. 47 Silberschatz, Galvin and Gagne © 2005
Thread Cancellation Thread thrd = new Thread (new Interruptible. Thread()); thrd. start(); . . . // now interrupt it thrd. interrupt(); Operating System Concepts 4. 48 Silberschatz, Galvin and Gagne © 2005
Thread Cancellation public class Interruptible. Thread implements Runnable { public void run() { while (true) { /** * do some work for awhile */ if (Thread. current. Thread(). is. Interrupted()) { System. out. println("I'm interrupted!"); break; } } // clean up and terminate } } Operating System Concepts 4. 49 Silberschatz, Galvin and Gagne © 2005
Thread Specific Data class Service { private static Thread. Local error. Code = new Thread. Local(); public static void transaction() { try { /** * some operation where an error may occur */ catch (Exception e) { error. Code. set(e); } } /** * get the error code for this transaction */ public static Object get. Error. Code() { return error. Code. get(); } } Operating System Concepts 4. 50 Silberschatz, Galvin and Gagne © 2005
Thread Specific Data class Worker implements Runnable { private static Service provider; public void run() { provider. transaction(); System. out. println(provider. get. Error. Code()); } } Somewhere in the code: … Worker worker 1 = new Worker(); Worker worker 2 = new Worker(); worker 1. start(); worker 2. start(); … Assume there were different errors in the transactions of both workers, but both transactions finished before any of the prints in the run() methods started. Would the workers print the same errors or not? Operating System Concepts 4. 51 Silberschatz, Galvin and Gagne © 2005
Chapter 4: Threads n Overview n Multithreading Models n Threading Issues n Specific thread libraries l Pthreads l Win 32 l Java threads n Thread implementations l Windows XP l Linux l Java Operating System Concepts 4. 52 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. 53 Silberschatz, Galvin and Gagne © 2005
Windows XP Threads Operating System Concepts 4. 54 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 The clone() system call allows to specify which resources are shared between the child and the parent l Full sharing threads l Little sharing like fork() Operating System Concepts 4. 55 Silberschatz, Galvin and Gagne © 2005
Java Threads Java threads are managed by the JVM Operating System Concepts 4. 56 Silberschatz, Galvin and Gagne © 2005
Chapter 4: Threads n Overview n Multithreading Models n Threading Issues n Specific thread libraries l Pthreads l Win 32 l Java threads n Thread implementations l Windows XP l Linux l Java n More threads examples and exercises Operating System Concepts 4. 57 Silberschatz, Galvin and Gagne © 2005
Understanding threads Take a look at this program. What would be its output? Why? Operating System Concepts 4. 58 Silberschatz, Galvin and Gagne © 2005
End of Chapter 4