/***************************************************
*--------------------------------------------------
* posix ipc message queue¸¦ »ç¿ëÇÑ´Ù.
*--------------------------------------------------
* Creat : 2001. 02. 10 (programed by Cori-Young )
* Site: http://www.byoneself.co.kr
* Update :
*--------------------------------------------------
* Compile : cc -o send send.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 status = 0;
char msg_buffer[256];
struct mq_attr attr;
int open_flags = 0;
int priority_of_msg;
int count=1;
(void)signal(SIGPIPE, signalCatcher);
(void)signal(SIGINT, signalCatcher);
printf("START OF TEST_SEND n");
attr.mq_maxmsg = 20;
attr.mq_msgsize = sizeof(msg_buffer);;
attr.mq_flags = 0;
open_flags = O_WRONLY|O_CREAT;
mqfd = mq_open(myname,open_flags,PMODE,&attr);
if (mqfd == -1)
{
perror("mq_open failure from main,");
exit(0);
};
priority_of_msg = 1;
while(1)
{
memset(msg_buffer, 0x00, sizeof(msg_buffer));
sprintf(msg_buffer, "%s:%d", "posix message queue test : ", count);
status = mq_send(mqfd, msg_buffer, sizeof(msg_buffer), priority_of_msg);
if (status == -1)
perror("mq_send failure on mqfd");
else
printf("successful mq_send, count = %dn",count);
count++;
sleep(1);
}
}
void signalCatcher(int sig)
{
if (mq_close(mqfd) == -1)
perror("mq_close failure on mqfd");
printf("n===>signal catch end program <===n");
sleep(1);
exit(1);
} |
|