0% found this document useful (0 votes)
53 views

Implement Shared Memory and IPC

The document describes code for implementing shared memory and inter-process communication (IPC) between two C programs, a server and client. The server program initializes a shared memory segment, writes data to it, and waits for the client to modify the data. The client program attaches to the shared memory, reads the data written by the server, modifies the data to signal it is done, and exits. The programs demonstrate the basic functionality of shared memory and IPC between processes.

Uploaded by

murali_20c357
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Implement Shared Memory and IPC

The document describes code for implementing shared memory and inter-process communication (IPC) between two C programs, a server and client. The server program initializes a shared memory segment, writes data to it, and waits for the client to modify the data. The client program attaches to the shared memory, reads the data written by the server, modifies the data to signal it is done, and exits. The programs demonstrate the basic functionality of shared memory and IPC between processes.

Uploaded by

murali_20c357
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

10.

Implement Shared memory and IPC


//SHMServer.C
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE

27

void die(char *s)


{
perror(s);
exit(1);
}
int main()
{
char c;
int shmid;
key_t key;
char *shm, *s;
key = 5678;
if ((shmid = shmget(key, MAXSIZE, IPC_CREAT | 0666)) < 0)
die("shmget");
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
die("shmat");
/*
*
* Put some things into the memory for the
*
other process to read.
*
*/
s = shm;
for (c = 'a'; c <= 'z'; c++)
*s++ = c;
/*
* 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);
}

//SHMClient.C
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE
27
void die(char *s)
{
perror(s);
exit(1);
}
int main()
{
int shmid;
key_t key;
char *shm, *s;
key = 5678;
if ((shmid = shmget(key, MAXSIZE, 0666)) < 0)
die("shmget");
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
die("shmat");
//Now read what the server put in the memory.
for (s = shm; *s != '\0'; s++)
putchar(*s);
putchar('\n');
/*
*Change the first character of the
*segment to '*', indicating we have read
*the segment.
*/
*shm = '*';
exit(0);
}

Implement all File Organization Techniques a) Single level directory b) Two level c)
Hierarchical d) DAG

You might also like