IPC _synchronization message queue
Hi
i am trying to do c program calculate matrix multiplication that fork 2 child each child calculate two element and send the result via message queue to their parent .
the last printf doesn't appears in output .
any one help me ?
#include<stdio.h>
#include<stdlib.h>
#include<linux/ipc.h>
#include<linux/msg.h>
#include<sys/types.h>
typedef struct mymsgbuf
{
long mtype;
int num,num1,num2,num3;
}mess_t;
int main( int argc,char* argv[])
{
int qid;
key_t msgKey;
pid_t pid;
mess_t buf;
int array1[2][2]={{1,2},{2,2}};
int array2[2][2] ={{3,4},{3,5}};
int val1;
int length;
int cont;
length = sizeof(mess_t) - sizeof(long);
msgKey = (key_t)atoi(argv[1]);
qid=msgget(msgKey,IPC_CREAT|0660);
if(!(pid= fork())){
int val1,val2 =0;
printf ("SON -QID =%d\n",qid);
srand(time(0));
val1 = (1*3)+(2*5);
val2 =(1*4)+(2*5);
buf.mtype=1;
buf.num = val1;
buf.num1 = val2;
msgsnd(qid,&buf,length,0);
printf("\n son-messaged %d\n",buf.num);
printf("\n c[0][1] %d \n",buf.num1);
exit(0);}
else if(!(pid =fork()))
{int val3, val4 =0;
val3 =(2*3)+(2*3);
val4=(2*4)+(2*6);
sleep(10);
buf.mtype = 1;
buf.num2 = val3;
buf.num3 = val4;
sleep(15);
msgsnd(qid,&buf,length,0);
// printf("\n c[1][0]_ch is = %d \n",buf.num3);
sleep(15);
exit(0);
}
else {
wait();
wait();
sleep(30);
msgrcv(qid,&buf,length,1,0);
printf("\n c[0][0] =%d\n",buf.num);
printf("\n c[0][1] =%d",buf.num1);
sleep(30);
msgrcv(qid,&buf,length,1,0);
printf("\n c[1][1] =%d", buf.num2);
msgctl (qid,IPC_RMID,0);
return ;
}}
|