Description
Study the following system calls.
Shared memory – shmget, shmat, shmdt, shmctl
Additional optional system calls
Pipe – pipe, mkfifo, mknod, open, read, write, close
Message Queue – msgget, msgsnd, msgrcv, msgctl
Aim:
Develop the following applications that uses interprocess communication concepts using
shared memory.
1. Develop an application for getting a name in parent and convert it into uppercase in child
using shared memory.
2. Develop an client / server application for file transfer using shared memory.
3. Develop an client/server chat application using shared memory.
Comment:
This program should be run as a single program in which parent is one process and child is
another process.
Client / server:
Keep common code and parent code in one program as server.c
Keep common code and child code in another program as client.c
Run server.c in one terminal and client.c in another terminal. Communicate between the two
processes.
Example :
Shared memory :
#include <sys/ipc.h>
# define NULL 0
#include <sys/shm.h>
#include <sys/types.h>
# include
# include
# include
# include
#include <sys/wait.h>
#include
// parent writing a char in shared memory and child reads it and prints it.
int main()
{
int pid;
char *a,*b,c;
int id,n,i;
// you can create a shared memory between parent and child here or you can //create
inside them separately.
id=shmget(111,50,IPC_CREAT | 00666);
pid=fork();
if(pid>0) //parent
{
// id=shmget(111,50,IPC_CREAT | 00666);
a=shmat(id,NULL,0);
a[0]=’d’;
wait(NULL);
shmdt(a);
}
else //child
{
sleep(3);
//id=shmget(111,50,0);
b=shmat(id,NULL,0);
printf(“\n child %c\n”,b[0]);
shmdt(b);
}
shmctl(id, IPC_RMID,NULL);
}