0% found this document useful (0 votes)
6 views1 page

Shared Memmory

This C program demonstrates the creation, writing, reading, and removal of a shared memory segment using System V IPC. It checks for existing shared memory and handles errors appropriately while displaying the shared memory ID and contents. The program concludes by attempting to remove the shared memory segment and reports the success of this operation.

Uploaded by

Thejus kj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Shared Memmory

This C program demonstrates the creation, writing, reading, and removal of a shared memory segment using System V IPC. It checks for existing shared memory and handles errors appropriately while displaying the shared memory ID and contents. The program concludes by attempting to remove the shared memory segment and reports the success of this operation.

Uploaded by

Thejus kj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include<stdio.

h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#define SEGSIZE 100
int main(int argc, char *argv[ ]){ //@CEK22AM022 11-03-23{
int shmid,cntr;
key_t key;
char *segptr,buff[]="Shared_Memmory......";
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=(char*)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");
printf("Removing shared memory Segment…\n");
if(shmctl(shmid,IPC_RMID,0)== -1)
printf("Can‟t Remove Shared memory Segment…\n");
else
printf("Removed Successfully");
printf("\n");
}

You might also like