DOC PREVIEW
U of I CS 241 - Lecture 39

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)Klara NahrstedtLecture 394/26/2006Contentsz Introduction to inter-process communication functionsz XSI Shared Memory – Access, attach, detach shared memoryz XSI Message Queues– Create message queue, send and receive to/from message queuesAdministrativez Read R&R Chapter 15.1, 15.3, and 15.4z MP5 is on – deadline May 1z Homework 2 – posted April 24– deadline May 3, midnightz Quiz 11 – April 28– Cover R&R Chapter 18.1-18.7 – Cover R&R Chapter 20.1-20.8z Quiz 12 – May 3 Î OPTIONAL !!! – Cover Synchronization– Cover File Systems– Cover Memory ManagementPOSIX: XSI InterprocessCommunication (IPC)z IPC is part of POSIX: XSI Extension z Origin in UNIX System V IPCz IPC includes– Message queues– Semaphore sets– Shared memory z We will go into detail on shared memory IPC mechanism and message queuesIPC Functions z Message Queues functions and their meaning– msgctl /*control*/– msgget /*create or access*/– msgrcv /* receive message*/– msgsnd /*send message*/z 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 Objectsz POSIX:XSI IPC object – unique integer > = 0– Returned from the get function for the object (similar to open file descriptors)z Examples: – msgget returns an integer identifier for message queue objectsz Identifiers are associated with additional data structures in sys/msg.h or sys/shm.hz Creation or accessing of IPC object requires a keyz 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 z 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);z Example: if ((thekey = ftok(“tmp/trouble.c”,1) == (key_t)-1))perror(“failed to derive key from /tmp/trouble.c”);Accessing IPC Resources from Shellz ipcs command displays information about POSIX:XCI IPC resourcesipcs [-qms] [-a | -bcopt]z If no options are given, ipcs gives all information on message queues, share memory segments and semaphore setsz If option –q, -m and –s are given, resources of message queues, shared memory, and semaphore sets are shown, respectively.POSIX:XSI Shared Memoryz Shared memory IPC allows processes to read/write from/to the same memory segmentz We need sys/shm.h header filez Important data structure is the representation of the actual shared memory segment shmid_dsstruct 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 segmentz Need function shmget to access shared memory segment– Function returns an identifier for the shared memorysegment 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 segmentz 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);z Need function shmdt to detach shared memory segment– Decrement shm_nattch in the structure shmidint shmdt(const void *shmaddr); z 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 z 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);z 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 shmidfrom 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 Examplesz 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)) {perror(“Failed to open file”); return 1; }if ((id = shmget(IPC_PRIVATE, sizeof(int), PERM)) == -1) {perror(“failed to create shared memory segment”); return 1; }if ((sharedtotal = (int *)shmat(id, NULL, 0)) == void *)-1) {perror(“failed to attach shared memory segment”); return 1;}Program 15.5 (Parent-Child Communication)if ((childpid = fork())==-1) {perror(“failed to create child process”);if (detachtoremove(id,sharedtotal) == -1)perror(“failed to destroy shared memory segment”); return 1; }if (childpid >0) fd = fd1; /*parent code*/else fd = fd2;while ((bytesread = readwrite(fd, STDOUT_FILENO)) > 0)totalbytes +=bytesread;if (childpid == 0) *sharedtotal = totalbytes; return 0; /*child code*/if (r_wait(NULL) == -1) perror(“failed to wait for child”);else …./* print information */if (detachandremove(id,


View Full Document

U of I CS 241 - Lecture 39

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 Lecture 39
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 Lecture 39 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 Lecture 39 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?