mq.c 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2010-11-17     Bernard      first version
 */

B
Bernard Xiong 已提交
11 12 13 14 15 16 17 18 19
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <mqueue.h>

#define MQ_NAME_1       "testmsg1"
#define MQ_NAME_2       "testmsg2"
mysterywolf's avatar
mysterywolf 已提交
20 21
#define MSG_SIZE    128
#define MAX_MSG     3
B
Bernard Xiong 已提交
22 23 24 25 26 27

const char *s_msg_ptr[] = {"msg test 1", "msg test 2", "msg test 3"};
char r_msg_ptr_1[MAX_MSG][MSG_SIZE];
char r_msg_ptr_2[MAX_MSG][MSG_SIZE];
pthread_t send1, send2, rev1, rev2;

mysterywolf's avatar
mysterywolf 已提交
28
int * send_1(void * mq)
B
Bernard Xiong 已提交
29
{
mysterywolf's avatar
mysterywolf 已提交
30 31
    int i;
    mqd_t mq1 = *(mqd_t *)mq;
B
Bernard Xiong 已提交
32

mysterywolf's avatar
mysterywolf 已提交
33 34 35 36 37 38 39 40 41
    printf("Enter into send_1 \n");
    for (i = 0; i < MAX_MSG; i++ ) {
        if ( -1 == mq_send(mq1, s_msg_ptr[i], MSG_SIZE, i)) {
            perror("mq_send doesn't return success \n");
            pthread_exit((void *)1);
        }
        printf("[%d] send '%s' in thread send_1. \n", i+1, s_msg_ptr[i]);
    }
    pthread_exit((void *)0);
B
Bernard Xiong 已提交
42 43 44

}

mysterywolf's avatar
mysterywolf 已提交
45
int * send_2(void * mq)
B
Bernard Xiong 已提交
46
{
mysterywolf's avatar
mysterywolf 已提交
47 48
    int i;
    mqd_t mq2 = *(mqd_t *)mq;
B
Bernard Xiong 已提交
49

mysterywolf's avatar
mysterywolf 已提交
50 51 52 53 54 55 56 57 58
    printf("Enter into send_2 \n");
    for (i = 0; i < MAX_MSG; i++ ) {
        if ( -1 == mq_send(mq2, s_msg_ptr[i], MSG_SIZE, i)) {
            perror("mq_send doesn't return success \n");
            pthread_exit((void *)1);
        }
        printf("[%d] send '%s' in thread send_2. \n", i+1, s_msg_ptr[i]);
    }
    pthread_exit((void *)0);
B
Bernard Xiong 已提交
59 60
}

mysterywolf's avatar
mysterywolf 已提交
61
int * receive_1(void * mq)
B
Bernard Xiong 已提交
62
{
mysterywolf's avatar
mysterywolf 已提交
63 64
    int i;
    mqd_t mq1 = *(mqd_t *)mq;
B
Bernard Xiong 已提交
65

mysterywolf's avatar
mysterywolf 已提交
66 67 68 69 70 71 72 73 74
    printf("Enter into receive_1 \n");
    for (i = 0; i< MAX_MSG; i++) {
        if ( -1 == mq_receive(mq1, r_msg_ptr_1[i], MSG_SIZE, NULL) ) {
            perror("mq_receive doesn't return success \n");
            pthread_exit((void *)1);
        }
        printf("[%d] receive '%s' in thread receive_1. \n", i+1, r_msg_ptr_1[i]);
    }
    pthread_exit((void *)0);
B
Bernard Xiong 已提交
75
}
mysterywolf's avatar
mysterywolf 已提交
76
int * receive_2(void * mq)
B
Bernard Xiong 已提交
77
{
mysterywolf's avatar
mysterywolf 已提交
78 79
    int i;
    mqd_t mq2 = *(mqd_t *)mq;
B
Bernard Xiong 已提交
80

mysterywolf's avatar
mysterywolf 已提交
81 82 83 84 85 86 87 88 89
    printf("Enter into receive_2 \n");
    for (i = 0; i< MAX_MSG; i++) {
        if ( -1 == mq_receive(mq2, r_msg_ptr_2[i], MSG_SIZE, NULL) ) {
            perror("mq_receive doesn't return success \n");
            pthread_exit((void *)1);
        }
        printf("[%d] receive '%s' in thread receive_2. \n", i+1, r_msg_ptr_2[i]);
    }
    pthread_exit((void *)0);
B
Bernard Xiong 已提交
90 91 92 93
}

int libc_mq()
{
mysterywolf's avatar
mysterywolf 已提交
94 95 96
    mqd_t mq1 = 0, mq2 = 0;
    struct mq_attr mqstat;
    int oflag = O_CREAT|O_RDWR;
B
Bernard Xiong 已提交
97

mysterywolf's avatar
mysterywolf 已提交
98 99 100 101
    memset(&mqstat, 0, sizeof(mqstat));
    mqstat.mq_maxmsg = MAX_MSG;
    mqstat.mq_msgsize = MSG_SIZE;
    mqstat.mq_flags = 0;
B
Bernard Xiong 已提交
102

mysterywolf's avatar
mysterywolf 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    if( ((mqd_t) -1) == (mq1 = mq_open(MQ_NAME_1,oflag,0777, &mqstat)) ) {
        printf("mq_open doesn't return success \n");
        return -1;
    }
    if( ((mqd_t) -1) == (mq2 = mq_open(MQ_NAME_2,oflag,0777, &mqstat)) ) {
        printf("mq_open doesn't return success \n");
        return -1;
    }
    pthread_create(&send1, NULL, (void *)send_1, (void *)&mq1);
    pthread_create(&send2, NULL, (void *)send_2, (void *)&mq2);
        pthread_create(&rev1, NULL, (void *)receive_1, (void *)&mq1);
        pthread_create(&rev2, NULL, (void *)receive_2, (void *)&mq2);
    pthread_join(send1, NULL);
    pthread_join(send2, NULL);
    pthread_join(rev1, NULL);
    pthread_join(rev2, NULL);

    mq_close(mq1);
    mq_close(mq2);
    mq_unlink(MQ_NAME_1);
    mq_unlink(MQ_NAME_2);

    printf("PASSED\n");
    return 0;
B
Bernard Xiong 已提交
127 128 129
}
#include <finsh.h>
FINSH_FUNCTION_EXPORT(libc_mq, posix mqueue test);