Вы находитесь на странице: 1из 3

#include<stdio.

h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/msg.h>
#include<sys/ipc.h>
#include<sys/types.h>

#define MSGSZ 512

struct msg_st{

long int msg_type;


int text[MSGSZ];

};

int buffer[MSGSZ];

int main()
{

int msgid;

struct msg_st *mymessage; //a pointer to the stucture that holds the message

int i = 0;

msgid = msgget( (key_t)6814,IPC_CREAT|0666);

if ( msgid == -1)
{
printf("\nmsgget failed");
exit(EXIT_FAILURE);
}

//mymessage->msg_type = 1; //msgrcv does not require this

/*
* Now that the message stucture is ready receive the message
*/

while(i<2)
{

if ( msgrcv(msgid,(void*)mymessage,MSGSZ*sizeof(int),1,0) == -1)
{
printf("\nmsgrcv failed");
exit(EXIT_FAILURE);
}

buffer[i] = mymessage->text[0];

i++;

printf("\nReceived nos. are %d %d\n",buffer[0],buffer[1]);

printf("\nSum = %d\n",buffer[0]+buffer[1]);
/*
* Done. Now delete the msgQ
*/

if ( msgctl(msgid,IPC_RMID,0) == -1)
{
printf("\nmsgctl failed");
exit(EXIT_FAILURE);
}

exit(EXIT_SUCCESS);

Вам также может понравиться