CS2257

Operating System Lab

Exp# 5d

Shared Memory

Aim To demonstrate communication between process using shared memory.

Algorithm Server 1. 2. 3.

Initialize size of shared memory shmsize to 27. Initialize key to 2013 (some random value). Create a shared memory segment using shmget with key & IPC_CREAT as parameter. a. If shared memory identifier shmid is -1, then stop. 4. Display shmid. 5. Attach server process to the shared memory using shmmat with shmid as parameter. a. If pointer to the shared memory is not obtained, then stop. 6. Clear contents of the shared region using memset function. 7. Write a–z onto the shared memory. 8. Wait till client reads the shared memory contents 9. Detatch process from the shared memory using shmdt system call. 10. Remove shared memory from the system using shmctl with IPC_RMID argument 11. Stop Client 1. 2. 3. 4. 5. 6. 7.

Initialize size of shared memory shmsize to 27. Initialize key to 2013 (same value as in server). Obtain access to the same shared memory segment using same key. a. If obtained then display the shmid else print "Server not started" Attach client process to the shared memory using shmmat with shmid as parameter. a. If pointer to the shared memory is not obtained, then stop. Read contents of shared memory and print it. After reading, modify the first character of shared memory to '*' Stop

Result Thus contents written onto shared memory by the server process is read by the client process.

http://cseannauniv.blogspot.com

Vijai Anand

CS2257

Operating System Lab

Program Server /* Shared memory server - shms.c */ #include #include #include #include #include #include



#define shmsize 27 main() { char c; int shmid; key_t key = 2013; char *shm, *s; if ((shmid = shmget(key, shmsize, IPC_CREAT|0666)) < 0) { perror("shmget"); exit(1); } printf("Shared memory id : %d\n", shmid); if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } memset(shm, 0, shmsize); s = shm; printf("Writing (a-z) onto shared memory\n"); for (c = 'a'; c <= 'z'; c++) *s++ = c; *s = '\0'; while (*shm != '*'); printf("Client finished reading\n"); if(shmdt(shm) != 0) fprintf(stderr, "Could not close memory segment.\n"); shmctl(shmid, IPC_RMID, 0); }

http://cseannauniv.blogspot.com

Vijai Anand

CS2257

Operating System Lab

Client /* Shared memory client - shmc.c */ #include #include #include #include #include



#define shmsize 27 main() { int shmid; key_t key = 2013; char *shm, *s; if ((shmid = shmget(key, shmsize, 0666)) < 0) { printf("Server not started\n"); exit(1); } else printf("Accessing shared memory id : %d\n",shmid); if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } printf("Shared memory contents:\n"); for (s = shm; *s != '\0'; s++) putchar(*s); putchar('\n'); *shm = '*'; }

http://cseannauniv.blogspot.com

Vijai Anand

CS2257

Operating System Lab

Output

Server $ gcc shms.c -o shms $ ./shms Shared memory id : 196611 Writing (a-z) onto shared memory Client finished reading

Client $ gcc shmc.c -o shmc $ ./shmc Accessing shared memory id : 196611 Shared memory contents: abcdefghijklmnopqrstuvwxyz

http://cseannauniv.blogspot.com

Vijai Anand

Shared Memory

Algorithm. Server. 1. Initialize size of shared memory shmsize to 27. 2. Initialize key to 2013 (some random value). 3. Create a shared memory segment using shmget with key & IPC_CREAT as parameter. a. If shared memory identifier shmid is -1, then stop. 4. Display shmid. 5. Attach server process to the shared memory ...

12KB Sizes 3 Downloads 230 Views

Recommend Documents

Memory Mapped Files And Shared Memory For C++ -
Jul 21, 2017 - Files and memory can be treated using the same functions. • Automatic file data ... In some operating systems, like Windows, shared memory is.

Shared Memory for Distributed Systems - CiteSeerX
Our business in every ..... Thus the objective is to design a software simulator which in turn will provide a set ...... scheduling strategy to be used for page faults.

Distributed Shared Memory on IP Networks - UW Computer Sciences ...
in memory and execution time. ... times in the range of 100 µs are common in modern “fast” ..... DSM_JOIN messages inside of a small timeout period. If.

Shared Representations for Working Memory and ...
University, Heidelberglaan 2, 3584 CS Utrecht, the Netherlands. Summary ..... Figure 3. Time Course of Decoding Accuracy and Mean Neural Activity in. V1–V3.

Hobbes: CVS for Shared Memory Abstract 1 Introduction
These are in addition to other benefits of the Hobbes model such ... on the merits and limitations of the model. Our current ... that existing programs written for shared memory ..... Our custom workloads take a different approach and are used to ...

On using network attached disks as shared memory - Semantic Scholar
Permission to make digital or hard copies of all or part of this work for personal or classroom use ... executes requests to read and write blocks of data. It can be.

Distributed Shared Memory on IP Networks - UW Computer Sciences ...
distributed shared memory system over an IP network are great. ..... dsm_setup file. Use of this .... services is guaranteed-once delivery, the semantics often.

pdf-1843\shared-memory-application-programming-concepts-and ...
... apps below to open or edit this item. pdf-1843\shared-memory-application-programming-conce ... -strategies-in-multicore-application-programming.pdf.

Shared Governance
Public community college governance stands quite apart from the ... in America's community colleges is virtually a state-by-state choice with some of the.

Shared!Practice!Forum! -
Nepal!earthquake,!the!initial!mental!burden!of!shock!and! ... OPENPediatrics'! clinician! community! site! and! public! website.! Please! go! to!

Developing Shared Purpose.pdf
Page 1 of 3. Groundswell | 1850 M Street NW, Suite 1150 | Washington, D.C. 20036. 202.505.3051 | www.groundswell.org | @grndswell. Activity: Developing ...

Shared, reproducible research
•Open data. •Open research process. •Open research results. •Open access to publications. •Open source. •Open peer review. •Collaborative research. •Citizen participation in research. •… Open, reproducible research. Reproducible r

2017.01 Shared Reading.pdf
Download. Connect more apps... Try one of the apps below to open or edit this item. 2017.01 Shared Reading.pdf. 2017.01 Shared Reading.pdf. Open. Extract.

Practical Memory Checking with Dr. Memory - BurningCutlery
call, which is not easy to obtain for proprietary systems like Windows. ..... Dr. Memory, as there is no way for the application to free this memory: it has lost ..... used by a program,” in Proc. of the 3rd International Conference on. Virtual Exe

Practical Memory Checking with Dr. Memory - BurningCutlery
gramming bugs. These errors include use of memory after free- .... redirected through a software code cache by the DynamoRIO dynamic binary translator.

Lab-4 Shared Memory.pdf
Whoops! There was a problem loading more pages. Retrying... Whoops! There was a problem previewing this document. Retrying... Download. Connect more ...

Shared Governance Model 4.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Shared ...

CLAHRC BITE SHARED HD WEB.pdf
Page 1 of 2. SHAREHD - a quality improvement collaborative to scale up Shared. Haemodialysis Care for patients on hospital based haemodialysis November 2017. CLAHRCBITE. Collaboration for Leadership in Applied Health Research and Care (CLAHRC). A bit

Shared Governance Resolution 2018.pdf
Page 1 of 1. “Shared Governance Resolution of 2018”. I. Title. A. This resolution shall be referred to as the “Shared Governance Resolution.” II. Findings. A. Whereas, the State University of New York (SUNY) and Stony Brook University (SBU) p

Shared-Use Agreement Example 1
WHEREAS, PAAC requested that the Owner, as a public service to transit patrons, permit the .... Legal & Corporate Services Department. AA. APPROVED AS TO ...

The shared circuits model
Printed in the United States of America doi: 10.1017/ ..... action and similar performance, no translation is needed. But things ..... The advance from cooperation plus deceptive copying ..... you reach to pick up the ringing phone, your act and my.