954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

forking, why child never exists?

Hello there,

I'm new to C programming and am following a course in C.
I've got an example code for piping. It's not a very hard code to understand. It goes like this:

#include <stdio.h>
#include <unistd.h> 
#include <stdlib.h>
#include <string.h>


#define READ_END       0
#define WRITE_END      1

const char banner [] = "hello there\n";

int main()
{
  int pipe_ends[2];
  pid_t pid;

  if(pipe(pipe_ends)) {
    printf("Could not create pipe\n");
    return -1;
  }

  pid = fork();
  
  if(pid < 0) {
    printf("Fork failed\n");
    return -1;
  }

  if(pid > 0) { /* parent */
    int i;
    close(pipe_ends[READ_END]);

    for(i=0; i<10; i++) {
      printf("Parent Writing [%d]...\n",i);
      write(pipe_ends[WRITE_END], banner, strlen(banner));
    }

    exit(EXIT_SUCCESS);
  }

  if(pid == 0) { /* child */
    char buff[128];
    int count;
    int x = 0;
    close(pipe_ends[WRITE_END]);
    while((count = read(pipe_ends[READ_END], buff, 128)) > 0) {
      write(STDOUT_FILENO, &buff, count);
      sleep(1);
    }
  }
  return 0;
}


Now my problem is not with the pipes. But when you run this, the program never ends!!! I experimented adding few prinf lines and found out that the child reaches the last code (i.e. the return 0 line) but it seems never to exit. Why is this?

I'm really new to C and please excuse me if I'm asking you something really stupid. Please help me.

JDevelop
Newbie Poster
15 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

Here's the output from my computer

Parent Writing [0]...
Parent Writing [1]...
hello there
Parent Writing [2]...
Parent Writing [3]...
Parent Writing [4]...
Parent Writing [5]...
Parent Writing [6]...
Parent Writing [7]...
Parent Writing [8]...
Parent Writing [9]...
[gerard@localhost test]$ hello there
hello there
hello there
hello there
hello there
hello there
hello there
hello there
hello there

[g@localhost]$

Just hit the enter key when the program is over.

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

Yeh, it finishes when you press enter. But WHY? Why do you have to press enter? Why doesn't it automatically finishes like many other programs when it reaches the 'return' statement?

JDevelop
Newbie Poster
15 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

Ok, if none of you are going to help, I'll solve that myself. :D
I really didn't care about that (thanks for posting the output gerard. That saved me a little bit of work) command line prompt before the child start its wok.

Parent Writing [9]... [gerard@localhost test]$ hello there hello there

Now I know what happens.

First the parent runs its quota of time and exits within that time.

And the next chance to run is for the terminal and it does its work by printing "[gerard@localhost test]$" and its quota expires.

Next turn is to the child and it does its work before its quota expires and the terminal gets its chance again.

Now, the process does not know that it was pre-empted. It just starts its work from where it stopped. The terminal has already printed "[gerard@localhost test]$" and now its waiting for the user's input. This is what I mistook as the "child never exits".

By the way sorry for "exists", it should be "exits".

Thanks, everybody

JDevelop
Newbie Poster
15 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: