1 Inter Process Communication & Timers. 2 Time.h (page R:Ch9 pp302-320) #include time_t time(time_t *calptr); Epoch: 00:00 (midnight), Jan 1, 1970 GMT.

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Review: Chapters 1 – Chapter 1: OS is a layer between user and hardware to make life easier for user and use hardware efficiently Control program.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
1 Threads CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5.
Chapter 2: Processes Topics –Processes –Threads –Process Scheduling –Inter Process Communication (IPC) Reference: Operating Systems Design and Implementation.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
1 Concurrency: Deadlock and Starvation Chapter 6.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Condition Variables Revisited Copyright ©: University of Illinois CS 241 Staff1.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
CS 241 Section Week #4 (2/19/09). Topics This Section  SMP2 Review  SMP3 Forward  Semaphores  Problems  Recap of Classical Synchronization Problems.
1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
1 Processes Chapter Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
Cpr E 308 Spring 2004 Real-time Scheduling Provide time guarantees Upper bound on response times –Programmer’s job! –Every level of the system Soft versus.
1 Using Semaphores CS 241 March 14, 2012 University of Illinois Slides adapted in part from material accompanying Bryant & O’Hallaron, “Computer Systems:
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Timers and Clocks.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Timers and Clocks.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
CIS Operating Systems Synchronization Professor Qiang Zeng Fall 2015.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Synchronization.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
CE Operating Systems Lecture 8 Process Scheduling continued and an introduction to process synchronisation.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Big Picture Lab 4 Operating Systems C Andras Moritz
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Web Server Architecture Client Main Thread for(j=0;j
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Synchronization Background The Critical-Section Problem Peterson’s.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo
Interprocess Communication Race Conditions
Chapter 6: Process Synchronization
Process Synchronization
Process Synchronization: Semaphores
Background on the need for Synchronization
CIS Operating Systems Synchronization
Process Synchronization
Applied Operating System Concepts -
Chapter 5: Process Synchronization
HW1 and Synchronization & Queuing
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and Process Synchronization
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CSE 451 Section 1/27/2000.
CSE 153 Design of Operating Systems Winter 2019
Presentation transcript:

1 Inter Process Communication & Timers

2 Time.h (page R:Ch9 pp ) #include time_t time(time_t *calptr); Epoch: 00:00 (midnight), Jan 1, 1970 GMT Day is 86,400 seconds time_t is usually a long If the long is 32 bits, time overflows in 2038 Extensions POSIX:XSI microseconds POSIX:TMR nanoseconds

3 Timing a function #include void function_to_time(void); int main(void) { time_t tstart; tstart = time(NULL); function_to_time(); printf(“function_to_time took %f seconds of elapsed time\n”, difftime(time(NULL), tstart)); return(0); }

4 POSIX XSI struct timeval time_t tv_sec; /* seconds since the Epoch*/ time_t tv_usec /* and microsoeconds*/ #include int gettimeofday(struct timeval *restrict tp, void *restrict tzp); tzp is null, historical

5 P307 Measure running time using gettimeofday #include #define MILLION L void function_to_time(void); int main(void) { long timedif; struct timeval tpend; struct timeval tpstart; if (gettimeofday(&tpstart, NULL)) { fprintf(stderr, “Failed to get start time\n”); return 1; }

6 Measure running time using gettimeofday function_to_time(); /* timed code goes here */ if (gettimeofday(&tpend, NULL)) { fprintf(stderr, “Failed to get end time\n”); return 1; } timedif = MILLION*(tpend.tv_sec - tpstart.tv_sec) + tpend.tv_usec – tpstart.tv_usec; printf(“The function_to_time took %ld microseconds\n”, timedif); return 0; }

7 Sleep & nanosleep #include unsigned sleep(unsigned seconds); sleep interacts with SIGALRM #include Int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); Resolution of CLOCK_REALTIME is of order 10ms Does not affect SIGALRM

8 Producer Consumer  Problem occurs in system & application programming e.g. Web Server dispatches incoming web requests to a waiting process(es) e.g. GUI events from keyboard,& mouse are queued by O/S and consumed by applications.

9 Producer-Consumer Problem  Producers insert items  Consumers remove items  Shared bounded buffer Efficient implementation is a circular buffer with an insert and a removal pointer. insertPtr removePtr

10 Challenge  Prevent buffer overflow Producer inserts too many items and the buffer overflows  Prevent buffer underflow Producer inserts an item Consumer removes an item Consumer removes another item  Proper synchronization Mutual Exclusion Progress Bounded wait i.e. Prevent deadlock

11 2 Counting Semaphores and a mutex  Counting semaphore to count # items in buffer  Counting semaphore to count # free slots  Mutex to protect accesses to shared buffer & pointers.

12 Assembling the solution  sem_wait(slots), sem_post(slots)  sem_wait(items), sem_post(items)  mutex_lock(m) mutex_unlock(m)  insertptr=(insertptr+1) % N  removalptr=(removalptr+1) % N  Initialize semaphore slots to size of buffer  Initialize semaphore items to zero.

13 Pseudocode getItem() sem_wait(items) mutex_lock(mutex) result=buffer[ removePtr ]; removePtr=(removePtr +1 ) % N mutex_unlock(mutex) sem_post(slots)

14 Pseudocode putItem(data) sem_wait(slots) mutex_lock(mutex) buffer[ insertPtr]= data; insertPtr=(insertPtr + 1 ) % N mutex_unlock(mutex) sem_post(items)

15 Analysis#1 What's the precise problem? putItem(data) { mutex_lock(mutex) sem_wait(slots) buffer[ insertPtr]= … insertPtr=… sem_post(items) mutex_unlock(mutex) } getItem() { mutex_lock(mutex) sem_wait(items) result=buffer[ removePtr ]; removePtr=… sem_post(slots) mutex_unlock(mutex) }

16 Deadlock e.g Consumer waits for producer to insert a new item but Producer is waiting for Consumer to release mutex putItem(data) { mutex_lock(mutex) #2 sem_wait(slots) buffer[ insertPtr]= … insertPtr=… sem_post(items) mutex_unlock(mutex) } getItem() { mutex_lock(mutex) sem_wait(items) BLOCKS #1 result=buffer[ removePtr ]; removePtr=… sem_post(slots) mutex_unlock(mutex) }

17 Analysis#2 putItem(data) { sem_wait(slots) mutex_lock(mutex) buffer[ insertPtr]= … insertPtr=… sem_post(items) mutex_unlock(mutex) } getItem() { sem_wait(items) sem_post(slots) mutex_lock(mutex) result=buffer[ removePtr ]; removePtr=… mutex_unlock(mutex) }

18 Buffer overflow when reader removes item from a full buffer: Producer inserts item too early putItem(data) { sem_wait(slots) mutex_lock(mutex) buffer[ insertPtr]= … insertPtr=… sem_post(items) mutex_unlock(mutex) } getItem() { sem_wait(items) sem_post(slots) mutex_lock(mutex) result=buffer[ removePtr ]; removePtr=… mutex_unlock(mutex) }

19 First Reader-Writer Problem  readers: read data  writers: write data  Rule: Multiple readers can read the data simultaneously Only one writer can write the data at any time A reader and a writer cannot be in critical section together.  Locking table: whether any two can be in the critical section simultaneously ReaderWriter Reader OKNo WriterNONo

20 First Readers-Writers Problem (Readers have priority) Let processes reading do so concurrently Let processes writing do so one at a time Introduce semaphores Semaphore mutex = 1; Semaphore wrt = 1; int readc = 0; /* reader counter */

21 Writer while (TRUE) { Think_up_data(); /*noncritical section*/ lock(&wrt); do writing unlock(&wrt); } Reader while (TRUE) { lock(&mutex); readcount =readcount+1; if readcount == 1 then lock(&wrt); unlock(&mutex); do reading lock(&mutex) readcount=readcount-1; if readcount == 0 then unlock(&wrt); unlock(&mutex); Use read data }

22 Topics: Hardware/OS Overview  Chapter (Stallings)  Chapter (Stallings)  Chapter 1 (R&R)  Keywords Need to know Processors – registers Interrupts and Interrupt Handling Polling and Programmed I/O Basic Memory principles Kernel mode, user mode Multiprogramming, uni-programming Time sharing Buffer Overflow and security

23 Topics: Processes  Chapter (Stallings)  Chapter 2 and 3 (R&R)  Keywords need to know What is process? What is the difference between process and program? What is the program image layout? Understand argument arrays What does it mean to have a thread-safe function? What is the difference between static and dynamic variables? What are the major process states? What is the difference between dispatcher and scheduler? What is PCB? What happens when process switches from running to ready state? What is process chain, process fan?

24 Topics: Threads  Chapter (R&R)  Chapter 4.1, 4.5 (Stallings)  Keywords need to know What is the difference between processes and threads? What is the difference between user-level threads and kernel-level threads? Detaching and joining threads What happens if you if you call exit(1) in a thread? What is a graceful way to exit a thread without causing process termination?

25 Topics: Concurrency (Mutual Exclusion)  Chapter (R&R)  Chapter (Stallings – and don’t forget Appendix A about the Software Solutions)  Keywords need to know What are the four conditions to provide appropriate synchronization and enter critical region? What is the difference between counting semaphore and mutex? What do sem_wait and sem_post do? How can counting semaphores be implemented using binary semaphores? How can test_and_set be used for synchronization? How can you make a function atomic? Consider increment (i++) and decrement function (i--). How do you ensure that race condition does not occur on the shared variable ‘i’ when two processes use them at the same time?

26 Topics: Thread Synchronization  Chapter (R&R)  Keywords to know What are mutex locks? How do you initialize mutex locks? When would you use mutex instead of counting semaphore? When would you use counting semaphore instead of mutex? Are mutex functions interrupted by signals?

27 Topics: Scheduling  Chapter Scheduling (Stallings)  Keywords need to know Scheduling policies FCFC, SJF, Round Robin, Priority Scheduling Preemptive vs. Non-Preemptive Scheduling Queues in Process management – what is ready queue? How are process states related to process management queues? What is average waiting time? What is the difference between process waiting time and turn-around time?

28 Topics: Signals  Chapter (R&R  Keywords need to know Signal basic concepts – generating signals, blocked, pending signals, delivered signals, ignored signals, … What is signal mask and what are the operations to modify signal mask? What is signal handler? What is the role of sigaction? How do you wait for signals?

29 Topics: Timers  Chapter (R&R)  Keywords need to know Understand what the various time functions are for Gettimeofday Understand the different clock resolutions Sleep function

30 Topics: Classical Sync Problems  Chapter 5.3 and 6.6 (Stallings )  Keywords need to know What is the producer-consumer problem? What are the various semaphores in the producer/consumer solution for? What is the dining philosopher problem? What is the danger of a simple solution for dining philosopher problem?