© 2006 RightNow Technologies, Inc. Synchronization September 15, 2006 These people do not actually work at RightNow.

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

Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: 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.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Interprocess Communication
Readers and Writers An introduction to the Linux programming interface for using UNIX semaphores.
XSI IPC Message Queues Semaphores Shared Memory. XSI IPC Each XSI IPC structure has two ways to identify it An internal (within the Kernel) non negative.
System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.
Synchronization Principles Gordon College Stephen Brinton.
1 Tuesday, June 27, 2006 "If the 8086 architects had designed a car, they would have produced one with legs, to be compatible with the horse." - Anonymous.
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.
© 2006 RightNow Technologies, Inc. Tribute to Synchronization Jeff Elser December 11 th, 2006.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Motivation for ‘and’ Syncronization *A = 1; *B = 1; Process 1Process 2Process 3 wait(&A);wait(&A);wait(&B); wait(&B); signal(&A); signal(&B); signal(&B);
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
1 CS 333 Introduction to Operating Systems Class 4 – Synchronization Primitives Semaphores Jonathan Walpole Computer Science Portland State University.
UNIX IPC CSE 121 Spring 2003 Keith Marzullo. CSE 121 Spring 2003Review of Concurrency2 Creating a UNIX process A process is created by making an exact.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
1 Chapter 6 Interprocess Communications. 2 Contents u Introduction u Universal IPC Facilities u System V IPC.
Thread Synchronization with Semaphores
S -1 Shared Memory. S -2 Motivation Shared memory allows two or more processes to share a given region of memory -- this is the fastest form of IPC because.
System V IPC Provides three mechanisms for InterProcess Communication (IPC) : Messages : exchange messages with any process or server. Semaphores : allow.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
1 Semaphores Chapter 7 from Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date: January.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Semaphores Creating and Accessing Semaphore Sets Semaphore Operations
2.3 interprocess communcation (IPC) (especially via shared memory & controlling access to it)
Inter Process Comunication in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
Semaphores Chapter 7 from Inter-process Communications in Linux:
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Interprocess Communication. Resource Sharing –Kernel: Data structures, Buffers –Processes: Shared Memory, Files Synchronization Methods –Kernel: Wait.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
Operating Systems Inter-Process Communication Shared Memory & Semaphores Moti Geva
Process Synchronization
Chapter 5: Process Synchronization
Background on the need for Synchronization
Chapter 5: Process Synchronization
Linux Interprocess Communication
Interprocess Communication-1
Topic 6 (Textbook - Chapter 5) Process Synchronization
Inter Process Communication
Module 7a: Classic Synchronization
Unix programming Term: Unit-V I PPT Slides
Lecture 2 Part 2 Process Synchronization
Critical section problem
Chapter 7: Synchronization Examples
| Website for Students | VTU -NOTES -Question Papers
CSE 153 Design of Operating Systems Winter 19
Chapter 6: Synchronization Tools
Process/Thread Synchronization (Part 2)
Presentation transcript:

© 2006 RightNow Technologies, Inc. Synchronization September 15, 2006 These people do not actually work at RightNow.

Outline Historical solutions Synchronization Primitives Brief word on concurrency Semaphores in depth –semid_ds –sem –Working with semaphores References

Peterson's Algorithm boolean lockA = lockB = false; int turn = A; // or B //Thread A: lockA = true; turn = B; while( lockB && turn != A ) { } lockA = false; //Thread B: lockB = true; turn = A; while( lockA && turn != B ) { } lockB = false;

Dekker's Algorithm boolean lockA = lockB = false; int turn = A; // or B //Thread A: lockA = true; while(lockB) { if (turn != A) { lockA = false; while(turn != A){} lockA = true; } lockA = false; turn = B; //Thread B: lockB = true; while(lockA) { if (turn != B) { lockB = false; while(turn != B){} lockB = true; } lockB = false; turn = A;

Synchronization Primitives Per-CPU variables Atomic operation Memory barrier Spin lock Seqlocks Read-copy-update Semaphore Local interrupt disabling Local softirq disabling

Synchronization Primitives – cont. Per-CPU Variables –Array of data structures that can be accessed by only 1 CPU –Only useful when data can be split across CPUs –Fails if kernel preemption is enabled (race conditions) Atomic operation –Read-modify-write oatomic_read(v) oatomic_set(v, i) – (test & set) oatomic_add(i, v) –Another set operates on bit masks

Synchronization Primitives – cont. Memory barrier –Avoids code optimization problems by creating a barrier that ensures that all code before the barrier gets executed before code after the barrier Spin locks –Busy wait while blocked –Useful for multi-processor systems when the critical code will execute very fast o1 = unlocked, 0 = locked –Read/Write Spin Locks

Synchronization Primitives – cont. Seqlocks –Read/Write spin locks that give high priority to writers Read-Copy Update –Allows many readers and writers –No locks or counters –Writer just copies the old data, updates it and changes the pointer –Old data cannot be freed until all readers are finished

Synchronization Primitives – cont. Semaphores –Kernel and System V IPC (User) –count oIf count > 0 the resource is free ocount = 0 means the resource is 100% utilized but the wait queue is empty oIf count < 0 the resource is 100% utilized and there are processes in the wait queue –wait – pointer to the wait queue –sleepers – flag that is set if there are processes that are waiting blocked –More to come!

Synchronization Primitives – cont. Local Interrupt Disabling –Creates a critical section in the kernel so that even hardware interrupts won’t break the execution of a set of statements –CPU specific Local softirq disabling –Disable deferrable functions w/o disabling interrupts –Increment the softirq counter to disable; decrement the softirq counter to enable

++ Concurrency Maximize the number of I/O devices operating –Disable interrupts for very short periods Maximize the number of productive CPUs –Avoid using spin locks if possible

More on Semaphores (semid_ds) /* One semid data structure for each set of semaphores in the system. */ struct semid_ds { struct ipc_perm sem_perm; /* permissions.. see ipc.h */ time_t sem_otime; /* last semop time */ time_t sem_ctime; /* last change time */ struct sem *sem_base; /* ptr to first semaphore in array */ struct wait_queue *eventn; struct wait_queue *eventz; struct sem_undo *undo; /* undo requests on this array */ ushort sem_nsems; /* no. of semaphores in array */ }; sem_perm –This is an instance of the ipc_perm structure, which is defined for us in linux/ipc.h. This holds the permission information for the semaphore set, including the access permissions, and information about the creator of the set (uid, etc). sem_otime –Time of the last semop() operation sem_ctime –Time of the last change to this structure (mode change, etc) sem_base –Pointer to the first semaphore in the array (see next structure) sem_undo –Number of undo requests in this array sem_nsems –Number of semaphores in the semaphore set (the array)

More on Semaphores (sem) /* One semaphore structure for each semaphore in the system. */ struct sem { short sempid; /* pid of last operation */ ushort semval; /* current value */ ushort semncnt; /* num procs awaiting increase in semval */ ushort semzcnt; /* num procs awaiting semval = 0 */ }; sem_pid –The PID (process ID) that performed the last operation sem_semval –The current value of the semaphore sem_semncnt –Number of processes waiting for resources to become available sem_semzcnt –Number of processes waiting for 100% resource utilization

Working with Semaphores int semget ( key_t key, int nsems, int semflg ); int semop ( int semid, struct sembuf *sops, unsigned nsops); struct sembuf { ushort sem_num; /* semaphore index in array */ short sem_op; /* semaphore operation */ short sem_flg; /* operation flags */ };

Working with Semaphores – cont. int semctl ( int semid, int semnum, int cmd, union semun arg ); union semun { int val; /* value for SETVAL */ struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */ ushort *array; /* array for GETALL & SETALL */ struct seminfo *__buf; /* buffer for IPC_INFO */ void *__pad; };

Semaphore Commands IPC_STAT –Retrieves the semid_ds structure for a set, and stores it in the address of the buf argument in the semun union. IPC_SET –Sets the value of the ipc_perm member of the semid_ds structure for a set. Takes the values from the buf argument of the semun union. IPC_RMID –Removes the set from the kernel. GETALL –Used to obtain the values of all semaphores in a set. The integer values are stored in an array of unsigned short integers pointed to by the array member of the union. GETNCNT –Returns the number of processes currently waiting for resources. GETPID –Returns the PID of the process which performed the last semop call. GETVAL –Returns the value of a single semaphore within the set. GETZCNT –Returns the number of processes currently waiting for 100% resource utilization. SETALL –Sets all semaphore values with a set to the matching values contained in the array member of the union. SETVAL –Sets the value of an individual semaphore within the set to the val member of the union.

References D. Bovet and M. Cesati. Understanding the Linux Kernel, Third Edition.O'Reilly and Associates, Inc., The Linux Documentation Project: R. Kline. Peterson's Algorithm,