CS241 System Programming IPC – Shared Memory and Message Queues (VI)ContentsAdministrativePOSIX: XSI Interprocess Communication (IPC)IPC FunctionsIdentifying and Accessing IPC Objectsftok FunctionAccessing IPC Resources from ShellPOSIX:XSI Shared MemoryAccessing a shared memory segmentAttaching and detaching a shared memory segmentControlling shared memoryExample (detachandremove function)Shared Memory ExamplesProgram 15.5 (Setup Shared Memory)Program 15.5 (Parent-Child Communication)POSIX:XSI Message QueuesAccess a message queueInsert Message into queueProtocol to send message (e.g., mymessage) to a message queueRemove message from message queueControl operations on message queueExample (Program 15.10)Initialize message queue (Program 15.9)Copy messages from message queue to STDOUTSummaryCS241 System Programming IPC – Shared Memory and Message Queues (VI)Klara NahrstedtLecture 394/26/2006ContentsIntroduction to inter-process communication functionsXSI Shared Memory –Access, attach, detach shared memoryXSI Message Queues–Create message queue, send and receive to/from message queuesAdministrativeRead R&R Chapter 15.1, 15.3, and 15.4MP5 is on – deadline May 1Homework 2 – posted April 24– deadline May 3, midnightQuiz 11 – April 28–Cover R&R Chapter 18.1-18.7 –Cover R&R Chapter 20.1-20.8Quiz 12 – May 3 OPTIONAL !!! –Cover Synchronization–Cover File Systems–Cover Memory ManagementPOSIX: XSI Interprocess Communication (IPC)IPC is part of POSIX: XSI Extension Origin in UNIX System V IPCIPC includes–Message queues–Semaphore sets–Shared memory We will go into detail on shared memory IPC mechanism and message queuesIPC Functions Message Queues functions and their meaning– msgctl /*control*/– msgget /*create or access*/– msgrcv /* receive message*/– msgsnd /*send message*/Shared Memory functions and their meaning– shmat /*attach memory to process*/– shmctl /*control*/– shmdt /*detach memory from process */– shmget /*create and initialize or access */Identifying and Accessing IPC ObjectsPOSIX:XSI IPC object –unique integer > = 0–Returned from the get function for the object (similar to open file descriptors)Examples: –msgget returns an integer identifier for message queue objectsIdentifiers are associated with additional data structures in sys/msg.h or sys/shm.hCreation or accessing of IPC object requires a keyKey can be created–By system (IPC_PRIVATE)–By user – pick the key directly–By using ftok – the system generates a key from a specified pathftok Function ftok function allows independent processes to derive the same key based on a known pathname #include <sys/ipc.h> key_t ftok(const char *path, int id);Example: if ((thekey = ftok(“tmp/trouble.c”,1) == (key_t)-1)) perror(“failed to derive key from /tmp/trouble.c”);Accessing IPC Resources from Shell ipcs command displays information about POSIX:XCI IPC resources ipcs [-qms] [-a | -bcopt]If no options are given, ipcs gives all information on message queues, share memory segments and semaphore setsIf option –q, -m and –s are given, resources of message queues, shared memory, and semaphore sets are shown, respectively.POSIX:XSI Shared MemoryShared memory IPC allows processes to read/write from/to the same memory segmentWe need sys/shm.h header fileImportant data structure is the representation of the actual shared memory segment shmid_ds struct ipc_perm shm_perm; /*operation permission structure */ size_t shm_segsz; /*size of segment in bytes*/ pid_t shm_lpid; /*process ID of last operation */ pid_t shm_cpid; /*process ID of creator*/ shmatt_t shm_nattch; /*number of current attaches*/ time_t shm_atime; /*time of last shmat*/ time_t shm_dtime; /*time of last shmdt*/ time_t shm_ctime; /*time of last shctl*/Accessing a shared memory segmentNeed function shmget to access shared memory segment–Function returns an identifier for the shared memory segment associated with the key parameter–Function creates the shared segment if either the key is IPC_PRIVATE or shmflg&IPC_CREAT is non-zero and no shared segment or identifier are associated with key. #include <sys/shm.h> int shmget(key_t key, size_t size, int shmflg);Attaching and detaching a shared memory segmentNeed function shmat to attach shared memory segment–Use structure shmid for needed information–Increment the value of shm_nattach for shmid #include <sys/shm.h> void *shmat(int shmid, const void *shmaddr, int shmflg);Need function shmdt to detach shared memory segment –Decrement shm_nattch in the structure shmidint shmdt(const void *shmaddr); Need function shmctl to deallocate the shared memory –The last process to detach the segment should deallocate the shared memory segment by calling shmctlControlling shared memory Need function shmctl to provide a variety of control operations on the shared memory segment shmid–Control operations are specified via cmd parameter–Interpretation of buf parameter depends on value of cmd#include <sys/shm.h>int shmctl(int shmid, int cmd, struct shmid_ds *buf);cmd control commands–IPC_RMID – remove shared memory segment shmid and destroy corresponding shmid_ds–IPC_SET – set values of fields for shared memory segment shmid from values found in buf–IPC_STAT – copy current values for shared memory segment shmid into bufExample (detachandremove function)#include <stdio.h>#include <errno.h>#include <sys/shm.h> int detachandremove(int shmid,void *shmaddr){ int error =0; if (shmdt(shmaddr) == -1) error = errno; if (shmctl(shmid, IPC_RMID, NULL) == -1) && !error) error = errno; if (!error) return 0; errno=error; return -1; }Shared Memory ExamplesConsider the following example (Program 15.5): –Parent and child process share a small memory segment–The child stores its byte count in the shared memory–The parent waits for the child to finish and then outputs the number of bytes received from each process along with the sum of these values–The parent creates the shared memory segment by using IPC_PRIVATE – allows the memory to be shared with the child–The parent does not access the shared memory until it has detected the termination of the childProgram 15.5 (Setup Shared Memory)../* declare h files */../* start program and declare variables */ if (((fd1 = open(argv[1],O_RDONLY)) == -1) || fd2 = open(argv[2],O_RDONLY)) == -1)) {
View Full Document