AIM : Inter-process
Communication using Shared Memory using System .Application to demonstrate:
Client and Server Programs in which server process creates a shared memory
segment and writes the message to the shared memory segment. Client process
reads the message from the shared memory segment and displays it to the screen
p8.c(CLIENT)
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<stdlib.h>
#define
SHMSZ 27
int
main()
{
int
shmid;
key_t
key;
char
*shm,*s;
key=5678;
if((shmid=shmget(key,SHMSZ,0666))<0)
{
perror("Shmget");
exit(1);
}
if((shm=shmat(shmid,NULL,0))==(char
*)-1)
{
perror("shmat");
exit(1);
}
for(s=shm;*s!=NULL;s++)
putchar(*s);
putchar('\n');
*shm='*';
exit(0);
}
p8b.c (server)
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdlib.h>
#include<unistd.h>
#define
SHMSZ 27
int
main()
{
char c;
int
shmid;
key_t
key;
char
*shm,*s;
key=5678;
if((shmid=shmget(key,SHMSZ,IPC_CREAT|0666))<0)
{
perror("Shmget");
exit(1);
}
if((shm=shmat(shmid,NULL,0))==(char
*)-1)
{
perror("shmat");
exit(1);
}
s=shm;
for(c='a';c<='z';c++)
*s++=c;
*s=NULL;
while(*shm!='*')
sleep(1);
exit(0);
}
/*
No comments:
Post a Comment