Thread: Multipipe Messaging

  1. #1
    Registered User Mcdom34's Avatar
    Join Date
    Jun 2012
    Location
    North Royalton, Ohio, United States
    Posts
    22

    Multipipe Messaging

    Hello, I am currently working on a program that creates one process and forks four other ones. The processes are LOG(parent), A(child), B(child), C(child), and D(child). I'm supposed to be able to send process B ten messages that contain either a C or a D, along with a number, 0-9, incrementally. From there, B is supposed to take each message, and based on whether the message contains a C or D, send it to process C or D, respectively. I've completed both portions, however I can't seem to get process C or D to read each of the messages it's given. The program just seems to skip over pid2=fork() and pid3=fork(). Attached is a copy of the code and the output I've received so far.
    Attached Files Attached Files

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You're using strstr where you should probably use memchr.
    Instead of this
    Code:
    if(strstr(msg, "C")!=NULL)
    you want this
    Code:
    if(memchr(msg, '\0', MSG_SIZE)!=NULL)
    (where MSG_SIZE is defined to be the size of the msg buffer).

    And you should post your code directly to the site, in code tags, like this:

    [code]
    your code here
    [/code]
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User Mcdom34's Avatar
    Join Date
    Jun 2012
    Location
    North Royalton, Ohio, United States
    Posts
    22
    What's the difference?

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    strstr expects a zero-byte terminator. The input string "\0" looks like an empty string and will always return a pointer to the beginning of the other string (it matches the empty string at the beginning, I guess). So if you're testing it logically, it will always be true.

    memchr doesn't assume a null-byte terminator but instead takes a maximum length of memory to search. It occurs to me now that you'd have to initialize the memory to a non-zero value in case there were already some stray zeros in there. You could use memset(msg, A_NON_ZERO_VALUE, MSG_SIZE).

    But maybe get rid of the idea of searching for a zero byte and instead accumulate the number of bytes read:

    Code:
    #define BYTES_TO_READ 3
    
    	if((pid2=fork())==0){ // Process C
    		close(bc[1]);
    		while(1){
    			int nbytes = 0;
    			do {
    				int nb;
    				if ((nb = read(bc[0], &msg, BYTES_TO_READ - nbytes)) < 0)
    					goto break_outer_while; // hit EOF (or error) on pipe
    				nbytes += nb
    			}while (nbytes < BYTES_TO_READ);
    			sleep(1); 
    			printf("C received %s from B\n", msg);  
    		}
    break_outer_while:
    		exit(1);
    	}
    Also, you don't have to define all your pipes in the parent. Only log needs to be there. Process A could create ab and process B could create bc and bd. That way there won't be quite so many pipes floating about.

    You should also post your code directly if you want more people to read it.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    EDIT: I wasn't thinking when I said you don't have to define all your pipes in the parent. Actually, you do have to do that since all the forks are coming from the parent.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Private messaging
    By FiringBlanks in forum General Discussions
    Replies: 34
    Last Post: 10-22-2011, 06:46 AM
  2. Messaging Middleware
    By lucaver in forum C Programming
    Replies: 1
    Last Post: 04-02-2009, 01:05 PM
  3. Asynchronous Messaging
    By birkelbach in forum Networking/Device Communication
    Replies: 1
    Last Post: 05-30-2008, 02:35 PM
  4. Messaging with composition
    By ChadJohnson in forum C++ Programming
    Replies: 3
    Last Post: 01-23-2008, 04:05 PM
  5. Messaging program?
    By CrackDown in forum Linux Programming
    Replies: 2
    Last Post: 12-02-2003, 04:04 AM