Thread: ipc message-passing hangs

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    ipc message-passing hangs

    hi,

    wrote 2 applications, one for sender and one for receiver. To send a msg, the sender application is started manually at commandline and 1 msg is sent and received. everthing works great. But when i penetrate the commandline and try to send as fast as i can with
    the keyboard, the receiver everytime hangs up and relieves some msgs in the message
    queue. (as i can see with ipcs). Is there any problem with synchronization?

    I use basically:

    msgget(2407, (IPC_CREAT | 0755))
    msgsnd(msgID, &dataMsg, MSGSIZE, 0)) ....

    and

    msgget(2407, 0755);
    msgrcv(msgID, &dataMsg, MSGSIZE, msgTyp, IPC_NOWAIT)) ....

    Greetz

  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
    Posting a whole program which shows the problem would be useful.

    Random lines of where you think the problem is doesn't work.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    you'r right...


    Code:
    IN HEADERFILE---------------
    
    #define MSGSIZE 20
    
    struct myMsg {
    	  long mtype;
    	  char mtext[MSGSIZE];
    	} dataMsg;
    
    
    SENDER -------------
    
    void mysend(long type, int pid)
    {
    	int msgID;
    	char msg[MSGSIZE];
    
    	dataMsg.mtype = type;
            sprintf(msg, "%d", pid);
    	strncpy(dataMsg.mtext, msg, MSGSIZE);
    
    	msgID = msgget(2407, (IPC_CREAT | 0755));
        	if (msgID >= 0) {
            	
            if (-1==msgsnd(msgID, &dataMsg, MSGSIZE, 0)) {
                    perror("msgsnd"); 
            } else {
            	printf("PID gesendet: %s \n", dataMsg.mtext);
            }
        	} else {
            	perror("msgget");
        	}
    } 
    
    int main(int argc, char **argv) {
    
        int pid;
        
        mysend(3333,pid);
    
        return 0;
    }
    
    RECEIVER---------------
    
    struct myMsg my_msgrcv(long type) {
    	int msgID;
    	struct myMsg dataMsg;
    	long msgTyp = 0;
    
    	msgTyp = type;
    
    	msgID = msgget(2407, 0755);
    	if (msgID >= 0) {
    		if (-1==msgrcv(msgID, &dataMsg, MSGSIZE, msgTyp, IPC_NOWAIT)) {
    		    perror("msgrcv"); 
    		} else {
    		     printf("received: %s \n", dataMsg.mtext);
    		}
    	    } else {
    			perror("msgget");
    	    }
    	return dataMsg;
    }
    
    int main() {
    
        while(1) {
               mymsgrcv(3333);
        }
    }
    The original code is to long, but i only shorten in main. It finally always stucks in the line with the msgrcv cmd.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Its an piece of code, that looks similar whereever i searched. I only experimented with
    the flags to have a non-blocking behaviour and a receiver that runs in endless loop.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    .... and please do not regard to the typing error in the RECEIVER main

    1)everything is compiled well and works, my first thought was that it is relied on the flags
    maybe i forgot something.

    2) i checked the also the /proc/sys/kernel/msgm* informations, but they are default-valued.

    3) when i kill the hanging process, and start the receiver again, the last message that is not
    taken from queue is received. could it be that there are some queue entries that are empty for some reasons?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well the first big problem is that MSGSIZE isn't the same as sizeof(dataMsg)

    If it's looping causing errors, then remove the while(1) loop and call it a finite number of times.

    What output do you get?
    Is it all just error messages?
    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.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > .... and please do not regard to the typing error in the RECEIVER main
    Why not?
    If it's approximate code, you're going to get approximate answers.
    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.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    > Well the first big problem is that MSGSIZE isn't the same as sizeof(dataMsg)

    havent seen that, very good, thanks i will work it out

    > What output do you get?

    None, i have some printf() in the while loop and, sorry i forgot: a sleep(1) at the end to see
    the progression of the loop. i can use kbd and type in the cmdline-window. But it simply stops every
    output. Shouldnt it normally go into the if-clause to print perror("msgrcv"); ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Desperate Over Ecs!
    By cookie in forum C Programming
    Replies: 17
    Last Post: 07-16-2008, 01:25 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Character arrays in a Structure
    By vsriharsha in forum C Programming
    Replies: 7
    Last Post: 07-11-2004, 05:36 PM
  4. Message printing problem
    By robert_sun in forum C Programming
    Replies: 1
    Last Post: 05-18-2004, 05:05 AM
  5. Solaris IPC
    By edk in forum C++ Programming
    Replies: 2
    Last Post: 11-21-2001, 01:42 PM