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.

Slides:



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

Operating Systems Part III: Process Management (Process Synchronization)
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Ch 7 B.
1 CSE 380 Computer Operating Systems Instructor: Insup Lee University of Pennsylvania Fall 2003 Lecture Note 2.6: Message-Based Communication.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
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
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Unix IPC and Synchronization. Pipes and FIFOs Pipe: a circular buffer of fixed size written by one process and read by another int pipe(int fildes[2])
Shared Memory  Creating a Shared Memory Segment Allocated in byte amounts  Shared Memory Operations Create Attach Detach  Shared Memory Control Remove.
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.
Critical Sections and Semaphores A critical section is code that contains access to shared resources that can accessed by multiple processes. Critical.
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.
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);
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
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.
NCHU System & Network Lab Lab 10 Message Queue and Shared Memory.
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,
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
CS162B: Semaphores (and Shared Memory) Jacob T. Chan.
Inter-Process Communication Mechanisms CSE331 Operating Systems Design.
Thread Synchronization with Semaphores
System V IPC Provides three mechanisms for InterProcess Communication (IPC) : Messages : exchange messages with any process or server. Semaphores : allow.
2.3 InterProcess Communication (IPC)
4061 Session 23 (4/10). Today Reader/Writer Locks and Semaphores Lock Files.
1 Concurrency: Mutual Exclusion and Synchronization Module 2.2.
TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.
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.
1 Shared Memory. 2  Introduction  Creating a Shared Memory Segment  Shared Memory Control  Shared Memory Operations  Using a File as Shared Memory.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Semaphores Creating and Accessing Semaphore Sets Semaphore Operations
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /* */
CSNB334 Advanced Operating Systems 4. Concurrency : Mutual Exclusion and Synchronization.
2.3 interprocess communcation (IPC) (especially via shared memory & controlling access to it)
UNIX IPC CSC345.
Inter Process Comunication in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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.
Message Queues. Unix IPC Package ● Unix System V IPC package consists of three things – Messages – allows processes to send formatted data streams to.
January 7, 2003Serguei Mokhov, 1 File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004.
Shared Memory Dr. Yingwu Zhu. Overview System V shared memory Let multiple processes attach a segment of physical memory to their virtual address spaces,
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
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.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Textbook: Advanced Programming in the UNIX Environment, 2 nd Edition, W. Richard Stevens and Stephen A. Rago 1 Chapter 15. Interprocess Communication System.
Operating Systems Inter-Process Communication Shared Memory & Semaphores Moti Geva
Process Synchronization: Semaphores
CSNB334 Advanced Operating Systems 4
Threads Threads.
Chapter 5: Process Synchronization
Unix IPC Unix has three major IPC constructs to facilitate interaction between processes: Message Queues (this PowerPoint document) permit exchange of.
Shared Memory Dr. Yingwu Zhu.
PTHREADS AND SEMAPHORES
Unix programming Term: Unit-V I PPT Slides
Lecture 2 Part 2 Process Synchronization
Concurrency: Mutual Exclusion and Process Synchronization
| Website for Students | VTU -NOTES -Question Papers
Chapter 6: Synchronization Tools
Shared Memory Dr. Yingwu Zhu Feb, 2007.
Presentation transcript:

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 the data does not need to be copied between the client and server The only trick in using shared memory is synchronizing access to a given region among multiple processes -- if the server is placing data into a shared memory region, the client shouldn’t try to access it until the server is done Often, semaphores are used to synchronize shared memory access ( … semaphores will be covered a few lectures from now) not covered in Wang, lookup in Stevens (APUE)

S -3 shmget() shmget() is used to obtain a shared memory identifier: #include int shmget( key_t key, int size, int flag ); shmget() returns a shared memory ID if OK, -1 on error key is typically the constant “ IPC_PRIVATE ”, which lets the kernel choose a new key -- keys are non-negative integer identifiers, but unlike fds they are system-wide, and their value continually increases to a maximum value, where it then wraps around to zero size is the size of the shared memory segment, in bytes flag can be “ SHM_R ”, “ SHM_W ”, or “ SHM_R|SHM_W ”

S -4 shmat() Once a shared memory segment has been created, a process attaches it to its address space by calling shmat() : void *shmat( int shmid, void *addr, int flag ); shmat() returns pointer to shared memory segment if OK, -1 on error The recommended technique is to set addr and flag to zero, i.e.: char *buf = (char *) shmat( shmid, 0, 0 ); The UNIX commands “ ipcs ” and “ ipcrm ” are used to list and remove shared memory segments on the current machine The default action is for a shared memory segments to remain in the system even after the process dies -- a better technique is to use shmctl() to set up a shared memory segment to remove itself once the process dies ( … see next slide)

S -5 shmctl() shmctl() performs various shared memory operations: int shmctl( int shmid, int cmd, struct shmid_ds *buf ); cmd can be one of IPC_STAT, IPC_SET, or IPC_RMID : –IPC_STAT fills the buf data structure (see ) –IPC_SET can change the uid, gid, and mode of the shmid –IPC_RMID sets up the shared memory segment to be removed from the system once the last process using the segment terminates or detached from it — a process detaches a shared memory segment using shmdt( void *addr ), which is similar to free() shmctl() returns 0 if OK, -1 on error

S -6 Shared Memory Example char *ShareMalloc( int size ) { int shmId; char *returnPtr; if( (shmId=shmget( IPC_PRIVATE, size, (SHM_R|SHM_W) )) < 0 ) Abort( "Failure on shmget {size is %d}\n", size ); if( (returnPtr=(char*) shmat( shmId, 0, 0 )) == (void*) -1 ) Abort( "Failure on Shared Mem (shmat)" ); shmctl( shmId, IPC_RMID, (struct shmid_ds *) NULL ); return( returnPtr ); }

S -7 mmap() An alternative to shared memory is memory mapped i/o, which maps a file on disk into a buffer in memory, so that when bytes are fetched from the buffer the corresponding bytes of the file are read One advantage is that the contents of files are non-volatile Usage: caddr_t mmap( caddr_t addr, size_t len, int prot, int flag, int filedes, off_t off ); –addr and off should be set to zero, –len is the number of bytes to allocate –prot is the file protection, typically ( PROT_READ|PROT_WRITE ) –flag should be set to MAP_SHARED to emulate shared memory –filedes is a file descriptor that should be opened previously

S -8 Memory Mapped I/O Example char *ShareMalloc( int size ) { int fd; char *returnPtr; if( (fd = open( "/tmp/mmap", O_CREAT | O_RDWR, 0666 )) < 0 ) Abort( "Failure on open" ); if( lseek( fd, size-1, SEEK_SET ) == -1 ) Abort( "Failure on lseek" ); if( write( fd, "", 1 ) != 1 ) Abort( "Failure on write" ); if( (returnPtr = (char *) mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0 )) == (caddr_t) -1 ) Abort( "Failure on mmap" ); return( returnPtr ); }

S -9 Semaphores

S -10 Motivation Programs that manage shared resources must execute portions of code called critical sections in a mutually exclusive manner. A common method of protecting critical sections is to use semaphores Code that modifies shared data usually has the following parts: Entry Section: The code that requests permission to modify the shared data. Critical Section: The code that modifies the shared variable. Exit Section: The code that releases access to the shared data. Remainder Section: The remaining code.

S -11 The Critical Section Problem The critical section problem refers to the problem of executing critical sections in a fair, symmetric manner. Solutions to the critical section problem must satisfy each of the following: Mutual Exclusion: At most one process is in its critical section at any time. Progress: If no process is executing its critical section, a process that wishes to enter can get in. Bounded Waiting: No process is postponed indefinitely. An atomic operation is an operation that, once started, completes in a logical indivisible way. Most solutions to the critical section problem rely on the existence of certain atomic operations

S -12 Semaphores A semaphore is an integer variable with two atomic operations: wait and signal. Other names for wait are down, P, and lock. Other names for signal are up, V, unlock, and post. A process that executes a wait on a semaphore variable S cannot proceed until the value of S is positive. It then decrements the value of S. The signal operation increments the value of the semaphore variable. Some (flawed) pseudocode: void wait( int *s ) void signal( int *s ) { { while( *s <= 0 ) ; (*s)++; (*s)--; } }

S -13 Semaphores (cont.) Three problems with the previous slide’s wait() and signal() : –busy waiting is inefficient –doesn’t guarantee bounded waiting –“ ++ ” and “ -- ” operations aren’t necessarily atomic! Solution: use system calls semget() and semop() (… see next slide) The following pseudocode protects a critical section: wait( &s ); /* critical section */ signal( &s ); /* remainder section */ What happens if S is initially 0? What happens if S is initially 8?

S -14 semget() Usage: #include int semget( key_t key, int nsems, int semflg ); Creates a semaphore set and initializes each element to zero Example: int semID = semget( IPC_PRIVATE, 1, S_IRUSR | S_IWUSR ); Like shared memory, icps and ipcrm can list and remove semaphores

S -15 semop() Usage: int semop( int semid, struct sembuf *sops, int nsops ); Increment, decrement, or test semaphores elements for a zero value. From : sops->sem_num, sops->sem_op, sops->sem_flg; If sem_op is positive, semop() adds value to semaphore element and awakens processes waiting for the element to increase if sem_op is negative, semop() adds the value to the semaphore element and if < 0, semop() sets to 0 and blocks until it increases if sem_op is zero and the semaphore element value is not zero, semop() blocks the calling process until the value becomes zero if semop() is interrupted by a signal, it returns -1 with errno = EINTR

S -16 Example struct sembuf semWait[1] = { 0, -1, 0 }, semSignal[1] = { 0, 1, 0 }; int semID; semop( semID, semSignal, 1 ); /* init to 1 */ while( (semop( semID, semWait, 1 ) == -1) && (errno == EINTR) ) ; { /* critical section */ } while( (semop( semID, semSignal, 1 ) == -1) && (errno == EINTR) ) ;