Posix Shm Slides
-
Upload
dhavalchikani -
Category
Documents
-
view
236 -
download
0
Transcript of Posix Shm Slides
-
8/11/2019 Posix Shm Slides
1/21
Linux/UNIX System Programming
POSIX Shared Memory
Michael Kerrisk, man7.org c 2013
Nov 2013
Outline
21 POSIX Shared Memory 21-121.1 Overview 21-321.2 Creating and opening shared memory objects 21-1021.3 Using shared memory objects 21-2021.4 Synchronizing access to shared memory 21-2921.5 Unmapping and removing shared memory objects 21-39
-
8/11/2019 Posix Shm Slides
2/21
Outline
21 POSIX Shared Memory 21-121.1 Overview 21-321.2 Creating and opening shared memory objects 21-1021.3 Using shared memory objects 21-2021.4 Synchronizing access to shared memory 21-2921.5 Unmapping and removing shared memory objects 21-39
Shared memory
Data is exchanged by placing it in memory pages sharedby multiple processes
Pages are in user virtual address space of each process
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-4 21.1
-
8/11/2019 Posix Shm Slides
3/21
-
8/11/2019 Posix Shm Slides
4/21
POSIX shared memory objects
Implemented (on Linux) as les in a dedicated tmpfs lesystem
tmpfs == virtual memory lesystem that employs swapspace when needed
Objects have kernel persistenceObjects exist until explicitly deleted, or system rebootsCan map an object, change its contents, and unmapChanges will be visible to next process that maps object
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-7 21.1
POSIX shared memory APIs
3 main APIs used with POSIX shared memory:
shm_open() :Open existing shared memory (SHM) object, orCreate and open new SHM object
Returns a le descriptor used in later callsftruncate() : set size of SHM objectmmap(): map SHM object into callers address spaceCompile with cc -lrt
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-8 21.1
-
8/11/2019 Posix Shm Slides
5/21
POSIX shared memory APIs
Other APIs used with POSIX shared memory:
munmap(): unmap a mapped object from callers addressspace
close(): close le descriptor returned by shm_open()shm_unlink() : remove SHM object name, mark for deletiononce all processes have closedfstat() : retrieve stat structure describing objects
Includes size of object, ownership, and permissions
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-9 21.1
Outline
21 POSIX Shared Memory 21-121.1 Overview 21-321.2 Creating and opening shared memory objects 21-1021.3 Using shared memory objects 21-2021.4 Synchronizing access to shared memory 21-2921.5 Unmapping and removing shared memory objects 21-39
-
8/11/2019 Posix Shm Slides
6/21
Creating/opening a shared memory object: shm_open()
# include < fcnt l . h> /* D ef in es O _* c on st an ts */ #inc lude / * De fi ne s mo de c o ns ta nt s */ #inc lude i nt s hm _o pe n ( c on st c ha r * name , i nt of la g , m od e_ t m od e );
Creates and opens a new object, or opens an existing objectname : name of object (/somename )mode : permission bits for new object
RWX for user / group / otherANDed against process umask
Required argument; specify as 0 if opening existing object
Returns le descriptor on success, or -1 on error
[TLPI 54.2]
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-11 21.2
Creating/opening a shared memory object: shm_open()
# include < fcnt l . h> /* D ef in es O _* c on st an ts */ #inc lude / * De fi ne s mo de c o ns ta nt s */ #inc lude i nt s hm _o pe n ( c on st c ha r * name , i nt of la g , m od e_ t m od e );
o ag species ags controlling operation of call
O_CREAT: create object if it does not already existO_EXCL: (with O_CREAT) create object exclusively
Give error if object already exists
O_RDONLY: open object for read-only accessO_RDWR: open object for read-write access
NB: No O_WRONLY ag...O_TRUNC: truncate an existing object to zero length
Contents of existing object are destroyed
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-12 21.2
-
8/11/2019 Posix Shm Slides
7/21
-
8/11/2019 Posix Shm Slides
8/21
Mapping a shared memory object: mmap()
inc lude v oi d * m ma p ( vo id * add r, s iz e_ t l en gt h , i nt pro t ,
i nt f la gs , i nt fd , o ff _t o ff se t );
addr : address at which to place mapping in callers virtualaddress spaceNULL == let system choose addressNormally use NULL for POSIX SHM objects
mmap() returns address actually used for mappingTreat this like a normal C pointer
On error, mmap() returns MAP_FAILED
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-15 21.2
Mapping a shared memory object: mmap()
inc lude v oi d * m ma p ( vo id * add r, s iz e_ t l en gt h , i nt pro t ,
i nt f la gs , i nt fd , o ff _t o ff se t );
length: size of mapping
Normally should be size of SHM objectSystem rounds up to multiple of system page size
sysconf(_SC_PAGESIZE)ftruncate() size is commonly multiple of page size
prot : memory protectionsPROT_READ: for read-only mappingPROT_READ | PROT_WRITE: for read-write mappingShould be consistent with access mode of shm_open()(O_RDONLY or O_RDWR)
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-16 21.2
-
8/11/2019 Posix Shm Slides
9/21
Mapping a shared memory object: mmap()
inc lude v oi d * m ma p ( vo id * add r, s iz e_ t l en gt h , i nt pro t ,
i nt f la gs , i nt fd , o ff _t o ff se t );
ags : bit ags controlling behavior of callPOSIX SHM objects: only need MAP_SHAREDMAP_SHARED == make callers modi cations to mappedmemory visible to other processes mapping same object
fd : le descriptor specifying le to mapUse FD returned by shm_open()
o ff set : starting point of mapping in underlying le or SHMobject
Must be multiple of system page sizeNormally speci ed as 0 (map from start of object)
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-17 21.2
Example: pshm/pshm_create_simple.c
. /pshm_crea te_s imple shm -object -name s ize
Create a SHM object with given name and size
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-18 21.2
-
8/11/2019 Posix Shm Slides
10/21
Example: pshm/pshm_create_simple.c
1 i nt fd ;2 s iz e_ t s iz e ;3 v oi d * a dd r ;4 size = a to i ( a rgv[2 ]) ;5 fd = s hm _o pe n ( argv [ 1] , O _C RE AT | O _E XC L | O_RDWR,
6 S_IRUSR | S_IWUSR);7 f t runca t e ( fd , size );8 a d dr = m ma p ( NULL , size , PROT_READ | PROT_WRITE,9 MAP_SHARED , fd , 0);
1 SHM object created with RW permission for user, openedwith read-write access mode
2 fd returned by shm_open() is used in ftruncate() + mmap()3 Same size is used in ftruncate() + mmap()4 mmap() not necessary, but demonstrates how its done5 Mapping protections PROT_READ | PROT_WRITE consistent
with O_RDWR access modeLinux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-19 21.2
Outline
21 POSIX Shared Memory 21-121.1 Overview 21-321.2 Creating and opening shared memory objects 21-1021.3 Using shared memory objects 21-2021.4 Synchronizing access to shared memory 21-2921.5 Unmapping and removing shared memory objects 21-39
-
8/11/2019 Posix Shm Slides
11/21
Using shared memory objects
Address returned by mmap() can be used just like any Cpointer
Usual approach: treat as pointer to some structured type
Can read and modify memory via pointer
[TLPI 48.6]
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-21 21.3
Example: pshm/phm_write.c
. / p shm_wr i te shm- name s t ri ng
Open existing SHM object shm-name and copy string to it
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-22 21.3
-
8/11/2019 Posix Shm Slides
12/21
Example: pshm/phm_write.c
1 i nt fd ;2 s iz e_ t l en ; /* Si ze of s ha re d m em or y o bj ec t */ 3 c ha r * a dd r ;4 fd = shm_open (a rgv[1] , O_RDWR , 0 );5 len = s t r len(argv[2] ) ;
6 ftruncate ( fd , len );7 p ri nt f ( " Re si ze d to % ld b yt es \ n " , ( l on g ) l en ) ;8 addr = mmap(NULL, len , PROT_READ | PROT_WRITE ,9 MAP_SHARED , fd , 0);
10 close(fd) ; /* fd is no l on ge r n ee de d */ 11 p rin tf ( " copying %ld byt es \n" , ( l ong) l en ) ;12 memcpy(addr, argv[2], len);
1 Open existing SHM object
2 Resize object to match length of command-line argument3 Map object at address chosen by system4 Copy argv[2] to object (without \0)5 SHM object is closed and unmapped on process termination
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-23 21.3
Example: pshm/phm_read.c
. /pshm_read shm -name
Open existing SHM object shm-name and write thecharacters it contains to stdout
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-24 21.3
-
8/11/2019 Posix Shm Slides
13/21
Example: pshm/phm_read.c
1 i nt fd ;2 c ha r * a dd r ;3 s tr uc t s tat sb ;45 fd = shm_open(argv[1] , O_RDONLY , 0);
67 fstat ( fd , & sb ) ;8 addr = mmap(NULL, sb.st_size , PROT_READ , MAP_SHARED ,9 fd , 0);
10 close(fd) ; /* fd is no l on ge r n ee de d */ 1112 write(STDOUT_FILENO, addr, sb.st_size);13 pr int f (" \n") ;
Open existing SHM objectUse fstat() to discover size of objectMap the object, using size from fstat() (in sb.st_size )Write all bytes from object to stdout , followed by newline
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-25 21.3
Pointers in shared memory
A little care is required when storing pointers in SHM:
mmap() maps SHM object at arbitrary location in memoryAssuming addr is speci ed as NULL, as recommended
Mapping may be placed at diff erent address in each
processSuppose we want to build dynamic data structures, withpointers inside shared memory...
Must use relative o ff sets , not absolute addressesAbsolute address has no meaning if mapping is at diff erentlocation in another process
[TLPI 48.6]
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-26 21.3
-
8/11/2019 Posix Shm Slides
14/21
Pointers in shared memory
Suppose we have situation at rightbaseaddr is return value frommmap()
Want to store pointer to target in *p
Wrong way:* p = t arge t
Correct method (relative off set):
* p = t arge t - b as ea dd r ;
To dereference pointer:t arge t = b as ea dd r + * p;
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-27 21.3
The /dev/shm lesystem
On Linux:tmpfs lesystem used to implement POSIX SHM is mounted at/dev/shmCan list objects in directory with ls(1)
ls -l shows permissions, ownership, and size of each object$ ls - l / dev / shm - rw - - -- -- -. 1 mtk mtk 4 09 6 Oct 27 1 3: 58 m ys hm -rw - -- -- --. 1 mtk mtk 32 Oct 27 13:57 sem . my sem
POSIX named semaphores are also visible in /dev/shmAs small SHM objects with names pre xed with sem.
Can delete objects with rm(1)
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-28 21.3
-
8/11/2019 Posix Shm Slides
15/21
Outline
21 POSIX Shared Memory 21-121.1 Overview 21-321.2 Creating and opening shared memory objects 21-1021.3 Using shared memory objects 21-2021.4 Synchronizing access to shared memory 21-2921.5 Unmapping and removing shared memory objects 21-39
Synchronizing access to shared memory
Accesses to SHM object by diff erent processes must besynchronized
Prevent simultaneous updatesPrevent read of partially updated data
Any synchronization technique can be usedSemaphores are most common techniquePOSIX unnamed semaphores are often convenient,since:
Semaphore can be placed inside shared memory region(And thus, automatically shared)
We avoid task of creating name for semaphore
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-30 21.4
-
8/11/2019 Posix Shm Slides
16/21
Example: synchronizing with POSIX unnamed semaphores
Example application maintains sequence number in SHMobjectSource les:
pshm/pshm_seqnum.h : de
nes structure stored in SHMobjectpshm/pshm_seqnum_init.c :
Create and open SHM object;Initialize semaphore and (optionally) sequence numberinside SHM object
pshm/pshm_seqnum_get.c : display current value of sequence number and (optionally) increase its value
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-31 21.4
Example: pshm/pshm_seqnum.h
1 # include 2 # include < fcnt l . h>3 # include 4 # include 5 # include " t l p i_hdr.h"67 s t ru ct s hm bu f { /* S ha re d m em or y b uf fe r */ 8 sem_t sem ; /* S em ap ho re to p ro te ct a cc e ss * / 9 int seqnum ; /* S eq ue nc e n um be r */
10 };
Header le used by pshm/pshm_seqnum_init.c andpshm/pshm_seqnum_get.c
Includes headers needed by both programsDe nes structure used for SHM object , containing:
Unnamed semaphore that guards access to sequencenumberSequence number
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-32 21.4
-
8/11/2019 Posix Shm Slides
17/21
Example: pshm/pshm_seqnum_init.c
. /pshm_seqnum_init shm -name [ ini t - va lue]
Create and open SHM object
Reset semaphore inside object to 1 (i.e., semaphoreavailable)Initialize sequence number
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-33 21.4
Example: pshm/pshm_seqnum_init.c
1 s t r uc t s hm bu f * s hm p ;2 shm_unlink (argv[1]) ;3 fd = shm_open(argv[1] , O_CR EAT|O_EXC L|O_RDWR , S _IRUS R |S _IWUS R ) ;4 ftruncate ( fd , sizeof(struct shmbuf) );5 shmp = mmap(NULL, sizeof(struct shmbuf) ,6 PROT_READ | PROT_WRITE , MAP_SHARED , fd , 0);7 sem_init ( &shmp->sem , 1 , 1);8 i f ( argc > 2)9 shmp->seqnum = atoi(argv[2]);
1 Delete previous instance of SHM object, if it exists2 Create and open SHM object3 Use ftruncate() to adjust size of object to match structure4 Map object, using size of structure5 Initialize semaphore state to available
pshared speci ed as 1, for process sharing of semaphore6 If argv[2] supplied, initialize sequence number to that value
Note : newly extended bytes of SHM object are initialized to 0
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-34 21.4
-
8/11/2019 Posix Shm Slides
18/21
Example: pshm/pshm_seqnum_get.c
. /pshm_seqnum_get shm -name [ run- length]
Open existing SHM object
Fetch and display current value of sequence number in SHMobject shm-name If run-length supplied, add to sequence number
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-35 21.4
Example: pshm/pshm_seqnum_get.c (1)
1 i nt fd , r un Le ng th ;2 s tr uc t s hm bu f * s hm p ;34 fd = shm_open (a rgv[1] , O_RDWR , 0 );56 shmp = mmap(NULL, sizeof(struct shmbuf) ,7 PROT_READ | PROT_WRITE , MAP_SHARED , fd , 0);
Open existing SHM objectMap object, using size of shmbuf structure
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-36 21.4
-
8/11/2019 Posix Shm Slides
19/21
Example: pshm/pshm_seqnum_get.c (2)
1 sem_wait (&shmp->sem);2 printf ( "Curr ent value o f s emaphore : %d\n" , shmp->seqnum) ;3 if ( argc > 2) {4 runLength = atoi (argv [2]);5 if ( runLength seqnum += atoi(argv[2]) ;
10 pr intf (" Updated semaphore value \n" );11 }12 }13 sem_post (&shmp->sem);
Reserve semaphore before touching sequence numberDisplay current value of semaphoreIf (nonnegative) argv[2] provided, add to sequence number
Sleep during update, to see that other processes are blocked
Release semaphoreLinux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-37 21.4
Exercise1 Write two programs that exchange a stream of data of arbitrary length via a POSIX
shared memory object:The reader ( pshm_xfr_reader.c ) reads blocks of data from stdin andcopies them a block at a time to the shared memory region.
The writer ( pshm_xfr_writer.c ) writes each block of data from theshared memory object to stdout
You must ensure that the reader and writer have exclusive, alternating access to theshared memory region (so that, for example, the reader does not copy new datainto the region before the writer has copied the current data to stdout ). This willrequire the use of a pair of semaphores. (Using unnamed semaphores stored insidethe shared memory object is simplest.) The psem_tty_lockstep_init.c andpsem_tty_lockstep_second.c programs provided in the POSIX semaphoresmodule show how semaphores can be used for this task.
When the reader reaches end of le, it should provide an indication to the writerthat there is no more data. To do this maintain a byte-count eld in the sharedmemory region which the reader uses to inform the writer how many bytes are
to be written. Setting this count to 0 can be used to signal end-of- le. Once thetwo programs have nished there work, the reader should destroy the unnamedsemaphores and unlink the shared memory object, but only after the writer hasprocessed the last data block.
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-38 21.4
-
8/11/2019 Posix Shm Slides
20/21
Outline
21 POSIX Shared Memory 21-121.1 Overview 21-321.2 Creating and opening shared memory objects 21-1021.3 Using shared memory objects 21-2021.4 Synchronizing access to shared memory 21-2921.5 Unmapping and removing shared memory objects 21-39
Unmapping a shared memory object
inc lude int munmap(vo id *addr, s ize_t l ength ) ;
Removes mapping described by addr and length from callersvirtual address space
addr : starting address of mappingMust be page-alignedTypically, specify address returned by mmap()
length: number of bytes to unmapRounded up to next page boundaryTypically, specify same length as was given to mmap()
Returns 0 on success, or -1 on errorSHM objects are automatically unmapped on processtermination
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-40 21.5
-
8/11/2019 Posix Shm Slides
21/21
Removing shared memory objects: shm_unlink()
#inc lude int shm_unl ink (const cha r *name) ;
Removes SHM object namename : pathname of SHM objectObject will be deleted once all processes have unmapped itand all FDs from shm_open() have been closedReturns 0 on success, or -1 on errorExample: pshm/pshm_unlink.c
[TLPI 54.4]
Linux/UNIX System Programming c 2013, Michael Kerrisk POSIX Shared Memory 21-41 21.5
Notes