#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    16
    Rep Power
    0

    Question Child process return results using exit()


    Hello! Given this example code:
    /*
    * The child executes the code inside the if.
    */
    if (pid == 0) {
    /*do something here*/
    exit(numbersRead);
    /*
    * The parent executes the wait.
    */
    while (wait(&status) != pid)
    /* empty */ ;

    What I'd like to do is, spawn a child process, make it work on something while the parent waits for it to finish (let's says, for example, read numbers from stdin) and then return it's results (in this case, return the amount of numbers read) to the parent process, using the exit() call. Also, I can't understand the line
    "while (wait(&status) != pid)", because it seems "status" isn't even used on the whole example program!
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    You know what they say... when the going gets tough, the tough get man. In your case, the man page you're looking for is
    http://www.openbsd.org/cgi-bin/man.c...86&format=html

    Happy reading :)
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    That's an entire subject area in UNIX/Linux programming that you're asking about there. As Scorpions points out, the man pages are your best friend in this new territory. Consult them often.

    If you read the man page for wait, you will see that if a valid status is returned, then it contains the process' exit code. It does not return data read by the child process.

    There are various ways to pass data between processes. The easiest for you to start off with, especially since you're dealing with a parent and child process, should be a pipe. Before you fork, call pipe to create a pair of file descriptors (eg, int fds[2]; ). Then for the child to pass data back to the parent, have the child write to the write end of the pipe (fds[1]) while the parent reads from the read end (fds[0]). When you're done, close the fds with the close() function.

    The source of your example code should have examples of this.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    16
    Rep Power
    0
    If you read the man page for wait, you will see that if a valid status is returned, then it contains the process' exit code. It does not return data read by the child process.
    This was what I was asking; If the child, for example, reads 5 lines from stdin, then I can inform the parent of it by using exit(5), correct?

    As to the man pages, I have a ton of print outs, though they're quite not begginer-friendly...
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally posted by bass20
    As to the man pages, I have a ton of print outs, though they're quite not begginer-friendly...
    I don't know if they all have this, but I once ate at the Hard Rock Cafe in Newport Beach. On the wall in the dining area was a large LED display constantly updating how much rain forest had been destroyed so far. I could just picture in my mind the sudden jump in that count when you printed out those man pages.

    They may not be beginner-friendly, but have you ever seen a Bible that is? They form the basis for command and function documentation, so you do need to learn to use them. Otherwise, you run the risk of getting mostly "RTFM" responses ("read the manual") to your questions.

    Since you're using fork, I know that you're doing this under some kind of UNIX, most likely Linux. The man pages are accessible from the command line and there are man pages for the C library functions; eg:
    man pipe

    If there's also a shell command by the same name as the C function, you'll have to specify which man page group it's in; eg:
    man 2 wait

    I haven't learned yet how that grouping is organized. If you want to search for a particular term in the man pages, use the -k option to list the man pages it occurs on; eg:
    man -k wait | more

    Under Linux, I normally open multiple consoles so that I can work in one and do something else in another, like open a man page. Or, if you're working online, you could open a browser and Google for a particular man page.

    This was what I was asking; If the child, for example, reads 5 lines from stdin, then I can inform the parent of it by using exit(5), correct?
    Yes, you could do that way, but should you? The purpose of the exit code is to inform the parent process (eg, the shell) of the reason why that process had terminated: zero means it ran successfully and non-zero that there if failed and the specific value should say why it failed (you, the programmer, assigns these specific values). Using the exit code for an entirely different purpose is a questionable practice.

    Also, what if you want to send the parent more than just an integer value? This mechanism will not allow you to do that. But something like a pipe would.

    To give you an example of using a pipe, I've attached the lab assignment I'd just written for class. It's tar'd so that the forum will accept it; just use the command "tar xvf pfiles.tar" to extract the single .cpp file.

    The program forks a child process which reads in a file and pipes the file contents back to the parent, who writes it out to another file. Both input and output file names are entered at the command line. That part of the program follows:
    Code:
            if (pid == 0)
            {       // child
                    close(pipefd[0]);
                    if ((fd=open(infilename,O_RDONLY)) == -1)
                    {
                            close(pipefd[1]);
                            perror("open input file error");
                            exit(2);
                    }
                    while (bytesread = read(fd,&buf,MAX_BUFSIZE))
                    {
                            write(pipefd[1],&buf,bytesread);
                    }
                    close(pipefd[1]);
                    close(fd);
            }
            else
            {       // parent
                    close(pipefd[1]);
                    if ((fd=open(outfilename,O_WRONLY | O_CREAT, 0600)) == -1)
                    {
                            close(pipefd[0]);
                            perror("open output file error");
                            exit(2);
                    }
                    while (bytesread = read(pipefd[0],&buf,MAX_BUFSIZE))
                    {
                            write(fd,&buf,bytesread);
                    }
                    close(pipefd[0]);
                    close(fd);
            }
    Remember that pipefd[0] is the read end of the pipe and that pipefd[1] is the write end.

    Hope that helps.
    Attached Files
    Last edited by dwise1_aol; May 15th, 2003 at 03:07 PM.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    16
    Rep Power
    0
    Thank you

IMN logo majestic logo threadwatch logo seochat tools logo