Presentation is loading. Please wait.

Presentation is loading. Please wait.

Textbook: Advanced Programming in the UNIX Environment, 2 nd Edition, W. Richard Stevens and Stephen A. Rago 1 Chapter 15. Interprocess Communication System.

Similar presentations


Presentation on theme: "Textbook: Advanced Programming in the UNIX Environment, 2 nd Edition, W. Richard Stevens and Stephen A. Rago 1 Chapter 15. Interprocess Communication System."— Presentation transcript:

1 Textbook: Advanced Programming in the UNIX Environment, 2 nd Edition, W. Richard Stevens and Stephen A. Rago 1 Chapter 15. Interprocess Communication System Programming http://ecourse.elearning.ccu.edu.tw/ 熊博安 國立中正大學資訊工程學系 pahsiung@cs.ccu.edu.twpahsiung@cs.ccu.edu.twClass: EA-205 (05)2720411 ext. 33119Office: EA-512

2 Slides©2008 Pao-Ann Hsiung, Dept of CSIE, National Chung Cheng University, Taiwan 2 XSI IPC 3 IPC Message Queues Semaphores Shared Memory  Originated in an internal AT&T version of UNIX called “Columbus UNIX”  Later added to System V  Criticized for inventing their own namespace instead of using the file system

3 Slides©2008 Pao-Ann Hsiung, Dept of CSIE, National Chung Cheng University, Taiwan 3 XSI IPC Each IPC structure has a nonnegative integer identifier (large!) When creating an IPC structure, a key must be specified Type: key_t Defined in (long integer) Converted into an identifier by kernel

4 Slides©2008 Pao-Ann Hsiung, Dept of CSIE, National Chung Cheng University, Taiwan 4 Client-Server Rendezvous at same IPC structure (1) Server creates a new IPC structure using key = IPC_PRIVATE Guarantees new IPC structure Server stores returned identifier in some file for client to obtain Disadvantage: file I/O!

5 Slides©2008 Pao-Ann Hsiung, Dept of CSIE, National Chung Cheng University, Taiwan 5 Client-Server Rendezvous at same IPC structure (2) Define a key in a common header Client and server agree to use that key Server creates a new IPC structure using that key Problem: key exists? (msgget, semget, shmget returns error) Solution: delete existing key, create a new one again!

6 Slides©2008 Pao-Ann Hsiung, Dept of CSIE, National Chung Cheng University, Taiwan 6 Client-Server Rendezvous at same IPC structure (3) Client and server agree on a pathname a project ID (char between 0 ~ 255) ftok() converts the 2 values into a key Client and server use that key (cf. 2) Disadvantage: ftok  a function call!

7 Slides©2008 Pao-Ann Hsiung, Dept of CSIE, National Chung Cheng University, Taiwan 7 Permission Structure for IPC struct ipc_perm { uid_t uid; /* owner ’ s EUID */ gid_t gid; /* owner ’ s EGID */ uid_t cuid; /* creator ’ s EUID */ gid_t cgid; /* creator ’ s EGID */ mode_t mode; /* access mode */ … }; Check on your system!

8 XSI IPC permissions PermissionBit User-read User-write (alter) 0400 0200 Group-read Group-write (alter) 0040 0020 Other-read Other-write (alter) 0004 0002 Slides©2008 Pao-Ann Hsiung, Dept of CSIE, National Chung Cheng University, Taiwan 8

9 9 Comparison of features

10 Slides©2008 Pao-Ann Hsiung, Dept of CSIE, National Chung Cheng University, Taiwan 10 Shared Memory Fastest form of IPC no need of data copying between client & server Must synchronize access to a shared memory segment Semaphores are used Record locking can also be used

11 Slides©2008 Pao-Ann Hsiung, Dept of CSIE, National Chung Cheng University, Taiwan 11 Shared Memory Segment Structure struct shmid_ds { struct ipc_perm shm_perm; size_t shm_segsz; /* size in #bytes */ pid_t shm_lpid; /* pid of last shmop */ pid_t shm_cpid; /* pid of creator */ shmatt_t shm_nattch; /* #current attaches */ time_t shm_atime; /* last-attach time */ time_t shm_dtime; /* last-detach time */ time_t shm_ctime; /* last-change time */ … };

12 Slides©2008 Pao-Ann Hsiung, Dept of CSIE, National Chung Cheng University, Taiwan 12 Obtain a shared memory id #include int shmget (key_t key, int size, int flag); Returns: shared memory ID if OK, -1 on error

13 Slides©2008 Pao-Ann Hsiung, Dept of CSIE, National Chung Cheng University, Taiwan 13 Shared Memory Operations #include int shmctl (int shmid, int cmd, struct shmid_ds *buf); Returns: 0 if OK, -1 on error

14 Slides©2008 Pao-Ann Hsiung, Dept of CSIE, National Chung Cheng University, Taiwan 14 Attaching to a process #include void *shmat (int shmid, void *addr, int flag); Returns: pointer to shared memory segment if OK, -1 on error addr=0  allow system to choose the first available address

15 Slides©2008 Pao-Ann Hsiung, Dept of CSIE, National Chung Cheng University, Taiwan 15 Detaching from a process #include int shmdt (void *addr); Returns: 0 if OK, -1 on error

16 Slides©2008 Pao-Ann Hsiung, Dept of CSIE, National Chung Cheng University, Taiwan 16 Figure 15.31: data storage #include #include“apue.h" #defineARRAY_SIZE 40000 #defineMALLOC_SIZE100000 #defineSHM_SIZE100000 #defineSHM_MODE0600/* user read/write */ chararray[ARRAY_SIZE];/* uninitialized data = bss */ int main(void) { intshmid; char*ptr, *shmptr;

17 Slides©2008 Pao-Ann Hsiung, Dept of CSIE, National Chung Cheng University, Taiwan 17 Figure 15.31: data storage printf("array[] from %lx to %lx\n", (unsigned long)&array[0], (unsigned long)&array[ARRAY_SIZE]); printf("stack around %lx\n", (unsigned long)&shmid); if ( (ptr = malloc(MALLOC_SIZE)) == NULL) err_sys("malloc error"); printf("malloced from %lx to %lx\n", (unsigned long)ptr, (unsigned long)ptr+MALLOC_SIZE); if ( (shmid = shmget(IPC_PRIVATE, SHM_SIZE, SHM_MODE)) < 0) err_sys("shmget error"); if ( (shmptr = shmat(shmid, 0, 0)) == (void *) -1) err_sys("shmat error"); printf("shared memory attached from %lx to %lx\n", (unsigned long)shmptr, (unsigned long)shmptr+SHM_SIZE); if (shmctl(shmid, IPC_RMID, 0) < 0) err_sys("shmctl error"); exit(0); }

18 Slides©2008 Pao-Ann Hsiung, Dept of CSIE, National Chung Cheng University, Taiwan 18 Figure 15.31: results $./a.out array[] from 804a080 to 8053cc0 stack around bffff9e4 malloced from 8053cc8 to 806c368 shared memory attached from 40162000 to 4017a6a0

19 Slides©2008 Pao-Ann Hsiung, Dept of CSIE, National Chung Cheng University, Taiwan 19 Memory Layout (Figure 15.31) 0xbffff9e4 0x4017a6a0 0x40162000 0x0806c368 0x08053cc8 0x08053cc0 0x0804a080


Download ppt "Textbook: Advanced Programming in the UNIX Environment, 2 nd Edition, W. Richard Stevens and Stephen A. Rago 1 Chapter 15. Interprocess Communication System."

Similar presentations


Ads by Google