Tuesday, 3 April 2012

Shared Data Example


How to share the data between two program.

Program 1:
=======
#include<iostream.h>
#include<sys/wait.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include<unistd.h>
#include <stddef.h>
#include <stdio.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;
int *w=new int;
*w=10;
//while(1)    {
if(*b==1)
{
write(b);
}
else
wait(w);
//}
}


Prgram 2:
======
#include<iostream.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include<unistd.h>
#include<sys/wait.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;
int *w=new int;
*w=10;
while(1)
{
if(*b==0)
{
read(b);
}
else
wait(w);
}
}

No comments:

Post a Comment

AWS Data Pipeline Services

https://www.youtube.com/watch?v=tykcCf-Zz1M