290 likes | 463 Views
Real Time System. Multitasking. Layers of abstraction for concurrency in programs. E.g. Furnace Control. Three task Furnace Heater Control Set Threshold Temperature Refresh Display with current temperature Single execution thread Multiple threads of execution. Multitasking.
E N D
Real Time System Multitasking
E.g. Furnace Control • Three task • Furnace Heater Control • Set Threshold Temperature • Refresh Display with current temperature • Single execution thread • Multiple threads of execution
Multitasking • fundamental mechanism for an application to control and react to multiple, discrete real-world events • Improve responsiveness by avoiding situations where long-running task can block a task that responds to external stimuli • Improved responsiveness reduces latency • improve performance by allowing a program to run simultaneously on multiple processors or cores • MultiCore processors
Multitasking • directly control the timing of external interactions • E.g display refresh • Task states
Multithreading • In broad sense, threads exist in the form of interrupts on almost all microprocessors • Most operating systems provide a higher-level mechanism than interrupts to realize imperative programs that share memory • Typically they conform to standardized API • which makes it possible to write programs that are portable • Eg. Pthreads or POSIX threads
Implementing threads- scheduling • cooperative multitasking • does not interrupt a thread unless the thread itself calls a certain procedure or make a system call • Disadvantage – a task may execute without making any OS system call • Other thread will be starved • Most OS have a ISR routing triggered by a TIMER after every time interval called JIFFY which schedule different threads.
Simple Multithreaded C Program • #include <pthread.h> • #include <stdio.h> • void* printN(void* arg) { • for (int i = 0; i < 10; i++) { • printf("My ID: %d\n", *(int*)arg); } • return NULL; • } • int main(void) { • pthread_t threadID1, threadID2; • void* exitStatus; • int x1 = 1, x2 = 2, x3 = 3; • pthread_mutex_init( mymutex,0); • pthread_create(&threadID1, NULL, printN, &x1); • pthread_create(&threadID2, NULL, printN, &x2); • printf("Started threads.\n"); • pthread_join(threadID1, &exitStatus); • pthread_join(threadID2, &exitStatus); • return 0; • }
Output of the simple program SStSatrtatraredttthredthredeadste. hrMyads.ea ds. MIyD :ID: 1 1 MMyyID: 2I MyD: ID: 2 M y2 M Iy ID:D: 1 My 2 I MDy: I D1: M2 y My ID: I1D: My I2 MDy I:D: 1 M1y My IDID: 2 : M1 y MIyD: 2 I DM:y 1 MID: y2 MID: y ID1: M 2y ID: 2
Mutual Exclusion • Race condition • Two concurrent pieces of code race to access the same resource, and the exact order in which their accesses occurs affects the results of the program • Critical Section • To prevent MUTEX(Mutual Exclusion Locks) are used
Simple Multithreaded C Program • pthread_mutex_t *mymutex= (pthread_mutex_t *)PTHREAD_MUTEX_DEFAULT; • void* printN(void* arg) { • pthread_mutex_lock(mymutex); • for (int i = 0; i < 10; i++) {printf("My ID: %d\n", *(int*)arg);} • pthread_mutex_unlock(mymutex); • return NULL; • } • int main(void) { • pthread_t threadID1, threadID2; • void* exitStatus; • int x1 = 1, x2 = 2, x3 = 3; • pthread_mutex_init( mymutex,0); • pthread_create(&threadID1, NULL, printN, &x1); • pthread_create(&threadID2, NULL, printN, &x2); • printf("Started threads.\n"); • pthread_join(threadID1, &exitStatus); • pthread_join(threadID2, &exitStatus); • return 0; • }
SSttarartteedtdhrtehreads.a Myds I. D: 1 My ID: 1 My ID: 1 My ID: 1 My ID: 1 My ID: 1 My ID: 1 My ID: 1 My ID: 1 My ID: 1 My ID: 2 My ID: 2 My ID: 2 My ID: 2 My ID: 2 My ID: 2 My ID: 2 My ID: 2 My ID: 2 My ID: 2
Simple Multithreaded C Program • void* printN(void* arg) { • inti; • for (i = 0; i < 10; i++) { • pthread_mutex_lock(mymutex); • printf("My ID: %d\n", *(int*)arg); • pthread_mutex_unlock(mymutex); • } • return NULL; • } • int main(void) { • pthread_t threadID1, threadID2; • void* exitStatus; • int x1 = 1, x2 = 2,x3=3; • pthread_mutex_init( mymutex,0); • pthread_create(&threadID1, NULL, printN, &x1); • pthread_create(&threadID2, NULL, printN, &x2); • pthread_mutex_lock(mymutex); • printf("Started threads.\n"); • pthread_mutex_unlock(mymutex); • pthread_join(threadID1, &exitStatus); • pthread_join(threadID2, &exitStatus); • return 0; • }
Started threads. My ID: 1 My ID: 1 My ID: 1 My ID: 1 My ID: 1 My ID: 1 My ID: 1 My ID: 1 My ID: 1 My ID: 1 My ID: 2 My ID: 2 My ID: 2 My ID: 2 My ID: 2 My ID: 2 My ID: 2 My ID: 2 My ID: 2 My ID: 2
Mutual Exclusion • As mutex locks proliferate in programs, the risk of deadlock increases
Deadlocks • A deadlock occurs when some threads become permanently blocked trying to acquire locks • There are a set of necessary conditions and if any one of them is avoided deadlock can be avoided. • Mutual exclusion condition: a resource that cannot be used by more than one process at a time • Hold and wait condition: processes already holding resources may request new resources held by other processes • No preemption condition: No resource can be forcibly removed from a process holding it, resources can be released only by the explicit action of the process. • Circular wait condition: two or more processes form a circular chain where each process waits for a resource that the next process in the chain holds.
Deadlock • Prevention • By avoiding any of the necessary condition • Deadlock avoidance • Bankers algorithm • most available techniques either impose severe constraints on the programmer or require considerable sophistication to apply
Memory consistency models • Sequential consistency Thread A x = 1; w = y; Thread B: y = 1; z = x;
Disadvantage of Multithreading • Difficulty of writing code • Difficulty of managing concurrency • Difficulty of testing and Debugging • Difficult to identify and verify root causes when errors occur • Defects are generally time related • Difficult to reproduce
Multiprocessing • Processes are imperative programs with their own memory spaces • Cannot refer to each other variable • consequently they do not exhibit the same difficulties as threads • Require support from OS - MMU • Mechanisms for communication provided by the operating system • Simple- File system • Message Passing
Message passing • a computation comprises one or more processes that communicate by calling library routines to send and receive messages • Communication is cooperative • data is sent by calling a routine, and the data is not received until the destination process calls a routine to receive the data • Programs written are highly portable • Provide programmer with explicit control over the location of memory in parallel programs • Library have routines for point to point and collective operations.
Semaphore • Counting Semaphore • Semaphore are like mutex which count instead of lock • Is a variable whose value is a non-negative integer • Two basic operation • inc/acquire • Dec/release • Only the thread that locks the mutex can unlock it whereas for a semaphore is acquired by one task and released by another task
Semaphore Conditional variables Conditional variaables generalize the idea of semaphore They support arbitrary condition than zero or non-zero as gating criteria for blocking Message passing pitfalls Message queue size Program may create message passing program that deadlock
Multithreading Vs. Multiprocessing • Approaches to design and develop parallel prog. • Distinction is based on how they • share data and • Implicit in multithreading using shared memory • Message queues are used in multiprocessing • synchronize shared data access • Synch. Using mutexes • Synch implicit due to different time of sending and receiving data
Model-Driven Development • modeling software help us to overcome complexity • primary artifacts of development are models rather than programs • programs are still the ultimate product, but they are derived automatically from the models • Model Driven Architecture • UML
Advantage of Model Driven Develop. • Abstraction • helps us understand a complex system or situation by removing or hiding from view irrelevant detail, allowing us to better focus on essentials. • Comprehensibility • Are expressed in a form (e.g., a notation) that directly appeals to human intuition, providing a shortcut for understanding complex phenomena. • Accuracy • provides a true–to-life representation of those aspects of the modeled system that are of concern. • Predictive-ness • Accurate models can be used to correctly predict interesting characteristics of the modeled system before the system is constructed • Low cost • models based development take less effort