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.

Slides:



Advertisements
Similar presentations
Florida State UniversityCOP5570 – Advanced Unix Programming IPC mechanisms Pipes Sockets System V IPC –Message Queues –Semaphores –Shared Memory.
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Ch 7 B.
Chapter 6: Process Synchronization
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.
02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying.
Monitors A high-level abstraction that provides a convenient and effective mechanism for process synchronization Only one process may be active within.
Operating Systems Unit 3: – Concurrent execution mutual exclusion – Concurrent programming semaphore monitor Operating Systems.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
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);
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.
Monitors CSCI 444/544 Operating Systems Fall 2008.
02/25/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Computer Science 162 Discussion Section Week 3. Agenda Project 1 released! Locks, Semaphores, and condition variables Producer-consumer – Example (locks,
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.
CS162B: Semaphores (and Shared Memory) Jacob T. Chan.
1 Chapter 6 Interprocess Communications. 2 Contents u Introduction u Universal IPC Facilities u System V IPC.
Inter-Process Communication Mechanisms CSE331 Operating Systems Design.
Monitors High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes. monitor monitor-name {
Thread Synchronization with Semaphores
The Third Assignment COSC 4330/6310 Spring Implementing delays To be able to test the semaphores, we must run the program in real time To be able.
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.
Synchronization II: CPE Operating Systems
5.1 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Problems with Semaphores Incorrect use of semaphore operations:
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
1 Announcements The fixing the bug part of Lab 4’s assignment 2 is now considered extra credit. Comments for the code should be on the parts you wrote.
Florida State UniversityCOP5570 – Advanced Unix Programming Today’s topics System V Interprocess communication (IPC) mechanisms –Message Queues –Semaphores.
1 Semaphores Chapter 7 from Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date: January.
Semaphores Creating and Accessing Semaphore Sets Semaphore Operations
IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /* */
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
2.3 interprocess communcation (IPC) (especially via shared memory & controlling access to it)
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Synchronization.
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:
© 2006 RightNow Technologies, Inc. Synchronization September 15, 2006 These people do not actually work at RightNow.
Advanced UNIX IPC Facilities After Haviland, et al.’s book.
Slide 9-1 Copyright © 2004 Pearson Education, Inc. Inter process Communication Mechanisms  Allow arbitrary processes to exchange data and synchronize.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Message Queues. Unix IPC Package ● Unix System V IPC package consists of three things – Messages – allows processes to send formatted data streams to.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
Shared Memory Dr. Yingwu Zhu. Overview System V shared memory Let multiple processes attach a segment of physical memory to their virtual address spaces,
Operating Systems Unit 4: – Dining Philosophers – Deadlock – Indefinite postponement Operating Systems.
Operating Systems Inter-Process Communication Shared Memory & Semaphores Moti Geva
Deadlock and Starvation
CS703 – Advanced Operating Systems
Chapter 5: Process Synchronization – Part 3
Deadlock and Starvation
Chapter 6-7: Process Synchronization
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Shared Memory Dr. Yingwu Zhu.
Lecture 25 Syed Mansoor Sarwar
PTHREADS AND SEMAPHORES
Module 7a: Classic Synchronization
Chapter 7: Synchronization Examples
| Website for Students | VTU -NOTES -Question Papers
More IPC B.Ramamurthy 4/15/2019.
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
Lecture 26 Syed Mansoor Sarwar
CSE 153 Design of Operating Systems Winter 19
Shared Memory Dr. Yingwu Zhu Feb, 2007.
Presentation transcript:

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

2 Dining Philosophers Example monitor dp { enum {thinking, hungry, eating} state[5]; condition self[5]; void pickup(int i) void putdown(int i) void test(int i) void init() { for (int i = 0; i < 5; i++) state[i] = thinking; }

3 Dining Philosophers void pickup(int i) { state[i] = hungry; test[i]; if (state[i] != eating) self[i].wait(); } void putdown(int i) { state[i] = thinking; // test left and right neighbors test((i+4) % 5); test((i+1) % 5); }

4 Dining Philosophers void test(int i) { if ( (state[(I + 4) % 5] != eating) && (state[i] == hungry) && (state[(i + 1) % 5] != eating)) { state[i] = eating; self[i].signal(); }

5 Monitors §Example motorbike …

6 §Low level atomic operations l interrupt disable l Test&Set l Compare&Swap §High level atomic operations l mutex l semaphores l monitors

7 Three primary mechanisms used for IPC in POSIX §Message queues §Semaphores §Shared memory

8 Derived from Unix System V §These three resource types are global and handled by the system. §Access permissions §May outlive the process that created it. §Programmer must take care of freeing these resources.

9 shmget system call shm_id=shmget(200, 2048, IPC_CREAT | 0600); Allocate a shared memory segment of 2048 bytes accessible only to current user.

10 §0400 user may read from this resource §0200 user may write to this resource §0040? §0002?

11 ftok() system call - generate an IPC key key_t ftok(const char *path, int id); §The ftok() function returns a key based on path and id The path argument must be the pathname of an existing file.

12 ftok() system call - generate an IPC key key_t set_key; key = ftok("/usr/local/dir1/abc", 4); if (key == -1) { perror("ftok: "); exit(1); }

13 Shared Memory int* SharedMemHandle(int shkey){ int* shm_addr; int shm_id; shm_id=shmget(shkey, 2048, IPC_CREAT | 0600); if(shm_id == -1){ perror("shmget: "); exit(1); } shm_addr=shmat(shm_id, NULL, 0); return shm_addr; }

14 Shared Memory int* counter1, *counter2; int* shm; shm=SharedMemHandle(SHMEM_KEY); counter1 = shm; counter2 = shm+sizeof(int); // memory management by programmer (*counter1)=0; //and so-on

15 Shared Memory int memid=shmget(SHMEM_KEY, 2048, 0600); int memdel=shmctl(memid, IPC_RMID, NULL); printf("%d memory delete status\n", memdel);

16 Semaphores int sem_set_id = semget(sem_key, 1, IPC_CREAT | 0600); printf("%d\n", sem_set_id); if (sem_set_id==-1) { fprintf(stderr, "semget error"); perror(""); exit(1); }

17 Semaphores int rc = semctl(sem_set_id, 0, SETVAL, 1); if (rc == -1) { fprintf(stderr, "semctl: "); perror(""); exit(1); }

18 Semaphores union semun sem_val; sem_val.val =1; int rc = semctl(sem_set_id, 0, SETVAL, sem_val); if (rc == -1) { fprintf(stderr, "semctl: "); perror(""); exit(1); } union semun {int val; /* value for SETVAL */ struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */ u_short *array; /* array for GETALL & SETALL */ };

19 Semaphores int gv = semctl(sem_set_id, 0, GETVAL, NULL); printf("value of sem_set_id is %d\n", gv); }

20 Semaphores int semdel, semid; semid=semget(sem_key, 1, IPC_CREAT | 0600); semdel=semctl(semid, 0, IPC_RMID); printf("%d semaphore delete status\n", semdel);

21 Semaphores int SemGetID(int which){ int sem_set_id; sem_id = semget(which, 1, 0600); if (sem_set_id == -1) { fprintf(stderr, "semget:"); perror(""); exit(1); return sem_id; }

22 Semaphores void waitSem(int which){ int sem_id; struct sembuf sem_op; sem_id = SemGetID(which); printf("execute wait for sem_id %d\n", sem_id); sem_op.sem_num = 0; sem_op.sem_op = -1; sem_op.sem_flg = 0; semop(sem_id, &sem_op, 1); }

23 Semaphores void signalSem(int which){ int sem_id; struct sembuf sem_op; sem_id = SemGetID(which); printf("execute signal for sem_id %d\n", sem_id); sem_op.sem_num = 0; sem_op.sem_op = 1; sem_op.sem_flg = 0; semop(sem_id, &sem_op, 1); }

24 Process p1.c int main(void){ int i; int* counter1, *counter2; int* shm; shm=SharedMemHandle(SHMEM_KEY); counter1 = shm; counter2 = shm+sizeof(int); //initialize;

25 Semaphores for (i=0; i<5; i++){ waitSem(sem_key); (*counter1)++; (*counter2)++; printf("Process 1 increments counter1 %d and counter2 %d\n", *counter1, *counter2); signalSem(sem_key); sleep(2); } //deallocate resources }

26 §ipcs l Prints information about active shared memory segments, message queues and semaphores. §ipcrm l remove a message queue, semaphore set, or shared memory ID

27 §Solaris implements two flavors of mutex locks, adaptive and spin. §When a kernel code segment attempts to acquire a mutex lock, and the lock is being held, the thread can do one of two things: spin or block. l Spinning on a mutex means simply executing a tight loop, with lock acquisition attempted in each pass through the loop. l Blocking means putting the thread to sleep.

28 §Spinning has the advantage of not requiring a context switch off the processor, such that, once the lock is acquired, execution continues. The downside is that a processor is consumed during the spin. §Blocking frees up the processor, as the blocked thread is put to a sleep state and context switched off the processor, freeing it up for other work. §It does, however, require the overhead of context switching the thread back in once the lock is available.

29 Adaptive mutex locks §They deal with the above choices in a dynamic fashion.

30 Adaptive mutex locks §If a mutex lock is currently owned by another thread when a kernel thread attempts to acquire the lock, the state of the thread is examined. §If the thread holding the lock is running, the kernel thread trying to get the lock will spin, based on the assumption that the running thread will release the lock in relatively short order.

31 Adaptive mutex locks §If the holder is sleeping, then the thread wanting the lock will also block (sleep), because it doesn't make sense to consume a processor spinning and waiting for the holder to wake up and release the lock.