Unix InterProcess Communication Message Passing Synchronization Shared Memory
![Unix Inter-Process Communication Message Passing Synchronization Shared Memory Remote Procedure Calls Unix Inter-Process Communication Message Passing Synchronization Shared Memory Remote Procedure Calls](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-1.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-2.jpg)
![Posix IPC z. Message Queues z. Semaphores z. Shared Memory Posix IPC z. Message Queues z. Semaphores z. Shared Memory](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-3.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-4.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-5.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-6.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-7.jpg)
![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()](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-8.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-9.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-10.jpg)
![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.](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-11.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-12.jpg)
![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,](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-13.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-14.jpg)
![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)](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-15.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-16.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-17.jpg)
![Example sembuf struct sembuf ops[2] = { 0, 0, 0, /* wait for [0] Example sembuf struct sembuf ops[2] = { 0, 0, 0, /* wait for [0]](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-18.jpg)
![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; /*](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-19.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-20.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-21.jpg)
![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 =](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-22.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-23.jpg)
![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.](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-24.jpg)
![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, /*](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-25.jpg)
![Union semun definition Union semun { int }; val; /* used for SETVAL */ Union semun definition Union semun { int }; val; /* used for SETVAL */](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-26.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-27.jpg)
![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))](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-28.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-29.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-30.jpg)
![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,](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-31.jpg)
![Sem_close - close a semaphore z. Int sem_close(sem_t *sem) Sem_close - close a semaphore z. Int sem_close(sem_t *sem)](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-32.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-33.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-34.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-35.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-36.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-37.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-38.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-39.jpg)
![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,](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-40.jpg)
![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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-41.jpg)
- Slides: 41
![Unix InterProcess Communication Message Passing Synchronization Shared Memory Remote Procedure Calls Unix Inter-Process Communication Message Passing Synchronization Shared Memory Remote Procedure Calls](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-1.jpg)
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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-2.jpg)
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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-3.jpg)
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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-4.jpg)
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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-5.jpg)
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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-6.jpg)
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 xmqueue h y Functions to create open or delete Posix Message Queues y. Header x<mqueue. h> y. Functions to create, open, or delete](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-7.jpg)
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 xsemaphore h y Functions to create open or delete xsemopen Posix Semaphores y. Header x<semaphore. h> y. Functions to create, open, or delete xsem_open()](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-8.jpg)
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 xsysmman h y Functions to create open or delete Posix Shared Memory y. Header x<sys/mman. h> y. Functions to create, open, or delete](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-9.jpg)
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 createopen second argument y How the object is being openedrequired x Readonly xwriteonly Posix create/open second argument y. How the object is being opened-required x. Read-only xwrite-only](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-10.jpg)
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 SIRUSR y SIWUSR y Mode Constants when a new IPC object is created y. S_IRUSR y. S_IWUSR y.](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-11.jpg)
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 yinclude systypes h yinclude sysipc h z Specific IPC types Include Files z. General y#include <sys/types. h> y#include <sys/ipc. h> z. Specific IPC types](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-12.jpg)
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>
![Keyt keys and ftok function z Prototype yinclude sysipc h ykeyt ftokconst char pathname Key_t keys and ftok() function z. Prototype y#include <sys/ipc. h> ykey_t ftok(const char *pathname,](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-13.jpg)
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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-14.jpg)
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 semgetkeyt key int nsems int flag semget - obtain a semaphore set z int semget(key_t key, int nsems, int flag)](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-15.jpg)
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 semopint semid struct sembuf opstr sizet Semop - perform a semaphore operation z Int semop(int semid, struct sembuf *opstr, size_t](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-16.jpg)
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 nsems1 yshort Sembuf structure z. Struct sembuf { yshort semnum; xsemaphore number: 0. . nsems-1 yshort](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-17.jpg)
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 ops2 0 0 0 wait for 0 Example sembuf struct sembuf ops[2] = { 0, 0, 0, /* wait for [0]](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-18.jpg)
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 ops2 declaration ops0 semnum 0 sembuf proper initialization struct sembuf ops[2]; /* declaration */ ops[0]. sem_num = 0; /*](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-19.jpg)
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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-20.jpg)
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 semop 0 yvalue of semop is added to semval Operation of semop z sem_op > 0 yvalue of sem_op is added to semval](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-21.jpg)
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 ysemop Critical section control z. Initial semval is 1 z. Enter. Critical. Section ysem_op =](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-22.jpg)
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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-23.jpg)
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.](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-24.jpg)
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 semctlint semid int semnum int cmd Semctl - control a semaphore z Int semctl(int semid, int semnum, int cmd, /*](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-25.jpg)
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 */](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-26.jpg)
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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-27.jpg)
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 semctlsemid Lock SETVAL arg Semctl examples Union semun arg; arg. val = 1; if (semctl(sem_id, Lock, SETVAL, arg))](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-28.jpg)
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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-29.jpg)
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 Semopen open a semaphore zsemclose close a semaphore zsemunlink Posix Semaphores z. Sem_open - open a semaphore zsem_close - close a semaphore zsem_unlink](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-30.jpg)
Posix Semaphores z. Sem_open - open a semaphore zsem_close - close a semaphore zsem_unlink -
![Semopen create a new named semaphore z Semt semopenconst char name int oflag Sem_open() - create a new named semaphore z Sem_t *sem_open(const char *name, int oflag,](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-31.jpg)
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
![Semclose close a semaphore z Int semclosesemt sem Sem_close - close a semaphore z. Int sem_close(sem_t *sem)](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-32.jpg)
Sem_close - close a semaphore z. Int sem_close(sem_t *sem)
![Semwait and semtrywait Wait for a semaphore z Int semwaitsemt sem y Decrement value Sem_wait and sem_trywait Wait for a semaphore z. Int sem_wait(sem_t *sem) y. Decrement value](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-33.jpg)
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.
![Sempost and semgetvalue z Int Sempostsemt 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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-34.jpg)
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 pthreadmutext lock PTHREADMUTEXINITIALIZER z Int pthreadmutexlockpthreadmutex mptr z int Posix mutex z Static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER z Int pthread_mutex_lock(pthread_mutex *mptr) z int](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-35.jpg)
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](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-36.jpg)
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 mmapvoid addr sizet len int prot int MMAP - Memory mapped file z Void *mmap(void *addr, size_t len, int prot, int](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-37.jpg)
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 MAPSHARED or MAPPRIVATE x MAPPRIVATE underlying MMAP - continued z Flags y Either MAP_SHARED or MAP_PRIVATE x. MAP_PRIVATE - underlying](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-38.jpg)
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 munmapvoid addr sizet Munmap - removes a mapping from the address space z. Int munmap(void *addr, size_t](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-39.jpg)
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 msyncvoid addr Msync - synchronize the file with the memory data z Int msync(void * addr,](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-40.jpg)
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 PrenticeHall References z. W. Richard Stevens, Unix Network Programming, Volume 2, 2 nd edition, Prentice-Hall](https://slidetodoc.com/presentation_image_h/28cb7e26ea277c59bd110bb4bd7c3b93/image-41.jpg)
References z. W. Richard Stevens, Unix Network Programming, Volume 2, 2 nd edition, Prentice-Hall PTR, ISBN 0 -13 -081081 -9, 1999.
Interprocess communication shared memory
Shared memory in unix
Shared memory vs distributed memory
Interprocess communication in linux
Interprocess communication in java
Interprocess communication in os
Race condition in interprocess communication
Characteristics of interprocess communication
Android interprocess communication
Reader writer problem
Principles of message passing programming
Desirable features of message passing system
Divergence of darkness
Message passing paradigm in distributed computing
Message passing
Desirable features of good message passing system
Message passing model
Variational message passing
Message passing interface tutorial
Message passing game
Message passing system in distributed system
Distributed system models
Message passing system in distributed system
Erlang message passing
Message passing interface
Message passing os
Special relativity vs general relativity
Ipcs unix
Buddy algorithm in linux
Pthreads tutorial
Shared memory consistency models: a tutorial
Java shared memory between threads
Centralized shared memory architecture
Symmetric shared memory architecture
Cuda bank conflict
Symmetric shared memory architecture
Distributed shared memory
Distributed shared memory
Murat olcay özcan
Design issues of distributed shared memory
Acc shared memory
Shared virtual memory