DOC PREVIEW
U of I CS 241 - Shared Memory and Message Queues (VI)

This preview shows page 1-2-3-24-25-26 out of 26 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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/2006ContentsIntroduction to inter-process communication functionsXSI Shared Memory –Access, attach, detach shared memoryXSI Message Queues–Create message queue, send and receive to/from message queuesAdministrativeRead R&R Chapter 15.1, 15.3, and 15.4MP5 is on – deadline May 1Homework 2 – posted April 24– deadline May 3, midnightQuiz 11 – April 28–Cover R&R Chapter 18.1-18.7 –Cover R&R Chapter 20.1-20.8Quiz 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 IPCIPC 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 ObjectsPOSIX: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 objectsIdentifiers are associated with additional data structures in sys/msg.h or sys/shm.hCreation or accessing of IPC object requires a keyKey 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 setsIf option –q, -m and –s are given, resources of message queues, shared memory, and semaphore sets are shown, respectively.POSIX:XSI Shared MemoryShared memory IPC allows processes to read/write from/to the same memory segmentWe need sys/shm.h header fileImportant 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 segmentNeed 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 segmentNeed 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 ExamplesConsider 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

U of I CS 241 - Shared Memory and Message Queues (VI)

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download Shared Memory and Message Queues (VI)
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Shared Memory and Message Queues (VI) and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Shared Memory and Message Queues (VI) 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?