• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
3  * Created by:  crystal.xiong REMOVE-THIS AT intel DOT com
4  * This file is licensed under the GPL license.  For the full content
5  * of this license, see the COPYING file at the top level of this
6  * source tree.
7  *
8  * Test whether message queue can work correctly under lots of usage.
9  * 1. Many threads sending/receiving on the same message queue.
10  * 2. Set different Priority to the messages in the message queue, to see
11  * 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 MQ_NAME       "/testmsg1"
29 #define MSG_SIZE	128
30 #define MAX_MSG		5
31 #define Max_Threads	100
32 
33 const char *s_msg_ptr[] =
34     { "send_1 1", "send_1 2", "send_1 3", "send_1 4", "send_1 5" };
35 char r_msg_ptr[Max_Threads][MAX_MSG][MSG_SIZE];
36 mqd_t mq = 0;
37 
send(void * ID)38 int *send(void *ID)
39 {
40 	int i;
41 	int ThreadID = *(int *)ID;
42 
43 	printf("Enter into send [%d] \n", ThreadID);
44 	for (i = 0; i < MAX_MSG; i++) {
45 		if (-1 == mq_send(mq, s_msg_ptr[i], MSG_SIZE, i)) {
46 			perror("mq_send doesn't return success \n");
47 			pthread_exit((void *)1);
48 		}
49 		printf("[%d] send '%s' in thread send %d. \n", i + 1,
50 		       s_msg_ptr[i], ThreadID);
51 	}
52 	pthread_exit(NULL);
53 
54 }
55 
receive(void * ID)56 int *receive(void *ID)
57 {
58 	int i;
59 	int ThreadID = *(int *)ID;
60 
61 	printf("Enter into receive[%d] \n", ThreadID);
62 	for (i = 0; i < MAX_MSG; i++) {
63 		if (-1 ==
64 		    mq_receive(mq, r_msg_ptr[ThreadID][i], MSG_SIZE, NULL)) {
65 			perror("mq_receive doesn't return success \n");
66 			pthread_exit((void *)1);
67 		}
68 		printf("[%d] receive '%s' in thread receive[%d]. \n", i + 1,
69 		       r_msg_ptr[ThreadID][i], ThreadID);
70 	}
71 	printf("receive[%d] quit ...\n", ThreadID);
72 	pthread_exit(NULL);
73 }
74 
main(int argc,char * argv[])75 int main(int argc, char *argv[])
76 {
77 
78 	struct mq_attr mqstat;
79 	int oflag = O_CREAT | O_NONBLOCK | O_RDWR;
80 	pthread_t sed[Max_Threads], rev[Max_Threads];
81 	int ThreadID[Max_Threads];
82 	int num, i;
83 
84 /* #ifndef  _POSIX_MESSAGE_PASSING
85 	printf("_POSIX_MESSAGE_PASSING is not defined \n");
86 	return PTS_UNRESOLVED;
87 #endif */
88 	if ((2 != argc) || ((num = atoi(argv[1])) <= 0)) {
89 		fprintf(stderr, "Usage: %s number_of_threads\n", argv[0]);
90 		return PTS_FAIL;
91 	}
92 	if (num > Max_Threads) {
93 		printf("The num of threads are too large.  Reset to %d\n",
94 		       Max_Threads);
95 		num = Max_Threads;
96 	}
97 	memset(&mqstat, 0, sizeof(mqstat));
98 	mqstat.mq_maxmsg = MAX_MSG;
99 	mqstat.mq_msgsize = MSG_SIZE;
100 	mqstat.mq_flags = 0;
101 
102 	if ((mq = mq_open(MQ_NAME, oflag, 0777, &mqstat)) == (mqd_t)-1) {
103 		printf("mq_open doesn't return success\n");
104 		return PTS_UNRESOLVED;
105 	}
106 
107 	for (i = 0; i < num; i++) {
108 		ThreadID[i] = i;
109 		pthread_create(&sed[i], NULL, (void *)send,
110 			       (void *)&ThreadID[i]);
111 		pthread_create(&rev[i], NULL, (void *)receive,
112 			       (void *)&ThreadID[i]);
113 	}
114 
115 	for (i = 0; i < num; i++) {
116 		pthread_join(sed[i], NULL);
117 		pthread_join(rev[i], NULL);
118 	}
119 	mq_close(mq);
120 	mq_unlink(MQ_NAME);
121 	return PTS_PASS;
122 }
123