I'm new to the IPCS concept,i want to achieve that one process creates and initializes a shared memory and then calls another process which attaches to the same shared memory segment and prints the data in the shared memory. However i'm unable to achieve it:
AlgoCommon.h
struct sub_algo_shm
{
int time;
int pno;
};
struct AlgoShm
{
int head;
int tail;
char fFlag;
struct sub_algo_shm algoshm;
};
AlgoThrottle.c
#define key (key_t)111119
#define SHM_SIZE 1024
int main()
{
int shmid ;
struct timeval CurTime;
struct timespec nTime;
struct AlgoShm *shmaddr,*ptr;
ptr = (struct AlgoShm *)malloc(10*sizeof(struct AlgoShm));
//Creating Shared Memory
if((shmid = shmget(key,SHM_SIZE,0644 |IPC_CREAT)) < 0)
{
perror("shmget:error");
}
else
{
printf("Shm created key=[%d] \n Segment Id [%d] ",key,shmid);
//Attaching to Shared Memory
shmaddr = (struct AlgoShm *)shmat(shmid,0,0);
if(shmaddr < 0)
{
perror(" shmat :error");
}
else
{
printf(" SHM Segment attached at [%x] ",shmaddr);
}
//Loading/Initializing Data Blocks in SHM
clock_gettime(CLOCK_REALTIME,&nTime);
printf(" Time in N secs[%ld] \n ",nTime.tv_nsec);
ptr->head = 5;
ptr->tail = 3;
ptr->fFlag = '0';
ptr->algoshm.time = 0;
ptr->algoshm.pno = 1;
//memcpy(shmaddr,&ptr,sizeof(ptr));
printf(" AlgoThrottle|ptr->head [%d] ",ptr->head);
// sleep(1);
system("./AlgoRead");
}
return 0;
}
AlgoRead.c
#define key (key_t)111119
#define SHM_SIZE 1024
int main()
{
int shmid ;
struct AlgoShm *shmaddr,*ptr1 ;
ptr1 = (struct AlgoShm *)malloc(10*sizeof(struct AlgoShm));
if((shmid = shmget(key,SHM_SIZE,0)) < 0)
{
perror(" AlgoRead|shmget:error ");
}
else
{
printf(" AlgoRead|SHM Created shmid [%d] ",shmid);
if((shmaddr = (struct AlgoShm *)shmat(shmid,0,0)) < 0)
{
perror("AlgoRead|shmat error");
}
else
{
//memcpy(ptr1,shmaddr,sizeof(struct AlgoShm));
printf(" AlgoRead|Attached to Segment at Address[%x] \n ",shmaddr);
printf(" AlgoRead|ptr1->head [%d] ptr1->tail [%d] ptr1->fFlag[%c] \n ",ptr1->head,ptr1->tail,ptr1->fFlag);
}
}
return 0;
}
This is my output:
Shm created key=[111119]
Segment Id [1179615678] SHM Segment attached at [4a6b4000] Time in N secs[114594083]
AlgoRead|SHM Created shmid [1179615678] AlgoRead|Attached to Segment at Address[624cb000]
AlgoRead|ptr1->head [36810768] ptr1->tail [0] ptr1->fFlag[▒]
AlgoThrottle|ptr->head [5]
Also,system call's output is showing first then the printf statement even after i used a sleep(1) before invoking system()