Unix InterProcess Communication Message Passing Synchronization Shared Memory

  • Slides: 41
Download presentation
Unix Inter-Process Communication Message Passing Synchronization Shared Memory Remote Procedure Calls

Unix Inter-Process Communication Message Passing Synchronization Shared Memory Remote Procedure Calls

SYSV and Posix z. SYSV y. Designed by Bell Labs in the 1980 s

SYSV and Posix z. SYSV y. Designed by Bell Labs in the 1980 s y. Generally supported y. Complicated and difficult to use z. Posix y. Developed as a realtime standard in the 1990 s y. Less generally supported y. Simpler to use

Posix IPC z. Message Queues z. Semaphores z. Shared Memory

Posix IPC z. Message Queues z. Semaphores z. Shared Memory

Posix IPC Names z. Name rules ymust conform to existing rules for pathnames xat

Posix IPC Names z. Name rules ymust conform to existing rules for pathnames xat most PATH_MAX bytes xterminate with a NULL byte x. Begin with `/’ • => all uses reference same entity • otherwise, effect is implementation dependent x. Additional `/’s • interpretation is implementation defined

Solaris 2. 6 Posix Name Conventions z. Requires initial slash z. Forbids additional slashes

Solaris 2. 6 Posix Name Conventions z. Requires initial slash z. Forbids additional slashes z. Named entities are created in /tmp z. Use #define to define names to make it easy to change for different operating systems.

Digital Unix Posix Name Conventions z. Creates the specified pathname in the file system

Digital Unix Posix Name Conventions z. Creates the specified pathname in the file system z. Owner must have rights to create name y/bozo is generally invalid xcreated in root filesystem z. Names generally have several slashes.

Posix Message Queues y. Header x<mqueue. h> y. Functions to create, open, or delete

Posix Message Queues y. Header x<mqueue. h> y. Functions to create, open, or delete xmq_open() mq_close() mq_unlink() y. Functions for control operations xmq_getattr() mq_setattr() y. Functions for IPC operations xmq_send() mq_receive() mq_notify()

Posix Semaphores y. Header x<semaphore. h> y. Functions to create, open, or delete xsem_open()

Posix Semaphores y. Header x<semaphore. h> y. Functions to create, open, or delete xsem_open() sem_close() sem_unlink() sem_init() sem_destroy() y. Functions for IPC operations xsem_wait() sem_trywait() sem_post() sem_getvalue()

Posix Shared Memory y. Header x<sys/mman. h> y. Functions to create, open, or delete

Posix Shared Memory y. Header x<sys/mman. h> y. Functions to create, open, or delete xshm_open() shm_unlink() y. Functions for control operations xftruncate() fstat() y. Functions for IPC operations xmmap(), munmap()

Posix create/open second argument y. How the object is being opened-required x. Read-only xwrite-only

Posix create/open second argument y. How the object is being opened-required x. Read-only xwrite-only xread-write O_RDONLY O_WRONLY O_RDWR y. Optional flags xcreate if does not already exist O_CREATE xexclusive create O_EXCL xnonblocking mode O_NONBLOCK xtruncate if already exists O_TRUNC

Mode Constants when a new IPC object is created y. S_IRUSR y. S_IWUSR y.

Mode Constants when a new IPC object is created y. S_IRUSR y. S_IWUSR y. S_IRGRP y. S_IWGRP y. S_IROTH y. S_IWOTH user read user write group read group write other read other write

Include Files z. General y#include <sys/types. h> y#include <sys/ipc. h> z. Specific IPC types

Include Files z. General y#include <sys/types. h> y#include <sys/ipc. h> z. Specific IPC types y#include <sys/sem. h> y#include <sys/shm. h> y#include <sys/msg. h>

Key_t keys and ftok() function z. Prototype y#include <sys/ipc. h> ykey_t ftok(const char *pathname,

Key_t keys and ftok() function z. Prototype y#include <sys/ipc. h> ykey_t ftok(const char *pathname, int id) yreturns IPC key if OK, -1 on error z. Purpose y. Create a unique integer key_t y. Same arguments return same value if performed on same computer system

System V Semaphores z. Functions ysemget() xcreates a semaphore set or accesses an existing

System V Semaphores z. Functions ysemget() xcreates a semaphore set or accesses an existing semaphore set. ysemop() xperforms operations on one or more of the semaphores ysemctl() xperforms various control operations on a semaphore

semget - obtain a semaphore set z int semget(key_t key, int nsems, int flag)

semget - obtain a semaphore set z int semget(key_t key, int nsems, int flag) ykey x. IPC_PRIVATE - new semaphore object created xinteger constant - if no IPC object matches key and IPC_CREAT in flag, new semaphore is created xftok() can be used to create a semaphore number corresponding to a filename (inode included). ynsems - number of semaphores in set yflag permissions x. SEM_R, (SEM_R>>3), (SEM_R>>6) read access x. SEM_A, (SEM_A>>3), (SEM_A>>6) alter access

Semop - perform a semaphore operation z Int semop(int semid, struct sembuf *opstr, size_t

Semop - perform a semaphore operation z Int semop(int semid, struct sembuf *opstr, size_t nops) ysemid - semaphore set id as returned by semget() yopstr xan array of sembuf structures xgives the sequence of semaphore operations to be performed ynops xthe number of semaphore operations in opstr

Sembuf structure z. Struct sembuf { yshort semnum; xsemaphore number: 0. . nsems-1 yshort

Sembuf structure z. Struct sembuf { yshort semnum; xsemaphore number: 0. . nsems-1 yshort sem_op xsemaphore operation to be performed yshort sem_flg xoperation flags: 0, IPC_NOWAIT, SEM_UNDO z}

Example sembuf struct sembuf ops[2] = { 0, 0, 0, /* wait for [0]

Example sembuf struct sembuf ops[2] = { 0, 0, 0, /* wait for [0] to be 0 */ 0, 1, SEM_UNDO /* then inc [0] by 1 */ }; Warning: should not assume order of sembuf elements!

sembuf proper initialization struct sembuf ops[2]; /* declaration */ ops[0]. sem_num = 0; /*

sembuf proper initialization struct sembuf ops[2]; /* declaration */ ops[0]. sem_num = 0; /* executable initialization */ ops[0]. sem_op = 0; ops[0]. sem_flg = 0; ops[1]. sem_num = 0; ops[1]. sem_op = 1; ops[1]. sem_flg = SEM_UNDO;

Semaphore attributes z semval ycurrent value of the semaphore z semncnt ynumber of threads

Semaphore attributes z semval ycurrent value of the semaphore z semncnt ynumber of threads waiting for semval to be greater than its current value z semzcnt ynumber of threads waiting for semval to be 0 z semadj y. Adjustment value to UNDO a semaphore

Operation of semop z sem_op > 0 yvalue of sem_op is added to semval

Operation of semop z sem_op > 0 yvalue of sem_op is added to semval yrelease of resources the semaphore controls z sem_op == 0 ywait until semval is zero yimmediate return if already zero z sem_op < 0 ywait until semval ≥ | sem_op | then add sem_op to semval (decreases semval due to negative value) yallocation of resources the semaphore controls

Critical section control z. Initial semval is 1 z. Enter. Critical. Section ysem_op =

Critical section control z. Initial semval is 1 z. Enter. Critical. Section ysem_op = -1 ysem_flg = 0 z. Exit. Critical. Section ysem_op = 1 ysem_flg = 0

Allocating Resources z. Initial semval is the total number of resources z. Allocate n

Allocating Resources z. Initial semval is the total number of resources z. Allocate n units of resource (may block) ysem_op = -n ysem_flg = 0 (or SEM_UNDO) z. Free m units of resource ysem_op = m ysem_flg = 0

Allocating Resources without blocking z. Initial semval is the total number of resources z.

Allocating Resources without blocking z. Initial semval is the total number of resources z. Allocate n units of resource ysem_op = -n ysem_flg = IPC_NOWAIT ysemop() returns an error of EAGAIN if insufficient resources

Semctl - control a semaphore z Int semctl(int semid, int semnum, int cmd, /*

Semctl - control a semaphore z Int semctl(int semid, int semnum, int cmd, /* union semun arg */ ); ysemid xidentifies the semaphore set ysemnum xidentifies the member of the semaphore set ycmd xaction to perform --see list on following slides yunion semun xrequired for some commands

Union semun definition Union semun { int }; val; /* used for SETVAL */

Union semun definition Union semun { int }; val; /* used for SETVAL */ struct semid_ds *buf; /* used for IPC_SET and IPC_STAT */ ushort /* used for GETALL and SETALL */ *array;

Semaphore cmd values z z z z z GETVAL - return current value of

Semaphore cmd values z z z z z GETVAL - return current value of semval SETVAL - set value of semval GETPID - return current value of sempid GETNCNT - return current value of semnvnt GETZCNT - return current value of semzcnt GETALL - return values of semval for all members of the set into arg. array SETALL - set all values semval for each member of the set from arg. array IPC_RMID - remove the semaphore set from the system IPC_SET - set elements of the semid_ds structure IPC_STAT - return current elements of the semid_ds structure

Semctl examples Union semun arg; arg. val = 1; if (semctl(sem_id, Lock, SETVAL, arg))

Semctl examples Union semun arg; arg. val = 1; if (semctl(sem_id, Lock, SETVAL, arg)) { perror(“Setting semaphore Lock to 1 failed. ”); return; } arg. val = BUFFSIZE; if (semctl(sem_id, Empties, SETVAL, arg)) { perror(“Setting semaphore Empties to BUFFSIZE failed. ”); return; } semctl(sem_id, 3, IPC_RMID); /* Book shows second arg of 0. Probably ignored. */

ipcs and ipcrm programs z. Ipcs y. Print IPC status information zipcrm y. Remove

ipcs and ipcrm programs z. Ipcs y. Print IPC status information zipcrm y. Remove a semaphore set, message set, or shared memory set.

Posix Semaphores z. Sem_open - open a semaphore zsem_close - close a semaphore zsem_unlink

Posix Semaphores z. Sem_open - open a semaphore zsem_close - close a semaphore zsem_unlink -

Sem_open() - create a new named semaphore z Sem_t *sem_open(const char *name, int oflag,

Sem_open() - create a new named semaphore z Sem_t *sem_open(const char *name, int oflag, /* mode_t mode, unsigned int val*/ ); y name x. A pathname-like name, not well defined across systems. x. Always #define the name so easily changed. x. Good name is /abcdef y oflag x 0 x. O_CREATE | O_EXCL y mode xpermissions bits y val xinitial value

Sem_close - close a semaphore z. Int sem_close(sem_t *sem)

Sem_close - close a semaphore z. Int sem_close(sem_t *sem)

Sem_wait and sem_trywait Wait for a semaphore z. Int sem_wait(sem_t *sem) y. Decrement value

Sem_wait and sem_trywait Wait for a semaphore z. Int sem_wait(sem_t *sem) y. Decrement value of semaphore by 1, wait if semaphore value is <=0. zint sem_trywait(sem_t *sem) y. Same as sem_wait() if semaphore will not block y. Returns with return value EAGAIN if would have blocked.

Sem_post and sem_getvalue z. Int Sem_post(sem_t *sem) y. Increment semaphore value yrelease waiting process

Sem_post and sem_getvalue z. Int Sem_post(sem_t *sem) y. Increment semaphore value yrelease waiting process if appropriate zint sem_getvalue(sem_t *sem, int *valp) yget the value of a semaphore

Posix mutex z Static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER z Int pthread_mutex_lock(pthread_mutex *mptr) z int

Posix mutex z Static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER z Int pthread_mutex_lock(pthread_mutex *mptr) z int pthread_mutex_trylock(pthread_mutex_t *mptr) z int pthread_mutex_unlock(pthread_mutex_t *mprt); z Enter and exit must be executed by the same thread.

Shared Memory using MMAP() zmmap() ymap file or a Posix shared memory object into

Shared Memory using MMAP() zmmap() ymap file or a Posix shared memory object into the address space of a process zmunmap() yremove a mapping from the address space of the process zmsync() ymake sure what is in the disk file is the same as what is in memory.

MMAP - Memory mapped file z Void *mmap(void *addr, size_t len, int prot, int

MMAP - Memory mapped file z Void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset) z addr y NULL - kernel chooses starting address y Specific starting address within process z len y number of bytes to map into the address space, starting at offset bytes into the file z prot y protection of the memory-mapped region chosen from PROT_READ, PROT_WRITE, PROT_EXEC, PROT_NONE y PROT_READ | PROT_WRITE gives read-write access

MMAP - continued z Flags y Either MAP_SHARED or MAP_PRIVATE x. MAP_PRIVATE - underlying

MMAP - continued z Flags y Either MAP_SHARED or MAP_PRIVATE x. MAP_PRIVATE - underlying object is not changed x. MAP_SHARED - underlying object is changed and changes are visible to other processes y MAP_FIXED ored to above to interpret addr exactly. y Use null addr and do not use MAP_FIXED for portable code. z Fd y file which is mapped into storage. May not be a device (terminal). z Offset y offset into file where mapping begins

Munmap - removes a mapping from the address space z. Int munmap(void *addr, size_t

Munmap - removes a mapping from the address space z. Int munmap(void *addr, size_t len); yaddr xaddress returned by mmap ylen xsize of the mapped region

Msync - synchronize the file with the memory data z Int msync(void * addr,

Msync - synchronize the file with the memory data z Int msync(void * addr, size_t len, int flags); y addr xaddress of memory mapped region y len xlength of the region y flags x. MS_ASYNC - perform asynchronous writes x. MS_SYNC - perform synchronous writes x. MS_INVALIDATE - invalidate cached storage y Usually addr and len are the whole region, but don’t have to be.

References z. W. Richard Stevens, Unix Network Programming, Volume 2, 2 nd edition, Prentice-Hall

References z. W. Richard Stevens, Unix Network Programming, Volume 2, 2 nd edition, Prentice-Hall PTR, ISBN 0 -13 -081081 -9, 1999.