Find the answer to your Linux question:
Results 1 to 9 of 9
Hi, Can anyone show a sample example of POSIX messages. My need is to have two pthreads(two different priorities) and a message is posted from one thread to another. please ...
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Posts
    28

    POSIX messages

    Hi,
    Can anyone show a sample example of POSIX messages.

    My need is to have two pthreads(two different priorities) and a message is posted from one thread to another.

    please help me out!!!

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    There is a wealth of information if you google
    Code:
    POSIX messages
    I didn't see any examples as such, but there's enough information there to get started. If you have any questions, please post a complete (compilable), tiny program which illustrates the problem and we'll take a crack at it.

    That is, unless there's someone else here with examples. Your post has been here an hour, and I wanted you to have something to work with right away.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Dec 2007
    Posts
    28
    Hi,

    In the newly created thread, I mentioned follwoin

    mqdesc = mq_open("First_Message", O_RDWR | O_CREAT)

    but it is failing...the follwoin is the example code. i also learned tht if the message is opened for first time..then we need to give two more arguments, so I also tried the follwoing

    mqdesc = mq_open("First_Message", O_RDWR | O_CREAT, 1, NULL) but still it returns -1

    #include <stdio.h>
    #include <mqueue.h>

    mqd_t mqdesc;

    void* first_thread_run_function(void* arg)
    {
    printf("I am in first thread\n");


    mqdesc = mq_open("First_Message", O_RDWR | O_CREAT);
    printf("mq_open return val %d\n", mqdesc);
    }

    int main(int argc, char *argv[])
    {
    int status;
    struct sched_param thread_param;
    pthread_attr_t thread_attr;
    pthread_t second_thread_id;
    pthread_t first_thread_id;



    if(pthread_attr_init(&thread_attr))
    {
    fprintf(stderr, "error pthread_attr_init()\n");
    exit(1);
    }

    if( pthread_attr_setschedpolicy(&thread_attr, SCHED_RR) )
    {
    fprintf(stderr, "error pthread_attr_setschedpolicy()\n");
    exit(1);
    }
    if( pthread_attr_setinheritsched(&thread_attr, PTHREAD_EXPLICIT_SCHED) )
    {
    fprintf(stderr, "error pthread_attr_setinheritsched()\n");
    exit(1);
    }

    thread_param.sched_priority = 4;

    if( pthread_attr_setschedparam(&thread_attr, &thread_param) )
    {
    fprintf(stderr, "error pthread_attr_setschedparam()\n");
    exit(1);
    }

    status = pthread_create(&first_thread_id, &thread_attr, first_thread_run_function, NULL);
    printf("I am in main\n");

    if(status != 0)
    {
    fprintf(stderr, "pthread_create() error\n");
    exit(1);
    }

    pthread_join(first_thread_id, NULL);
    return EXIT_SUCCESS;
    }

  4. #4
    Just Joined!
    Join Date
    Dec 2007
    Posts
    28
    Hi,

    There is a mistake in naming conventions, now I can send a message. But on the receiving thread, it is returning -1. The following is the sample code.

    plz excuse me...if there is compilation error..i jst copied the code from my project files.

    #include <pthread.h>
    #include <stdio.h>
    #include <mqueue.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/stat.h>
    #include <linux/stat.h>
    #include <sys/fcntl.h>
    #include <sys/types.h>
    #include <linux/types.h>
    #include <linux/posix_types.h>
    #include <errno.h>

    typedef struct {
    int msg_id;
    int msg_val;
    void* msg_ptr;
    }STRUCT_MSG;


    mqd_t mqdesc;

    STRUCT_MSG gst_Message;
    STRUCT_MSG gpst_ReceiveMessage;

    extern int errno;
    void* first_thread_run_function(void* arg)
    {

    printf("I am in first thread\n");

    printf("opening message \n");
    mqdesc = mq_open("/First", O_RDWR | O_CREAT, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), NULL);
    // printf("Error: %s.\n", strerror(errno));
    printf("mq_open return val %d\n", mqdesc);

    mqd_t mq_RetVal = mq_send(mqdesc, (const char*) &gst_Message, sizeof(gst_Message), 0);

    printf("mq_send return val %d\n", mq_RetVal);
    printf("message sent\n");

    while(1);
    }
    void* second_thread_run_function(void* arg)
    {
    size_t msg_len;
    unsigned int msg_prio;

    printf("I am in second thread\n");
    msg_len = sizeof(STRUCT_MSG);
    mqd_t mq_RetVal = mq_receive(mqdesc, (char*) &gpst_ReceiveMessage, msg_len, &msg_prio);
    //printf("Error: %s.\n", strerror(errno));
    printf("mq_receive return val %d\n", mq_RetVal);

    printf("Message received\n");
    }

    int main(int argc, char *argv[])
    {
    int status;
    struct sched_param thread_param;
    pthread_attr_t thread_attr;
    pthread_t second_thread_id;
    pthread_t first_thread_id;



    if(pthread_attr_init(&thread_attr))
    {
    fprintf(stderr, "error pthread_attr_init()\n");
    exit(1);
    }

    if( pthread_attr_setschedpolicy(&thread_attr, SCHED_RR) )
    {
    fprintf(stderr, "error pthread_attr_setschedpolicy()\n");
    exit(1);
    }
    if( pthread_attr_setinheritsched(&thread_attr, PTHREAD_EXPLICIT_SCHED) )
    {
    fprintf(stderr, "error pthread_attr_setinheritsched()\n");
    exit(1);
    }

    thread_param.sched_priority = 4;

    if( pthread_attr_setschedparam(&thread_attr, &thread_param) )
    {
    fprintf(stderr, "error pthread_attr_setschedparam()\n");
    exit(1);
    }

    status = pthread_create(&first_thread_id, &thread_attr, first_thread_run_function, NULL);
    printf("I am in main\n");

    if(status != 0)
    {
    fprintf(stderr, "pthread_create() error\n");
    exit(1);
    }

    thread_param.sched_priority = 5;

    if( pthread_attr_setschedparam(&thread_attr, &thread_param) )
    {
    fprintf(stderr, "error pthread_attr_setschedparam()\n");
    exit(1);
    }

    status = pthread_create(&second_thread_id, &thread_attr, second_thread_run_function, NULL);

    printf("I am in main\n");
    pthread_join(second_thread_id, NULL);
    return EXIT_SUCCESS;
    }

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Each thread needs to have its own variable of type mqd_t, and each thread needs to do an mq_open(). What ties them together is that they should both specify "/First".
    --
    Bill

    Old age and treachery will overcome youth and skill.

  6. #6
    Just Joined!
    Join Date
    Dec 2007
    Posts
    28
    Hi Thanks for your reply.

    I opened the message again in the second thread

    mqdesc = mq_open("/First", O_RDONLY);

    But still "mq_receive" returns -1 and error message is
    Error: Message too long.

    My idea is to to send a message of size STRUCT_MSG and receive the same.
    so in the First thread, I set the attribute of msgsize to sizeof(STRUCT_MSG).

    but on the second thread, still I am receiving msgsize of 8192 and gettin the above error.

    can u plz correct me where I am wrong.

  7. #7
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I'm not really an expert on POSIX message queues. I haven't ever even used them.

    But you say you get the error not on the sending of the message, but on the receiving of it. If that's the case, I'm guessing that attribute msgsize is a property not of the message queue itself, but of your connection to it.

    So have you tried setting attribute msgsize on both the sending side and the receiving side? Just curious.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  8. #8
    Just Joined!
    Join Date
    Jan 2008
    Posts
    1
    you should set msg size in mq_receive() like this

    mq_attr attr;
    mq_getattr(mqdesc,&attr);
    mqd_t mq_RetVal = mq_receive(mqdesc, (char*) &gpst_ReceiveMessage, attr.msgsize, &msg_prio);

  9. #9
    Just Joined!
    Join Date
    Dec 2007
    Posts
    28
    Hi,

    Thanks for all your support.
    Now I can able to receive the messages properly.

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...