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
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.
26 #include "posixtest.h"
30 #define Max_Threads 10
42 const char *s_msg_ptr[] = {"msg test 1", "msg test 2", "msg test 3"};
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);
52 printf("[%d] send '%s' in thread send [%d]. \n", i+1, s_msg_ptr[i], send_info.ThreadID);
54 pthread_exit((void *)0);
57 int* receive(void * info)
60 char r_msg_ptr[MAX_MSG][MSG_SIZE];
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);
71 printf("[%d] receive '%s' in thread receive recv [%d]. \n", i+1, r_msg_ptr[i], recv_info.ThreadID);
74 pthread_exit((void *)0);
77 int main(int argc, char *argv[])
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;
84 pthread_t sed[Max_Threads], rev[Max_Threads];
85 mq_info info[Max_Threads];
87 /* #ifndef _POSIX_MESSAGE_PASSING
88 printf("_POSIX_MESSAGE_PASSING is not defined \n");
89 return PTS_UNRESOLVED;
91 if ( (2 != argc) || (( num = atoi(argv[1])) <= 0)) {
92 fprintf(stderr, "Usage: %s number_of_threads\n", argv[0]);
95 if (num > Max_Threads) {
96 printf("The num of threads are too large. Reset to %d\n", Max_Threads);
99 memset(&mqstat, 0, sizeof(mqstat));
100 mqstat.mq_maxmsg = MAX_MSG;
101 mqstat.mq_msgsize = MSG_SIZE;
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;
109 printf("mq[%i] created \n", i);
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]);
117 for ( i = 0; i < num; i++) {
118 pthread_join(sed[i], NULL);
119 pthread_join(rev[i], NULL);
122 for ( i = 0; i < num; i++) {
125 mq_unlink(MQ_NAME[i]);
126 mq_unlink(MQ_NAME[i]);