Friday, February 22, 2008

Program for shared memory in c++

See to understand the program first u need to have an idea of shared memory.

Shared memory comes under Inter-Process Communication.

Shared memory is the fastest of all IPC.

Shared memory comes under System V IPC.

SHARED MEMORY:

Shared memory is one of the three System V IPC.

It allows two unrelated processes to access the same logical memory.

Shared memory is a very efficient way of transferring data between two running processes.


In the program i have used these functions.

shmget and shmat for shared memory.

shmget is used to create shared memory.

shmat is used to attach the shared memory to the address space of the process.

When we come to the program,i have created two process i.e process-1 and process-2.

process-1 and process-2 uses same memory i.e shared memory.

I have created a single bit shared memory to control the two process not to access the shared memory at a time.

When process-1 uses the shared memory then process-2 waits until process-1 completes its work and wise versa .

Run the two process at a time (i.e in two terminals ) and u will observe that When process-1 uses the shared memory then process-2 waits until process-1 completes its work and wise versa .

The programs are as follows:

process1.cpp

#include
#include
#include
#include
#include

#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";
cout<<*b;
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;
while(1)
{
if(*b==1)
{
write(b);
}
else
wait();
}
}

Process2.cpp

#include
#include
#include
#include
#include

#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;
cout<<"read is completed";
*b=1;
cout<<"*b is "<<*b;
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;

while(1)
{
if(*b==0)
{
read(b);
}
else
wait();
}
}