MultiThread Programming - Multithread Programming Alireza...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Multithread Programming Alireza Abedinejad Multithread Programming Alireza Abedinejad 1 Program: An ordered set of instructions to perform a computation or data processing task. An algorithm in a form to be used by a computer. algorithm 2 Process: A program in (some stage of) execution. program 3 Multiprogramming Multiprocessing: 1) Multiprogramming 2) Parallel processing (multiple processors) depending on context Multitasking: 1) Multiprogramming 2) A particular form of multiprogramming depending on context 4 Concurrent vs. Simultaneous Execution Multiprogramming: Concurrent execution of programs. Processes take turns using processor, one at a time. Logical parallelism. Parallel Processing: Simultaneous execution of two or more programs on as many processors. If two processors, then two processes at a time use the processor. Physical parallelism. 5 The Process Model Conceptual model of 4 independent, sequential processes Only one program active at any instant Switch from one process to another by “context switch” 6 Process Creation and Termination When do new processes get created? Any command -- Ex: Click on the IE icon First process? When do processes get terminated? 1. Normal exit (voluntary) – exit() system call 2. Error exit (voluntary) – exit() system call 3. Fatal error (involuntary) 4. Killed by another process (involuntary) 7 Implementation of Multiprogramming Context Switch Resources (CPU) taken away from one process and Resources given to another given Save the context of previous process and restore context of Save new process new 8 Process Context? Contents of Registers Program Counter Stack Pointer Process State Open file descriptors .. Operating System stores process contexts in Process Table Operating Process – one entry per process one 9 When to Switch Context? When a process waits for I/O When a process has got its quota of time When a previously started I/O has completed 10 Interprocess Communication Wide Variety of interprocess communication (IPC) Wide mechanisms mechanisms OS dependent Examples Pipes Sockets Shared memory Threads share an address space What happens if 2 threads or processes (with IPC) want What to change something shared? to 11 Synchronization - Example ATM (threaded) server – can deposit or withdraw money ATM int adjust_balance(int account, int amount) { int /* amount can be negative – withdrawal */ /* int balance = get_balance(account); int balance += amount; balance put_balance(account, balance); put_balance(account, return balance; return } Simultaneously, you deposit $100 and your SO withdraws Simultaneously, $10 in an account with $100 balance. What are the possible balances? possible 12 Synchronization – Race conditions Race condition Result is non-deterministic and depends upon timing Need to close or eliminate the timing window Solution should minimize wasted resources Classes of problems with race condition Readers/Writers – add print_balance() to example Producer/Consumer Sleeping barber – queue management Dining Philosophers 13 Synchronization - Mutual Exclusion Insure that only one thread has access to shared resources Insure at a time at Code that uses mutual exclusion for synchronization is Code called a critical section critical Only one thread is executing in the region Other threads wait on attempting to enter When one thread leaves, another may enter 14 Mutual Exclusion – by Brute Force Disable Interrupts disable_interrupts (); /* new system call execute_critical_code (); enable_interrupts (); */ Should OS let user disable interrupts? Does not work on multiprocessors! 15 Mutual Exclusion Strict Alternation int turn; int my_id = get_pid(); while (1) { while (turn != my_id) ; /* critical section */ turn = your_id (); /*just for this example*/ /* other code */ } Busy waiting – aka spinlock Busy 16 Critical Sections – 4 IFF conditions Requirements Mutual Exclusion Progress – no thread outside of critical region can block Progress another thread’s entrance another Bounded Waiting – no process should have to wait Bounded forever (starvation) forever Performance – overhead of entering and exiting section Performance is small 17 Locks Locks and critical sections acquire (lock) – at beginning of critical section Waits if lock is set – can block or busy wait Waits Returns when lock is available – it sets lock Returns lock release (lock) – called at end of critical section and clears lock lock mutex – locks that block – give up processor 18 Hardware Sync. Help Class of synchronization instructions – atomic Class read-modify-write read-modify-write Test&Set (TAS) int test_and_set (int &target) { int temp; temp = target; /* actually sets PSW /* temp target = 1; return (temp); } cond. code */ TAS allows us to build locks 19 Semaphores semaphore - invented by Dijkstra in 1968, as part of the semaphore THE OS THE A semaphore is: a variable that is manipulated atomically through two variable operations, signal and wait signal wait wait(semaphore): decrement, block until semaphore is wait(semaphore): free – history P() (maybe test in Dutch) (aka down) down signal(semaphore): increment, allow another to enter – signal(semaphore): V() (increment in Dutch?)(aka up) up 20 Semaphores Semaphore has a queue of processes/threads wait() call – tests and waits if semaphore is “available”, thread continues if semaphore is “unavailable”, thread blocks, waits on queue signal() call - opens the semaphore if thread(s) are queued, one thread is unblocked if queue is empty, the signal is remembered for next time a if wait() is called wait() Semaphores have a history - counter wait decrements counter signal increments counter 21 Semaphores typedef struct { typedef int value; struct process *L; } semaphore; /* wait and signal must be atomic */ /* void wait(semaphore s) { void if (s.value <= 0) { /* append current process to s.L */ block(); } s.value--; s.value--; } void signal(semaphore s) { s.value++; if (s.value > 0) { /* remove process P from s.L */ wakeup(P); } } 22 Semaphores Binary semaphore – a MUTEX Binary semaphore guarantees mutually exclusive access to resource only one thread/process allowed entry at a time counter is initialized to 1 Counting semaphore Counting represents a resources with many units available allows threads/process to enter as long as more units allows are available are counter is initialized to N (number of units available) 23 Beyond Semaphores Semaphores can help solve any of the traditional Semaphores synchronization problems, BUT: synchronization Can easily be misused Global variables Proper usage Proper Have no direct relationship to the data being controlled Another approach – use programming language support Another 24 Classical Problems of Synchronization Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem 25 Bounded-Buffer Problem Multiple producers and consumers /* producer */ do { …. /* produce an item into nextp */ … while counter == n; buffer[in] = nextp; in = (in+1) % n; counter++; } while (1); /* consumer */ do { while counter == 0; nextc = buffer[out]; out = (out + 1) % n; counter--; /* consume the item in nextc */ } while (1); Single producer and consumer Use counter to check whether buffer is full or empty Problems Modification of counter is not atomic Cannot handle multiple producers or consumers 26 Bounded-Buffer Problem Producer: do { do … produce an item in nextp … wait(empty); wait(mutex); … add nextp to buffer … signal(mutex); signal(full); } while (1); Consumer: do { wait(full) wait(mutex); … remove an item from buffer to nextc … signal(mutex); signal(empty); … consume the item in nextc … } while (1); Shared data semaphore full, empty, mutex; Initialization: full = , empty = , mutex = 27 Readers-Writers Problem A date object is to be shared Readers: only read shared objects Writers: modifies shared objects Problem Multiple readers can read simultaneously When a writer has obtained the permission to shared object Readers and other writers can not access First Readers-Writers Problem First No reader will be kept waiting unless a writer is accessing shared No object object Readers have priority Second Readers-Writers Problem A writer performs its write as soon as possible Or, if a writer is waiting, no new readers may start reading Writers have priority 28 Readers-Writers Problem (Readers with Priority) Variables and Initialization Semaphore mutex, wrt; int readcount; Mutex=1; wrt=1; readcount=0; Reader wait(mutex); readcount++; if (readcount == 1) wait(wrt); signal(mutex); … reading is performed … wait(mutex); readcount--; if (readcount == 0) signal(wrt); signal(mutex): 29 Writer wait(wrt); wait(wrt); … writing is performed … signal(wrt); Semaphores – Example Add print_balance() – Readers – Writers semaphore mutex, wrt; int rc = 0; /* rc is the number of active readers */ //Writing : //Writing /* Safe Update example code block */ /* {wait (&wrt); adjust_balance(account, amount); adjust_balance(account, signal (&wrt); } signal //Read: /* Correct balance for many – example code block */ /* {wait (&mutex); {wait rc++; rc++; if (rc == 1) wait (&wrt); if signal (&mutex); signal print_balance (get_balance (account)); print_balance wait (&mutex); wait rc--; rc--; if (rc == 0) signal (&wrt); if signal (&mutex); } signal 30 Comments on Reader Priority semaphores wrt are initialized to 1 note that readers have priority note a writer can gain access to the data only if there are no writer readers (i.e. when readcount is zero, signal(wrt) executes) executes) possibility of starvation possibility writers may never gain access to data 31 Synchronization Semaphores Easy to add to any language Harder to use correctly What about multiprocessor support? 32 Interprocess Communication Common IPC mechanisms Common shared memory – read/write to shared region shmget(), shmctl() in Unix Memory mapped files in WinNT/2000 semaphores - signal notifies waiting process software interrupts - process notified asynchronously – signal () signal pipes - unidirectional stream communication message passing - processes send and receive messages 33 IPC – Software Interrupts Similar to hardware interrupt. Processes interrupt each other Processes Asynchronous! Stops execution then restarts Keyboard driven – e.g. cntl-C An alarm scheduled by the process expires An Unix: SIGALRM from alarm() or settimer() Unix: alarm() settimer() resource limit exceeded (disk quota, CPU time...) programming errors: invalid data, divide by zero 34 IPC – Software Interrupts SendInterrupt(pid, num) Send signal type num to process pid, Send num pid kill() in Unix (NT doesn’t allow signals to processes) HandleInterrupt(num, handler) type num, use function handler type num use handler signal() in Unix Use exception handler in WinNT/2000 Typical handlers: Typical ignore terminate (maybe w/core dump) user-defined 35 IPC - Pipes Pipes are a bi-directional stream communication method Pipes between 2 processes between Unix/Linux 2 file descriptors file Byte stream Win/NT Win/NT 1 handle Byte stream and structured (messages) 36 IPC – Pipes #include <iostream.h> #include <unistd.h #include <stdlib.h> #define BUFFSIZE 1024 char data[ ] = “whatever” int pipefd[2]; /* file descriptors for pipe ends */ /* NO ERROR CHECKING, ILLUSTRATION ONLY!!!!! */ main() { char sbBuf[BUFFSIZE]; pipe(pipefd); if (fork() > 0 ) { /* parent, read from pipe */ close(pipefd[1]); /* close write end */ read(pipefd[0], sbBuf, BUFFSIZE); /* do something with the data */ } else { /* child, write data to pipe */ close(pipefd[0]); /* close read end */ write(pipefd[1], data, sizeof(DATA)); close(pipefd[1]); exit(0); } } 37 IPC – Message Passing Communicate information from one process to another via Communicate primitives: primitives: send(dest, &message) receive(source, &message) Receiver can specify ANY Receiver ANY Receiver can block (or not) Applicable to multiprocessor systems Applicable 38 IPC – Message Passing void Producer() { while (TRUE) { /* produce item */ build_message(&m, item); send(consumer, &m); receive(consumer, &m); /* wait for ack */ } } void Consumer { while(TRUE) { receive(producer, &m); extract_item(&m, &item); send(producer, &m); /* ack */ /* consume item */ } } 39 IPC – Message Passing send ( ) Synchronous • Returns after data is sent Returns after • Blocks if buffer is full Asynchronous • Returns as soon as I/O started • Done? – Explicit check – Signal • Blocks if buffer is full receive () Sync. • Returns if there is a message • Blocks if not Async. • Returns if there is a message • Returns if no message 40 IPC – Message Passing Indirect Communication – mailboxes Messages are sent to a named area – mailbox Processes read messages from the mailbox Mailbox must be created and managed Sender blocks if mailbox is full Many to many communication 41 IPC – Message Passing Scrambled messages (checksum) Lost messages (acknowledgements) Lost acknowledgements (sequence no.) Process unreachable (down, terminates) Naming Authentication Performance (from copying, message building Look at MPI - http://www-unix.mcs.anl.gov/mpi/ http://www-unix.mcs.anl.gov/mpi/ Look 42 What is thread? Thread is a shorthand for thread of control. Thread thread A section of code executed independently of other section threads of control within a single program Consist of program counter, a register set, and a stack Consist space. space. Shares with peer threads its code section, data section, and Shares operation system 43 Meaning of thread within a process threads pc Data segment 44 Why Thread? Reduce context-switching Reduce multiple redundant processes 45 Semaphore implementation UNIX System V semaphores semaphore = set of nonnegative integers POSIX semaphores are single integers each semaphore in the set tracks: value last pid to successfully modify # processes waiting to increment # processes waiting for semaphore to hit zero 46 Semaphore API Create using semget() – 3 parameters key_t key – see man page for ftok() number of semaphores in the set permission flags returns semaphore set ID Modify using semop() – 3 parameters semaphore set ID array of semaphore operation structures – 1 for each array semaphore value in the semaphore set semaphore length of array 47 Semaphore API Control operations – semctl() semaphore set ID index of desired semaphore in set command command argument Use semctl() to remove a semaphore from the system finite resource… you can run out! See handouts and man pages for more info 48 Multithread : Windows API CreateThread ResumeThread ExitThread 49 Windows API : Thread Functions HANDLE CreateThread( HANDLE LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD LPSECURITY_ATTRIBUTES DWORD dwStackSize, // initial stack size DWORD LPTHREAD_START_ROUTINE lpStartAddress, // thread function LPTHREAD_START_ROUTINE LPVOID lpParameter, // thread argument LPVOID DWORD dwCreationFlags, // creation option DWORD LPDWORD lpThreadId // thread identifier ); LPDWORD ); DWORD WINAPI ThreadProc( LPVOID lpParameter ); 50 Visual C++ : Thread Class Class CWinThread Class BOOL CreateThread( DWORD dwCreateFlags = 0, UINT nStackSize = 0, dwCreateFlags nStackSize LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL ); lpSecurityAttrs DWORD ResumeThread( ); virtual int Run( ); 51 Visual C++ : Synchronization 1. CCriticalSection CMutex Csemaphore CEvent 52 Visual C++ : Synchronization 1. 2. 3. 4. Does the application have to wait for something to happen Does before it can access the resource (for example, data must be received from a communications port before it can be written to a file)? If yes, use CEvent. CEvent Can more than one thread within the same application Can access this resource at one time (for example, your application allows up to five windows with views on the same document)? If yes, use CSemaphore. CSemaphore Can more than one application use this resource (for Can example, the resource is in a DLL)? If yes, use CMutex. CMutex If no, use CCriticalSection. If CCriticalSection 53 ...
View Full Document

Ask a homework question - tutors are online