I am using C language with GCC compiler on Linux. I have two processes, and I want to pass integer from one process to another i.e from external process to central process and than central process should print it. But my code is not working. Can anyone tell me how to correct it? Here is my code
central.c
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<stdio.h>
#define MsgKey 2345
typedef struct data_struct
{
int temp;
}data;
void main(void)
{
data temp_msg;
int msgqid;
if(msgqid=msgget(MsgKey, 0600 | IPC_CREAT)<0)
{
printf("From Central Process: Msg queue failed");
}
msgrcv(msgqid,&temp_msg,sizeof(temp_msg),2,0);
printf("Value = %d\n",temp_msg.temp);
printf("Central process exiting\n");
}
external.c
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<stdio.h>
#define MsgKey 2345
typedef struct data_struct
{
int temp;
}data;
void main(void)
{
data temp_msg;
int msgqid;
temp_msg.temp=5;
if(msgqid=msgget(MsgKey, 0600 | IPC_CREAT)<0)
{
printf("From External Process: Msg queue failed");
}
if(msgsnd(msgqid,&temp_msg,sizeof(temp_msg),0)<0)
{
printf("Error");
}
printf("External process exiting\n");
}
Than on terminal I typed
gcc -o central central.c
gcc -o external external.c
./central &
./external
I received this "External process exiting" and the external process terminates while central process keeps on running on the background.