Find the answer to your Linux question:
Results 1 to 2 of 2
send.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include "msgq_heads.h" int main( int argc, char **argv) { struct msgbuf buf; int mid; key_t key = ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2011
    Posts
    21

    Message Queue Problem


    send.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    #include "msgq_heads.h"

    int main( int argc, char **argv)
    {
    struct msgbuf buf;
    int mid;
    key_t key = 0x10;
    int ch;
    mid = msgget(key,IPC_CREAT|0666);

    if(-1 == mid ){
    perror("msgget");
    exit(0);
    }
    while ( 1 )
    {
    printf("Enter Msg:");
    fgets(buf.mbuf,sizeof buf.mbuf,stdin);

    if(!strcmp(buf.mbuf,"q\n")) {
    if (-1 == msgsnd(mid,&buf,sizeof buf,0))
    {
    perror("msgsnd");
    }
    exit(0);
    }
    printf("Enter type:");
    scanf("%d",&buf.type);
    getchar();
    if (-1 == msgsnd(mid,&buf,sizeof buf,0))
    {
    perror("msgsnd");
    exit(0);
    }
    }
    return 0;
    }


    rece.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    #include "msgq_heads.h"

    int main( int argc, char **argv)
    {
    struct msgbuf buf;
    int mid;
    key_t key = 0x10;

    mid = msgget(key,IPC_CREAT|0666);

    if(-1 == mid ){
    perror("msgget");
    exit(0);
    }
    while ( 1 )
    {
    //printf("Enter type to read:");
    //scanf("%d",&buf.type);

    if ( msgrcv(mid,&buf,sizeof buf,0,0) == -1)
    {
    perror("msgrcv");
    exit(0);
    }

    if(!strcmp(buf.mbuf,"q\n"))
    exit(0);
    printf("type : %d \n", buf.type);
    printf("Msg : %s \n",buf.mbuf);
    }
    return 0;
    }


    msgq_heads.h

    #define SIZE 50
    struct msgbuf {
    int type;
    char mbuf[SIZE];
    };



    Below are the two programs i am using to send and receive messages from the one process to other.
    The receive program is working fine with the statement "msgrcv(mid,&buf,sizeof buf,0,0)".
    That means it is reading all the messages available in the queue.

    But when i use the statement "msgrcv(mid,&buf,sizeof buf,buf.type,0)" by reading the type from the code that
    is commented it seems it is waiting or blocking.

    I dont understand what i am doing wrong, please correct me.

  2. #2
    Just Joined!
    Join Date
    Dec 2011
    Posts
    21
    I understood the problem, it is silly though.
    The datatype what i have choosen for type in msgbuf is
    "int"
    but the msgrcv functions type parameter is
    "long"
    .

    I changed the type to long it is working fine.

    But i wonder the representation of 1 in int and long is supposed to be same ,
    except the other extra bytes in long in case sizeof (long) > sizeof (int) which are any way zeros.

    Infact i have written a program to check the sizes of int and long and the compiler reported
    int is of 4 and long is of 8 bytes and i == l in below program.

    But why was above the program not working when used int instead of long.

    Code:
    #include <stdio.h>
    int main( void )
    {
        struct INT{
            int i;
        }I;
        I.i =1;
    
        struct LONG{
        long l;
        }L;
        L.l = 1;
    
        printf("sizeof int  = %ld" , sizeof (int));
        printf("\nsizeof long  = %ld", sizeof (long));
    
        if (I.i == L.l){
            printf("\ni == l");
        }else{
            printf("\ni != l");
        }
        return 0;
    }

Posting Permissions

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