/***************************************************
*--------------------------------------------------
* posix ipc message queue¸¦ »ç¿ëÇÑ´Ù.
*--------------------------------------------------
* Creat : 2001. 02. 10 (programed by Cori-Young )
* Site: http://www.byoneself.co.kr
* Update :
*--------------------------------------------------
* Compile : cc -o recv recv.c -lrt
*--------------------------------------------------
* Machine hardware: sun4u
* OS version: 5.7
* Processor type: sparc
* Hardware: SUNW,Ultra-60
***************************************************/
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <time.h>
#include <sched.h>
#include <sys/mman.h>
#include <sys/fcntl.h>
#include <signal.h>
#include <mqueue.h>
#include <errno.h>
#define PMODE 0666
extern int errno;
const char *myname = "/qid";
mqd_t mqfd;
void signalCatcher(int sig);
int main()
{
int i;
int count=1;
char msg_buffer[256];
struct mq_attr attr;
int open_flags = 0;
ssize_t num_bytes_received = 0;
(void)signal(SIGPIPE, signalCatcher);
(void)signal(SIGINT, signalCatcher);
printf("START OF TEST_RECEIVE n");
attr.mq_maxmsg = 20;
attr.mq_msgsize = sizeof(msg_buffer);
attr.mq_flags = 0;
open_flags = O_RDONLY|O_CREAT;
mqfd = mq_open(myname,open_flags,PMODE,&attr);
if (mqfd == -1)
{
perror("mq_open failure from main");
exit(0);
};
while(1)
{
num_bytes_received = mq_receive(mqfd, msg_buffer, sizeof(msg_buffer), 0);
if (num_bytes_received == -1)
{
perror("mq_receive failure on mqfd");
}
else
{
printf("data read for count %d = %s n",count,msg_buffer);
count ++;
}
}
}
void signalCatcher(int sig)
{
if (mq_close(mqfd) == -1)
perror("mq_close failure on mqfd");
if (mq_unlink("myipc") == -1)
perror("mq_unlink failure in test_ipc");
printf("n===>signal catch end program <===n");
sleep(1);
exit(1);
} |
|