If two process are there and one shared memory is there. One wants to write the data and another wants to read the data to the shared memory.
I have written a program in which if process1 is writing to shared memory another will wait and vise varsa. To undersand these programs one should have the knowledge of shared memory in Linux.
I did for Linux platform not for others.
The programs are as follows:
process1.cpp
I have created one bit shared memory for the logic.
process2.cpp
If u want to compile these programs then open two terminals and run process1.cpp and process2.cpp at a time. Then u will observe that if one is writing to the shared memory then another will be waiting vice vasa.
I have written a program in which if process1 is writing to shared memory another will wait and vise varsa. To undersand these programs one should have the knowledge of shared memory in Linux.
I did for Linux platform not for others.
The programs are as follows:
process1.cpp
Code: Cpp
#include<iostream.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include<unistd.h>
#define SHMSZ 1024
int write(int *b)
{
char c;
int shmid;
key_t key;
int *shm;
char *s;
/*
* We'll name our shared memory segment
* "5678".
*/
key = 5678;
/*
* Create the segment.
*/
if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
cerr<<"shmget";
exit(1);
}
/*
* Now we attach the segment to our data space.
*/
if ((shm = (int *)/*(char *)*/shmat(shmid, 0, 0)) == /*(char *)*/(int *) -1) {
cerr<<"shmat";
exit(1);
}
/*
* Now put some things into the memory for the
* other process to read.
*/
// s = shm;
int i;
for (i=0; i<=1000; i++,shm++)
*shm = i;
*b=0;
cout<<"write is complited"<<endl;
cout<<*b<<endl;
return(*b);
}
main()
{
char c;
int shmid;
key_t key;
static int *b;
key = 5679;
/*
* Create the one bit segment.
*/
if ((shmid = shmget(key, 1, IPC_CREAT | 0666)) < 0) {
cerr<<"shmget";
exit(1);
}
/*
* Now we attach the segment to our data space.
*/
if ((b = ( int *)shmat(shmid, 0, 0)) == (int *) -1)
{
cerr<<"shmat";
exit(1);
}
cout<<*b<<endl;
while(1)
{
if(*b==1)
{
write(b);
}
else
wait();
}
}
process2.cpp
Code: Cpp
#include<iostream.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include<unistd.h>
#define SHMSZ 1024
int read(int *b)
{
char c;
int shmid;
key_t key;
int *shm;
char *s;
/*
* We'll name our shared memory segment
* "5678".
*/
key = 5678;
/*
* Create the segment.
*/
if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
cerr<<"shmget";
exit(1);
}
/*
* Now we attach the segment to our data space.
*/
if ((shm = (int *)shmat(shmid, 0, 0)) == (int *) -1) {
cerr<<"shmat";
exit(1);
}
/*now read what the server put in the memory.
*/
int i;
for (i=0; i<=1000; i++,shm++)
cout<<*shm<<endl;
cout<<"read is completed"<<endl;
*b=1;
cout<<"*b is "<<*b<<endl;
return(*b);
}
main()
{
char c;
int shmid;
key_t key;
static int *b;
key = 5679;
/*
* Create the one bit segment.
*/
if ((shmid = shmget(key, 1, IPC_CREAT | 0666)) < 0) {
cerr<<"shmget";
exit(1);
}
/*
* Now we attach the segment to our data space.
*/
if ((b = (int *)shmat(shmid, 0, 0)) == (int *) -1) {
cerr<<"shmat";
exit(1);
}
cout<<" *b is "<<*b<<endl;
while(1)
{
if(*b==0)
{
read(b);
}
else
wait();
}
}


