You are on page 1of 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>
#include<time.h>

#define MSGSZ 512

struct msg_st{

long int msg_type ;


int text[MSGSZ];

};

int buffer[MSGSZ];

int main()
{

int msg_id;

struct msg_st mymessage; //a stucture that holds the message

int num;

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


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

/**
* if msgget is successful
* send the message
*/

mymessage.msg_type = 1;

srand(time(0));

num = rand()%1000;

buffer[0] = num; //put the no. so read into the buffer

mymessage.text[0]=buffer[0] ;

/*
* Now that the message stucture is populated send the message
*/
if ( msgsnd(msg_id,(void*)&mymessage,MSGSZ*sizeof(int),0) == -1)
{
printf("\nmsgsnd failed");
exit(EXIT_FAILURE);
}

printf("\nSent number = %d\n",num);

exit(EXIT_SUCCESS);

You might also like