Thread: Interprocess Communication

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    69

    Interprocess Communication

    Hello,

    I am currently writing a program to create two processes and then have them communicate using a pipe. Here is my code:

    Code:
    #include <stdio.h>
    
    int main() {
        int pid, processes[2];
        int fork();
        
        pipe(processes);
        
        pid = fork();
        
        if (pid == 0) {
            close(processes[0]);
            char buf[BUFSIZ];
            
            char message[] = "Hi, this pid 0. Here is something for you!";
            printf("Sending message: %s\n",message);
            write(processes[1],message, strlen(message)+1);
            printf("pid %d send completed\n", pid);
        }
        else {
            close(processes[1]);
            char buf[BUFSIZ];
            
            while ( read(processes[0],buf,BUFSIZ) > 0 )
                printf("pid %d received message: %s \n",pid,buf);
                
        }
    }
    This succeeds in sending a message one way, but how would I go about sending both ways? Basically, what I want to do is have the process that reads the message be able to send a reply back to the process it received the message from. Also, how would I get the time that the message was sent at? I have tried a number of different things to get both of these to work and none have done so. Any help would be greatly appreciated. Thanks.

  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
    > but how would I go about sending both ways?
    Create another pipe of course

    > Also, how would I get the time that the message was sent at?
    Depends what you mean by time
    Wall-clock time you get using the time() function.
    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
    Sep 2003
    Posts
    69
    Ok, I've looked up how to use the time() function and I can't seem to figure it out. Could you show me how I would go about using it in the context of my program? I tried to use and got a really huge number.

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by Salem
    > but how would I go about sending both ways?
    Create another pipe of course
    this is the best (and most portable) solution. but if your using a sun box you could just use the same pipe both ways! (full-duplex pipes)

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    C'mon, just read the manual pages
    They all give you "see also" sections which list related functions, just follow through a bit.

    struct tm *gmtime(const time_t *timep);
    struct tm *localtime(const time_t *timep);
    Convert a time_t into a struct tm

    char *asctime(const struct tm *tm);
    Produces a nice printable string

    size_t strftime(char *s, size_t max, const char *format,
    const struct tm *tm);
    Same as asctime(), but with lots of formatting controls along the line of printf
    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. Replies: 1
    Last Post: 05-05-2009, 04:53 PM
  2. interprocess communication question
    By ac251404 in forum C++ Programming
    Replies: 2
    Last Post: 05-24-2006, 05:34 PM
  3. Secure interprocess communication
    By 3saul in forum C Programming
    Replies: 3
    Last Post: 03-23-2006, 04:25 PM
  4. Looking for communication lib
    By BrownB in forum C Programming
    Replies: 3
    Last Post: 04-27-2005, 10:01 AM
  5. Interprocess Communication and UNIX???
    By atif in forum C Programming
    Replies: 3
    Last Post: 05-17-2002, 04:47 PM