Thread: Linux Message Queue Question!!!

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    1

    Question Linux Message Queue Question!!!

    In the code below I simply want to send a message to an message queue. and output the number of messages in the queue. But the output is 0.....

    could any one provide a solution to this so that once I send a message to the queue, the queue size++?

    Thanks very much!!!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <linux/ipc.h>
    #include <linux/msg.h>
    #include <sys/types.h>
    #include <wait.h>
    #include <unistd.h>
    #include <errno.h>
    
    typedef struct
    {
      long mtype;
      int veh_type;//1: car, 2: truck
      int pid; //msg id
    } msg;
    
    main()
    {    msg message;
        message.mtype=1;
        message.veh_type=0;
        message.pid=0;
    
        int qid=msgget(111,IPC_CREAT|0666);
        msgsnd(qid,&message,sizeof(msg),0);
    
        struct msqid_ds buf;
      
        msgctl(qid,IPC_STAT,&buf);
        int result=buf.msg_qnum;
        printf("%d\n",result);
        msgctl(qid,IPC_RMID,0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    All the functions return a success/fail status.

    Perhaps you can begin with checking those things.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question - linux - gcc - c standards
    By AMAMH in forum C Programming
    Replies: 12
    Last Post: 12-03-2009, 02:49 AM
  2. Lockless Threaded Queue
    By fguy817817 in forum C Programming
    Replies: 27
    Last Post: 11-18-2009, 03:25 PM
  3. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  4. queue help
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-29-2001, 09:38 AM
  5. Question about LINUX
    By River21 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-17-2001, 06:39 PM

Tags for this Thread