-2

I am trying to understand in the communication between the parent and child. Disclaimer: The following is the sample code provided by my professor in class to understand interprocess communication. My main weakness is in file descriptors. I understand that pipe(int array[2]) makes array[0]=fd for std in and array[1] as std out. But how is this communication between those two? I am fairly new to CS. Thanks!

/*
 * This program demonstrates the use of pipes for interprocess communication.
 * The parent process creates a pipe using the "pipe" system call. It then
 * creates a child process and passes the child a file descriptor for one side
 * of the pipe. It then writes a name to its side of the pipe and waits for the
 * child to print a message incorporating the name.
 *
 * Before you attempt to run this program, be sure you've compiled the program
 * named "hw3b.c" and have the executable in a file named "hw3b".
 */

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

/*
 * The name of the file we plan to run.  It's here in a define so that we can
 * change it easily
 */
#define CHILDPROCNAME "hw3b"

/*
 * The behavior of main() is specified by the previous comment
 */

int main(char* argv)
{
    // store the ids of the two file descriptors that serve as the pipe
    int mypipe[2];

    // make the pipe
    pipe(mypipe);

    // child code:
    if (fork() == 0) {
        // execv requires us to create a full invocation, consisting of the
        // string name of the program to run, a string for each command-line
        // argument, and then a null string.  We store all that in this array of
        // strings
        char* myargv[3];

        // we use this to turn an int into a string
        char buf[50];

        //  set up the name of the program to run
        myargv[0] = calloc(strlen(CHILDPROCNAME) + 1, sizeof(char));
        strcpy(myargv[0], CHILDPROCNAME);

        // write one of the pipe's fds to the second parameter
        sprintf(buf, "%d", mypipe[0]);
        myargv[1] = calloc(strlen(buf) + 1, sizeof(char));
        strcpy(myargv[1], buf);

        // third param is null
        myargv[2] = 0;

        // switch to child process
        execv(CHILDPROCNAME, myargv);

        // NB: this should use fprintf and write to stderr
        printf("Uh oh, execv didn't work!\n");

       // crash on failure
       exit(-1);
    }

    // parent code
    else {
       // status variable for storing the result of waitpid
       int status;

       // Send a string across the pipe to the child process
       write(mypipe[1], "Kerry", strlen("Kerry"));

       // wait for the child process to finish
       waitpid(-1, &status, 0);

       // NB: we should use status!
    }

    // close our half of the pipe
    close(mypipe[1]);
}
Jens
  • 69,818
  • 15
  • 125
  • 179
teamaster
  • 403
  • 2
  • 6
  • 16
  • 3
    You need to add the rest of the code. `childproc` isn't even used, and it is defined to be a string. `main` isn't implemented and isn't even a valid forward declaration without a semi-colon after it.. Please post `p1.c` – Ed S. Sep 15 '12 at 18:25
  • Where's the rest of the `main`?... – Sergey Kalinichenko Sep 15 '12 at 18:25
  • IN the main are if preparing for the arguments for childprocess to call execv and then calling it – teamaster Sep 15 '12 at 18:26
  • Apparently your prof wants you to write a program to create two processes that communicate via a pipe. Please give us some more (plenty!) of context to help you. – Jens Sep 15 '12 at 18:41
  • I just added. please do not vote down anymore T__T. I am life-banned now. – teamaster Sep 16 '12 at 00:45

1 Answers1

1

It simply replaces childproc in your program with p1. He might have meant that it depends on the value i.e p1.

P.P
  • 117,907
  • 20
  • 175
  • 238