Download presentation
Presentation is loading. Please wait.
Published byJoanna Gray Modified over 8 years ago
1
Operating Systems Inter-Process Communication Shared Memory & Semaphores Moti Geva geva.biu@gmail.com
2
Types of IPC Pipes/Named pipes Signals Shared memory Semaphores Messages Sockets
3
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
4
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?
5
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
6
Page based memory
7
Example...
8
A more specific example
9
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
10
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?
11
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
12
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
13
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.
14
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
15
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
16
In practice(3) shmdt – detaches a previously attached shared memory SYNOPSIS #include sys/types.h #include sys/shm.h int shmdt(const void *shmaddr);
17
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.
18
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.
19
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 */ };
20
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).
21
$ 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!!!
22
$ 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
23
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?
24
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
25
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
26
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
27
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
28
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
29
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
30
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
31
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
32
#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
33
$ 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 ); }
34
$ 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:19 1995 T ID KEY MODE OWNER GROUP Message Queues: Shared Memory: Semaphores: s 40 0x00000000 --ra------- wiseman opers $ ipcrm -s 40 $ ipcs IPC status from sunlight as of Wed Jul 12 14:54:44 1995 T ID KEY MODE OWNER GROUP Message Queues: Shared Memory: Semaphores:
35
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.
36
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.
37
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.