Back to home page

Enduro/X

 
 

    


0001 #include <stdlib.h>
0002 #include <stdio.h>
0003 #include <string.h>
0004 #include <sys/stat.h>
0005 #include <sys/types.h>
0006 #include <sys/poll.h>
0007 #include <fcntl.h>
0008 #include <sys/wait.h>
0009 #include <errno.h>
0010 #include <mqueue.h>
0011 
0012 #define MAX_SIZE 128
0013 
0014 int main(int argc, char argv)
0015 {
0016     mqd_t fd;
0017     struct mq_attr attr;
0018     char buffer[MAX_SIZE + 1];
0019 
0020     struct pollfd fds[1];
0021     
0022 
0023     /* initialize the queue attributes */
0024     attr.mq_flags = 0;
0025     attr.mq_maxmsg = 5;
0026     attr.mq_msgsize = MAX_SIZE;
0027     attr.mq_curmsgs = 0;
0028 
0029     /* cleanup for multiple runs... */
0030     mq_unlink ("/TESTQ");
0031 
0032     /* create the message queue */
0033     fd = mq_open ("/TESTQ", O_CREAT | O_RDWR | O_NONBLOCK, S_IWUSR | S_IRUSR, &attr);
0034 
0035     fds[0].fd = (int)fd;
0036     fprintf(stderr, "#1: fd=%d/%p\n", fds[0].fd, fd);
0037     fds[0].events = POLLIN;
0038 
0039     if (!poll(&fds, 1, 1000))
0040     {
0041         fprintf(stderr, "#1 poll fail: %d:%s\n", errno, strerror(errno));
0042     }
0043     fflush(stderr);
0044 
0045     /* most likely it will crash here... if the system is not like BSD where 
0046          * it have ptr to file descriptor as mqd_t
0047          */
0048     fds[0].fd = *((int*)fd);
0049     fprintf(stderr, "#2: fd=%d\n", fds[0].fd);
0050     fflush(stderr);
0051     fds[0].events = POLLIN;
0052 
0053     if (!poll(&fds, 1, 1000))
0054     {
0055         fprintf(stderr, "#2 poll fail: %d:%s\n", errno, strerror(errno));
0056     }
0057 
0058 }
0059