Operating Systems Inter-Process Communication Shared Memory & Semaphores Moti Geva

Slides:



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

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.
Shared memory. Process A Process B Physical Memory Virtual address space A Virtual address space B Share Instruction memory Data Instruction memory Data.
Inter-process communication (IPC) using Shared Memory & Named Pipes CSE 5520/4520 Wireless Networks.
System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.
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.
© 2006 RightNow Technologies, Inc. Tribute to Synchronization Jeff Elser December 11 th, 2006.
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.
Unix IPC Unix has three major IPC constructs to facilitate interaction between processes: Message Queues (this PowerPoint document) permit exchange of.
CS162B: Semaphores (and Shared Memory) Jacob T. Chan.
System Config IPC Iris Zhu
1 Chapter 6 Interprocess Communications. 2 Contents u Introduction u Universal IPC Facilities u System V IPC.
Lecture 6 Introduction to Distributed Programming System V IPC: Message Queues, Shared Memory, Semaphores.
Inter-Process Communication Mechanisms CSE331 Operating Systems Design.
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.
2.3 InterProcess Communication (IPC)
TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.
Lecture 7 Introduction to Distributed Programming System V IPC: Message Queues, Shared Memory, Semaphores.
Florida State UniversityCOP5570 – Advanced Unix Programming Today’s topics System V Interprocess communication (IPC) mechanisms –Message Queues –Semaphores.
Inter-process Communication:
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.
Semaphores Creating and Accessing Semaphore Sets Semaphore Operations
Interprocess Communication Bosky Agarwal CS 518. Presentation Layout Introduction Pipes FIFOs System V IPC 1.Using pipes 2.Working of pipes 3.Pipe Data.
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)
File descriptor table File descriptor (integer)File name 0stdin 1stdout 2stderr Use open(), read(), write() system calls to access files Think what happens.
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.
Slide 9-1 Copyright © 2004 Pearson Education, Inc. Inter process Communication Mechanisms  Allow arbitrary processes to exchange data and synchronize.
Interprocess Communication
Message Queues. Unix IPC Package ● Unix System V IPC package consists of three things – Messages – allows processes to send formatted data streams to.
Interprocess Communication. Resource Sharing –Kernel: Data structures, Buffers –Processes: Shared Memory, Files Synchronization Methods –Kernel: Wait.
Shared Memory Dr. Yingwu Zhu. Overview System V shared memory Let multiple processes attach a segment of physical memory to their virtual address spaces,
Textbook: Advanced Programming in the UNIX Environment, 2 nd Edition, W. Richard Stevens and Stephen A. Rago 1 Chapter 15. Interprocess Communication System.
Distributed and Parallel Processing George Wells.
Linux/UNIX Programming APUE (Interprocess Communication)
C Threads and Semaphores
Lecture 7 Introduction to Distributed Programming System V IPC:
Message queue Inter process communication primitive
CSNB334 Advanced Operating Systems 4
Using Processes.
Linux Interprocess Communication
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.
Making Virtual Memory Real: The Linux-x86-64 way
Interprocess Communication-1
Hwajung Lee ITEC452 Distributed Computing Lecture 7 Interprocess Communication (IPC): An Overview.
Interprocess Communication and Synchronization using System V IPC
Hwajung Lee ITEC452 Distributed Computing Lecture 2 Interprocess Communication (IPC): An Overview.
Message Queues.
PTHREADS AND SEMAPHORES
Inter Process Communication
Unix programming Term: Unit-V I PPT Slides
Inter-Process Communication
Virtual Memory.
Introduction to Distributed Programming System V IPC:
Virtual Memory: Systems CSCI 380: Operating Systems
| Website for Students | VTU -NOTES -Question Papers
Chapter 2 Processes and Threads 2.1 Processes 2.2 Threads
Shared Memory Dr. Yingwu Zhu Feb, 2007.
Presentation transcript:

Operating Systems Inter-Process Communication Shared Memory & Semaphores Moti Geva

Types of IPC Pipes/Named pipes Signals Shared memory Semaphores Messages Sockets

Fast IPC How can we make IPC faster? All IPC shown until now use system calls to deliver info How can we reduce the number of system calls? We can use memory segments... But we still need the OS for that

Mapping shared memory In order to map a memory segment in two processes (or more) we need to ask the OS to do so How can we recognize that two processes want the same page? Should be something like ‘named pipes’ shouldn’t it?

Mapping shared memory(2) Using the file system is one way to do so using mmap() system call (discussed later...) How else can we identify the same page? A unique ID sounds nice, doesn’t it? There are three system’s IPC objects that use an ID for sharing Shared memory Semaphores Message queues

Page based memory

Example...

A more specific example

In short A process owns a set of virtual pages It can only access those pages allocated to it by the OS A process cannot access any physical pages directly – only using OS mapped pages So all we need is that the OS will map the same physical page to two (or more) processes

Some questions Does the virtual pages has to be the same? What happens if the allocation is smaller than one page? What happens if the allocation is bigger than one page? Does the physical memory has to be continuous?

Working with shared memory There are 4 stages to be executed while using shared memory Notify OS we want a shared memory object Map the object to the process’ memory (virtual) Unmap the memory from the process’ memory Delete the shared memory object Important: IPC objects are never deleted unless explicitly deleted or the computer is rebooted

shmget - allocates a shared memory segment SYNOPSIS #include sys/ipc.h #include sys/shm.h int shmget(key_t key, size_t size, int shmflg); DESCRIPTION shmget() returns the identifier of the shared memory segment associated with the value of the argument key. A new shared memory segment, with size equal to the value of size rounded up to a multiple of PAGE_SIZE, is created if key has the value IPC_PRIVATE or key isn't IPC_PRIVATE, no shared memory segment corresponding to key exists, and IPC_CREAT is asserted in shmflg (i.e. shmflg&IPC_CREAT isn't zero). In practice

Shared memory flags (shmflg) The value shmflg is composed of: IPC_CREAT to create a new segment. If this flag is not used, then shmget() will find the segment associated with key and check to see if the user has permission to access the segment. IPC_EXCL used with IPC_CREAT to ensure failure if the segment already exists. mode_flags (lowest 9 bits) specifying the permissions granted to the owner, group, and world. Presently, the execute permissions are not used by the system.

In practice(2) shmat – attching a shared memory segment to a process SYNOPSIS #include sys/types.h #include sys/shm.h void *shmat(int shmid, const void *shmaddr, int shmflg); DESCRIPTION shmid – segment id prevousy allocated by shmget shmaddr – if is NULL, the system chooses a suitable (unused) address at which to attach the segment. Otherwise, it is used as the (virtual) address for attachment

Flags shmflg SHM_RND - the address equal to shmaddr rounded down to the nearest multiple of SHMLBA. Otherwise shmaddr must be a page-aligned address at which the attach occurs. SHM_RDONLY - segment is attached for reading. Otherwise the segment is attached for read and write

In practice(3) shmdt – detaches a previously attached shared memory SYNOPSIS #include sys/types.h #include sys/shm.h int shmdt(const void *shmaddr);

In practice(4) shmctl - shared memory control SYNOPSIS #include sys/ipc.h #include sys/shm.h int shmctl(int shmid, int cmd, struct shmid_ds *buf); DESCRIPTION shmctl() allows the user to receive information on a shared memory segment, set the owner, group, and permissions of a shared memory segment, or destroy a segment.

The shmctl commands IPC_STAT is used to copy the information about the shared memory segment into the buffer buf. IPC_SET is used to apply the changes the user has made to the uid, gid, or mode members of the shm_perms field. Only the lowest 9 bits of mode are used. The shm_ctime member is also updated. The user must be the owner, creator, or the super-user. IPC_RMID is used to mark the segment as destroyed. It will actually be destroyed after the last detach. The user must be the owner, creator, or the super-user. The user must ensure that a segment is eventually destroyed; otherwise its pages that were faulted in will remain in memory or swap.

struct shmid_ds struct shmid_ds { struct ipc_perm shm_perm; /* operation perms */ int shm_segsz; /* size of segment (bytes) */ time_t shm_atime; /* last attach time */ time_t shm_dtime; /* last detach time */ time_t shm_ctime; /* last change time */ unsigned short shm_cpid; /* pid of creator */ unsigned short shm_lpid; /* pid of last operator */ short shm_nattch; /* no. of current attaches */... }; struct ipc_perm { key_t key; ushort uid; /* owner euid and egid */ ushort gid; ushort cuid; /* creator euid and egid */ ushort cgid; ushort mode; /* lower 9 bits of access modes */ ushort seq; /* sequence number */ };

Father-son relationship fork() After a fork() the child inherits the attached shared memory segments. exec() After an exec() all attached shared memory segments are detached (not destroyed). exit() Upon exit() all attached shared memory segments are detached (not destroyed).

$ cat ipc2.c #include char str[]="shalom"; main() { int s; if ( fork()==0 ) strcpy ( str, "hello" ); else { wait ( &s ); printf ( "%s\n", str ); } $ gcc -o ipc2{,.c} $ ipc2 shalom Without shared memory!!!

$ cat ipc3.c #include void cleanup(); char str[]="shalom"; char *sharestr; int shmid; main() { int s,i; shmid=shmget ( IPC_PRIVATE, strlen ( str ) + 1, 0600 ); sharestr=(char *)shmat ( shmid, 0, 0600 ); for ( i=1 ; i < 19 ; i++ ) signal ( i, &cleanup ); strcpy ( sharestr, str ); if ( fork()==0 ) strcpy ( sharestr, "hello" ); else { wait ( &s ); printf ( "%s\n", sharestr ); shmdt ( sharestr ); shmctl ( shmid, IPC_RMID, 0 ); } void cleanup() { shmdt ( sharestr ); shmctl ( shmid, IPC_RMID, 0 ); exit ( 1 ); } $ gcc -o ipc3{,.c} $ ipc3 hello I II III

Critical section How can we prevent one process accessing data while another process updates it? The well known bank example... What atomic functions do we have?

Semaphores Semaphore are the basic UNIX synchronization tool Semaphores have a counter and a queue (for waiting processes) Semaphores have 2 basic operations wait signal

Working with semaphores There are 4 stages to be executed while using shared memory Let the OS we want a semaphore object Initialize the semaphore Execute signal/wait operation on the semaphore Delete the semaphore object Important (in case you forgot): IPC objects are never deleted unless explicitly deleted or the computer is rebooted

In practice semget - get a semaphore set identifier SYNOPSIS #include sys/types.h #include sys/ipc.h #include sys/sem.h int semget(key_t key, int nsems, int semflg); DESCRIPTION This function returns the semaphore set identifier associated with the argument key. A new set of nsems semaphores is created if key has the value IPC_PRIVATE or if no existing semaphore set is associated to key and IPC_CREAT is asserted in semflg

In practice(2) semctl - semaphore control operations SYNOPSIS #include sys/types.h #include sys/ipc.h #include sys/sem.h int semctl(int semid, int semnum, int cmd,...); DESCRIPTION The function semctl performs the control operation specified by cmd on the semaphore set identified by semid, or on the semnum-th semaphore of that set. (Semaphores are numbered starting at 0.) This function has three or four arguments

The forth argument #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED) /* union semun is defined by including */ #else /* according to X/OPEN we have to define it ourselves */ union semun { int val; /* value for SETVAL */ struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */ unsigned short *array; /* array for GETALL, SETALL */ /* Linux specific part: */ struct seminfo *__buf; /* buffer for IPC_INFO */ }; #endif

Available commands IPC_STAT – copy data to arg.buf IPC_SET – IPC_RMID – mark for deletion GETALL – Return semval for all semaphores GETNCNT – get number of processes waiting for signal GETPID – who executed last op on semaphore? GETVAL – get the semaphore counter value GETZCNT - the number of processes waiting for semval to become 0 SETALL – setval for all values SETVAL - set the semaphore counter value

In practice(3) semop - semaphore operations SYNOPSIS #include sys/types.h #include sys/ipc.h #include sys/sem.h int semop(int semid, struct sembuf *sops, unsigned nsops); Description semid – semaphore set identifier sops – array of operations (described in the next slide) nsops – number of sops

sembuf unsigned short sem_num; /* semaphore number */ short sem_op; /* semaphore operation */ short sem_flg; /* operation flags */ sem_num - semaphore number in the set sem_op negative number – wait positive number – signal 0 – wait for counter to become zero sem_flg – IPC_NOWAIT – system call fails if operation cannot be perfomed SEM_UNDO – if process exits, operation is undone

#include void cleanup(); int semid; union semun semarg; main() { int i; struct sembuf sops[1]; semid=semget ( IPC_PRIVATE, 1, 0600 ); for ( i=1 ; i < 19 ; i++ ) signal ( i, &cleanup ); semarg.val=1; semctl ( semid, 0, SETVAL, semarg ); sops->sem_num = 0; sops->sem_flg = 0; if ( fork()==0 ) { sops->sem_op = -1; semop ( semid, sops, 1 ); printf ("now you are locked\n"); sleep( 2 ); sops->sem_op = 1; semop ( semid, sops, 1 ); printf("son finished\n"); } else { sleep( 1 ); printf("waiting for son to release semaphore\n"); sops->sem_op = -1; semop ( semid, sops, 1 ); printf("father finished\n"); semctl ( semid, 0, IPC_RMID, semarg ); } void cleanup() { semctl ( semid, 0, IPC_RMID, semarg ); exit ( 1 ); } $ gcc -o ipc4{,.c} $ ipc4 now you are locked waiting for son to release semaphore son finished father finished I II III

$ cat ipc5.c #include #define NUMOFSEM 3 main() { int semid,value,i; union semun semarg,semarg1; semid=semget ( IPC_PRIVATE, NUMOFSEM, 0600 ); semarg.array=( ushort * ) malloc ( NUMOFSEM*sizeof ( ushort ) ); semarg1.array=( ushort * ) malloc ( NUMOFSEM*sizeof ( ushort ) ); for ( i=0 ; i<NUMOFSEM ; i++ ) semarg.array[i]=1; semctl ( semid, 0, SETALL, semarg ); semctl ( semid, 0, GETALL, semarg1 ); for ( i=0 ; i<NUMOFSEM ; i++ ) printf ( "semaphore %d: %d\n", i, semarg1.array[i]); semarg.val=0; semctl ( semid, 0, SETVAL, semarg ); value=semctl ( semid, 0, GETVAL, semarg ); printf ( "\nsemaphore 0: %d\n", value ); }

$ gcc -o ipc5{,.c} $ ipc5 semaphore 0: 1 semaphore 1: 1 semaphore 2: 1 semaphore 0: 0 $ ipcs IPC status from sunlight as of Wed Jul 12 14:54: T ID KEY MODE OWNER GROUP Message Queues: Shared Memory: Semaphores: s 40 0x ra wiseman opers $ ipcrm -s 40 $ ipcs IPC status from sunlight as of Wed Jul 12 14:54: T ID KEY MODE OWNER GROUP Message Queues: Shared Memory: Semaphores:

Cashing the check mmap, munmap - map or unmap files or devices into memory SYNOPSIS #include sys/mman.h void * mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset); int munmap(void *start, size_t length); DESCRIPTION The mmap function asks to map length bytes starting at offset offset from the file (or other object) specified by the file descriptor fd into memory, preferably at address start. This latter address is a hint only, and is usually specified as 0. The actual place where the object is mapped is returned by mmap, and is never 0.

Virtual page protection The prot argument describes the desired memory protection. It is either PROT_NONE or is the bitwise OR of one or more of the other PROT_* flags. PROT_EXEC Pages may be executed. PROT_READ Pages may be read. PROT_WRITE Pages may be written. PROT_NONE Pages may not be accessed.

The flags MAP_FIXED Do not use any other address other than the specified MAP_SHARED Share this mapping with all other processes that map this object. The file may not actually be updated until msync(2) or munmap(2) are called.msyncmunmap MAP_PRIVATE The opposite of MAP_SHARED