Shared Memory - fac-staff.seattleu.edu

Shared Memory - fac-staff.seattleu.edu

Shared Memory Dr. Yingwu Zhu Overview System V shared memory Let multiple processes attach a segment of physical memory to their virtual address spaces, an efficient means of passing data between processes When write access is allowed for more than one process, an outside protocol or mechanism such as a semaphore can be used to prevent inconsistencies and collisions. Overview: Shared Memory

An efficient means of process communication Creation: shmget() Access control: shmctl() Attached to addr. space: shmat() Detached from addr. space : shmdt() Header files #include #include #include Notes

A process creates a shared memory segment using shmget(). The original owner of a shared memory segment can assign ownership to another user with shmctl(). It can also revoke this assignment. Other processes with proper permission can perform various control functions on the shared memory segment using shmctl(). Once created, a shared segment can be attached to a process address space using shmat(). It can be detached using shmdt() (see shmop()). The attaching process must have the appropriate permissions for shmat(). Once attached, the process can read or write to the segment, as allowed by the permission requested in the attach operation. A shared memory segment is described by a control structure with a unique ID that points to an area of physical memory. The identifier of the segment is called the shmid. The structure definition for the shared memory segment control

structures and prototypes can be found in . Accessing a share memory int shmget(key_t key, size_t size, int shmflg) key: share memory identifier size: number of bytes shmflg: access permissions flags and creation control flags specify a key of IPC_PRIVATE that will be used only by your process and it's children. specify IPC_CREAT, if a segment for the key does not exist, it is created If you also specify IPC_EXCL, you are guranteed a new segment or an error will be returned. #define PERM_UREAD 0400 #define PERM_UWRITE 0200 #define PERM_GREAD 0040 #define PERM_GWRITE 0020 #define PERM_OREAD 0004 #define PERM_OWRITE 0002 return: shared memory segment ID

Controlling a Shared Mem. Seg. int shmctl(int shmid, int cmd, struct shmid_ds *buf); cmd SHM_LOCK -- Lock the specified shared memory segment in memory. The process must have the effective ID of superuser to perform this command. SHM_UNLOCK -- Unlock the shared memory segment. The process must have the effective ID of superuser to perform this command. IPC_STAT -- Return the status information contained in the control structure and place it in the buffer pointed to by buf. The process must have read permission on the segment to perform this command. IPC_SET -- Set the effective user and group identification and access permissions. The process must have an effective ID of owner, creator or superuser to perform this command. IPC_RMID -- Remove the shared memory segment. The buf is a sructure of type struct shmid_ds which is defined in

Attaching a Shared Mem. Seg. void *shmat (int shmid, const void*shmaddr, int shmflg); returns a pointer, shmaddr, to the head of the shared segment associated with a valid shmid Normally we set the 2nd argument, shmaddr, 0, the system picks the attachment address Normally we set the 3rd argument, shmflg, 0, by default, attached segments are accessible for reading and writing Specify the access permissions for the shared memory segment and to request special attachment conditions, such as an aligned address or a read-only segment Detaching a Share Mem. Seg. int shmdt(const void* shmaddr); Detaches the shared memory segment located at the address indicated by shmaddr

Deleting Segment When you detach from the segment, it isn't destroyed. Nor is it removed when everyone detaches from it. You have to specifically destroy it using a call to shmctl(), similar to the control calls for the other System V IPC functions: shmctl(shmid, IPC_RMID, NULL); Examples: client-server communication //server.c main() { char c; int shmid; key_t key; char *shm, *s; /* * We'll name our shared

memory segment * "5678". */ key = 5678; /* * Create the segment. */ if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) { perror("shmget"); exit(1); } /* * Now we attach the segment to our data space. */ if ((shm = (char*)shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } /* * Now put some things into the memory for the * other process to read. */ s = (char*)shm; for (c = 'a'; c <= 'z'; c++) *s++ = c;

*s = NULL; /* * Finally, we wait until the other process * changes the first character of our memory * to '*', indicating that it has read what * we put there. */ while (*shm != '*') sleep(1); exit(0); } //client.c main() { int shmid; key_t key; char *shm, *s; /* * We need to get the segment named * "5678",

created by the server. */ key = 5678; /* * Locate the segment. */ if ((shmid = shmget(key, SHMSZ, 0666)) < 0) { perror("shmget"); exit(1); } /* * Now we attach the segment to our data space. */ if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } /* * Now read what the server put in the memory. */ for (s = shm; *s != NULL; s++) putchar(*s); putchar('\n'); /* * Finally, change the first character of the * segment to '*', indicating we have read * the segment. */ *shm = '*';

exit(0); } Fork and shmat After fork(), the child inherits the attached shared memory segments

Recently Viewed Presentations

  • We Wear the Mask Paul Laurence Dunbar - Mrs. Terry

    We Wear the Mask Paul Laurence Dunbar - Mrs. Terry

    Poetry Analysis Paragraph. Should follow the two-chunk model: TS Topic Sentence: A statement that expresses a theme or meaning of the poem and/or an evaluation of the author's craft. Example: In "We Wear the Mask," Paul Laurence Dunbar uses a...
  • Bringing Hope to Our Local Communities

    Bringing Hope to Our Local Communities

    However several of those who have come off the streets are now also being trained up for ministry Christian Life Assembly has partnered with our Melbourne and Sydney work Each year several churches and ministries such as YWAM work together...
  • TEMPLATE GUIDANCE - soton.ac.uk

    TEMPLATE GUIDANCE - soton.ac.uk

    It removes the need to separately search WebCat and DelphiS. Library Search is the University Library's new search system. It provides a one-search interface across all of our library online and print content and removes the need to separately search...
  • Biblestudyresourcecenter. com biblestudyresourcecenter.com Yitro Yitro Jethro The 17th

    Biblestudyresourcecenter. com biblestudyresourcecenter.com Yitro Yitro Jethro The 17th

    He said, "I bore you on eagles' wings, and brought you to Myself." These words allude to the future as well. In the final redemption, the LORD will gather in the exiles of Israel, carrying on them on the clouds...
  • Rapping up - Casalini Libri

    Rapping up - Casalini Libri

    Mitchell Mathers Be careful what you wish for ... Becky The Bells OCLC perceptions study or eat your spinach Convenience trumps quality Compare Summons data with UCL outcomes? Raise the profile of the library and demonstrate real value (John Shipp)...
  • EGR 2201 Unit 2 Basic Laws

    EGR 2201 Unit 2 Basic Laws

    across a resistor is equal to the current . i. through the resistor times the resistor's resistance . R. In symbols: v = i R. The voltage's polarity and current's direction must obey the passive sign convention, as shown in...
  • Diapositiva 1 - Campigliodolomiti

    Diapositiva 1 - Campigliodolomiti

    Sito leader di sport invernali. 410.000. uniqueusers/month
  • Ct image quality - TEIATH

    Ct image quality - TEIATH

    Optimizing image quality is the process of achieving a balance among the various characteristics (such as detail and noise) and adjusting the image quality to appropriate levels in order to manage the radiation dose to the patient. The clinical utility...