Add POSIX test suite 1.5.2 (http://sourceforge.net/projects/posixtest/).
[dragonfly.git] / test / contrib / posixtestsuite-1.5.2 / stress / mqueues / multi_send_rev_1.c
1
2 /*
3  * Copyright (c) 2002, Intel Corporation. All rights reserved.
4  * Created by:  crystal.xiong REMOVE-THIS AT intel DOT com
5  * This file is licensed under the GPL license.  For the full content
6  * of this license, see the COPYING file at the top level of this
7  * source tree.
8  *
9  * Test whether message queue can work correctly under lots of usage.
10  * 1. Many threads sending/receiving on different message queue.
11  * 2. Set different Priority to the messages in the message queue, to see whether the highest priority is received first.
12  */
13
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <stdlib.h>
18 #include <sys/wait.h>
19 #include <sys/mman.h>
20 #include <string.h>
21 #include <getopt.h>
22 #include <pthread.h>
23 #include <limits.h>
24 #include <mqueue.h>
25
26 #include "posixtest.h"
27
28 #define MSG_SIZE        128
29 #define MAX_MSG         3
30 #define Max_Threads     10
31 #define Name_Size       20
32
33 typedef struct {
34         int ThreadID;
35         mqd_t mqID;
36 }mq_info;
37
38 int* send(void *info)
39 {
40         int i;
41
42         const char *s_msg_ptr[] = {"msg test 1", "msg test 2", "msg test 3"};
43         mq_info send_info;
44         send_info.ThreadID = ((mq_info *)info)->ThreadID;
45         send_info.mqID = ((mq_info *)info)->mqID;
46         printf("Enter into send [%d], mq = %d \n", send_info.ThreadID, send_info.mqID);
47         for (i = 0; i < MAX_MSG; i++ ) {
48                 if ( -1 == mq_send(send_info.mqID, s_msg_ptr[i], MSG_SIZE, i)) {
49                         perror("mq_send doesn't return success \n");
50                         pthread_exit((void *) 1);
51                 }
52                 printf("[%d] send '%s' in thread send [%d]. \n", i+1, s_msg_ptr[i], send_info.ThreadID);
53         }
54         pthread_exit((void *)0);
55
56 }
57 int* receive(void * info)
58 {
59         int i;
60         char r_msg_ptr[MAX_MSG][MSG_SIZE];
61
62         mq_info recv_info;
63         recv_info.ThreadID = ((mq_info *)info)->ThreadID;
64         recv_info.mqID = ((mq_info *)info)->mqID;
65         printf("Enter into receive [%d], mq = %d \n", recv_info.ThreadID, recv_info.mqID);
66         for (i = 0; i< MAX_MSG; i++) {
67                 if ( -1 == mq_receive(recv_info.mqID, r_msg_ptr[i], MSG_SIZE, NULL) ) {
68                         perror("mq_receive doesn't return success \n");
69                         pthread_exit((void *)0);
70                 }
71                 printf("[%d] receive '%s' in thread receive recv [%d]. \n", i+1, r_msg_ptr[i], recv_info.ThreadID);
72         }
73
74         pthread_exit((void *)0);
75 }
76
77 int main(int argc, char *argv[])
78 {
79         const char * MQ_NAME[Max_Threads] = {"/msg1", "/msg2", "/msg3", "/msg4", "/msg5", "/msg6", "/msg7", "/msg8", "/msg9", "/msg10"};
80         mqd_t mq[Max_Threads];
81         struct mq_attr mqstat;
82         int oflag = O_CREAT|O_NONBLOCK|O_RDWR;
83         int num, i;
84         pthread_t sed[Max_Threads], rev[Max_Threads];
85         mq_info info[Max_Threads];
86
87 /* #ifndef  _POSIX_MESSAGE_PASSING
88         printf("_POSIX_MESSAGE_PASSING is not defined \n");
89         return PTS_UNRESOLVED;
90 #endif */
91         if ( (2 != argc) || (( num = atoi(argv[1])) <= 0)) {
92                 fprintf(stderr, "Usage: %s number_of_threads\n", argv[0]);
93                 return PTS_FAIL;
94         }
95         if (num > Max_Threads) {
96                 printf("The num of threads are too large.  Reset to %d\n", Max_Threads);
97                 num = Max_Threads;
98         }
99         memset(&mqstat, 0, sizeof(mqstat));
100         mqstat.mq_maxmsg = MAX_MSG;
101         mqstat.mq_msgsize = MSG_SIZE;
102         mqstat.mq_flags = 0;
103
104         for (i = 0; i < num; i++) {
105                 if( ((mqd_t) -1) == (mq[i] = mq_open(MQ_NAME[i],oflag,0777, &mqstat)) ) {
106                 perror("mq_open doesn't return success \n");
107                 return PTS_UNRESOLVED;
108                 }
109                 printf("mq[%i] created \n", i);
110         }
111         for ( i = 0; i < num; i++) {
112                 info[i].ThreadID = i;
113                 info[i].mqID = mq[i];
114                 pthread_create(&sed[i], NULL, (void *)send, (void *)&info[i]);
115                 pthread_create(&rev[i], NULL, (void *)receive, (void *)&info[i]);
116         }
117         for ( i = 0; i < num; i++) {
118                 pthread_join(sed[i], NULL);
119                 pthread_join(rev[i], NULL);
120         }
121
122         for ( i = 0; i < num; i++) {
123                 mq_close(mq[i]);
124                 mq_close(mq[i]);
125                 mq_unlink(MQ_NAME[i]);
126                 mq_unlink(MQ_NAME[i]);
127         }
128         return PTS_PASS;
129 }