47 lines
1.0 KiB
C
47 lines
1.0 KiB
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <sys/types.h>
|
||
|
#include <sys/ipc.h>
|
||
|
#include <sys/shm.h>
|
||
|
|
||
|
#include "info.h"
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
int id;
|
||
|
struct info *ctrl;
|
||
|
|
||
|
id = shmget( KEY,MSIZ, 0 );
|
||
|
// get the id of the already created shared memory segment
|
||
|
// .. function to create is same as getting id of the already created shared memory segment
|
||
|
if ( id < 0 )
|
||
|
{
|
||
|
//error … ;
|
||
|
exit(1) ;
|
||
|
}
|
||
|
|
||
|
// attach a local pointer to the shared memory
|
||
|
ctrl = (struct info *) shmat( id, 0, 0);
|
||
|
if ( ctrl <= (struct info *) (0) )
|
||
|
{
|
||
|
//error … ;
|
||
|
exit(1) ;
|
||
|
}
|
||
|
|
||
|
while (ctrl->flag!=-1)
|
||
|
{
|
||
|
// if flag is 1, add the numbers from shared memory, set flag to 0
|
||
|
if (ctrl->flag==1)
|
||
|
{
|
||
|
ctrl->sum = ctrl->value1+ctrl->value2;
|
||
|
printf("Sum: %f + %f = %f\n", ctrl->value1, ctrl->value2, ctrl->sum);
|
||
|
ctrl->flag = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//detach the pointer from the shared memory
|
||
|
// .. we do not need to delete here, can be done in either of the process
|
||
|
shmdt(ctrl);
|
||
|
exit(0);
|
||
|
}
|