Write a C program that illustrates inter process communication using shared memory system calls.
#include <stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#define SEGSIZE 100
int main(int argc, char *argv[ ])
{
int shmid,cntr;
key_t key;
char *segptr;
char buff[ ]=”Hello world”;
key=ftok(“.”,’s’);
if((shmid=shmget(key, SEGSIZE, IPC_CREAT |
IPC_EXCL | 0666))= = -1)
{
if((shmid=shmget(key,SEGSIZE,0))= = -1)
{
perror(“shmget”);
exit(1);
}
}
else
{
printf(“Creating a new shared memory seg \n”);
printf(“SHMID:%d”, shmid);
}
system(“ipcs –m”);
if((segptr=shmat(shmid,0,0))= =(char*)-1)
{
perror(“shmat”);
exit(1);
}
printf(“Writing data to shared memory…\n”);
strcpy(segptr,buff);
printf(“DONE\n”);
printf(“Reading data from shared memory…\n”);
printf(“DATA:-%s\n”segptr);
printf(“DONE\n”);
print(“Removing shared memory Segment…\n”);
if(shmctl(shmid,IPC_RMID,0)= = -1)
printf(“Can’t Remove Shared memory Segment…\n”);
else
printf(“Removed Successfully”);
}
#include <stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#define SEGSIZE 100
int main(int argc, char *argv[ ])
{
int shmid,cntr;
key_t key;
char *segptr;
char buff[ ]=”Hello world”;
key=ftok(“.”,’s’);
if((shmid=shmget(key, SEGSIZE, IPC_CREAT |
IPC_EXCL | 0666))= = -1)
{
if((shmid=shmget(key,SEGSIZE,0))= = -1)
{
perror(“shmget”);
exit(1);
}
}
else
{
printf(“Creating a new shared memory seg \n”);
printf(“SHMID:%d”, shmid);
}
system(“ipcs –m”);
if((segptr=shmat(shmid,0,0))= =(char*)-1)
{
perror(“shmat”);
exit(1);
}
printf(“Writing data to shared memory…\n”);
strcpy(segptr,buff);
printf(“DONE\n”);
printf(“Reading data from shared memory…\n”);
printf(“DATA:-%s\n”segptr);
printf(“DONE\n”);
print(“Removing shared memory Segment…\n”);
if(shmctl(shmid,IPC_RMID,0)= = -1)
printf(“Can’t Remove Shared memory Segment…\n”);
else
printf(“Removed Successfully”);
}
No comments:
Post a Comment