Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    understanding pid() and fork() programming

    Hi everyone.

    I have been doing programming since the last 5 years. however, i have used to cut and paste the template without knowing in depth since i am chasing the time to finish a certain project. The title above seems easy if we read the manual, its just that im kinda confuse just for a second. Hope you guys can help me to give a better understanding for me so that i can upgrade my knowledge.

    Well, during my normal practice cut and paste, i try to understand this code below :-

    pid_t pid;
    		if (pid < 0) {	perror("Erro spawning process : "); exit(EXIT_FAILURE);	}
    		if (pid == 0) {	exit(EXIT_SUCCESS);	}
    		cout << "hello everyone";
    so when i execute this progam, the cout hello everyone will display. This is where my confusion is, in both of the if statement, both process calles an exit() function, but return an EXIT_SUCCES and EXIT_FAILURE, which i understand both function will terminate the calling process, its just that for EXIT_SUCCESS will return a 0 to the calling process indicating the termination is complete without no error and for EXIT_FAILURE is when termination is with some abnormal or error termination. So if both functions terminates, then why does the "hello everyone" appears to the screen? Also, which process is printing out the string, is it the parent or child? I do see examples as it mention that its the parent that is printing out the string, but how? I still think the process is exited at the previous code (EXIT_*), but still, i am wrong, but dont know why...

    Thanks everyone for giving the time to read my post..

  2. #2
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    I suppose you know what fork() does. If not, you can read it up in the respective manual pages. After fork, the pid can represent three states:

    pid > 0 : within parent, new child has this pid; enables one to sig the process
    pid == 0: within child, use pid() to determine process id
    pid < 0 : fork() failed for some reason

    I am sure you can make up the remaining code analysis.
    Last edited by Kloschüssel; 03-28-2011 at 05:20 PM. Reason: corrected the messed up cases

  3. #3
    if(pid<0) error part
    if(pid == 0) child part
    so.. other stuff is done in both child and parent.. anyway child has exited.. so parent will put the hello everyone.

  4. $spacer_open
  5. #4
    Linux User
    Join Date
    Nov 2008
    Tokyo, Japan
    Fork creates an exact copy of the parent process in memory except for two main differences:
    1. the return value of the "fork()" is different in the parent compared to the child, as the other posts explained above
    2. the process ID is different, and you can use the "pid()" command to check on the new process ID.
    There are a few other differences, but they are not relevant to your question.

    The return value is how your code can tell inside which process it is executing: the original parent, or the new child. After the fork() command, you must think of two different programs that are both executing the exact same code immediately after the "fork()" command. Both programs will do the exact same thing until execution of one of them changes its behavior conditionally according to the return value of the "fork()" command.

    You can use an IF statement to change the behavior of the child process. For example, the child process can execute another program using the "exec()" function. In this code:
    if (pid == 0) { exit(EXIT_SUCCESS); }
    The child is created but it immediately exits and is deleted. Using the "exec()" command will execute a new process, replacing the image of the previous process in memory, but still keeping all the file-handles open for communication.

    The exit code seen in the shell which executed the process will be the exit code of the parent, not the child, unless you tell it explicitly to exit with the exit-code of the child.

  6. #5
    Thank you everyone. Now i have a clear understanding and yeah, it is straight forward though. Appreciate all of your reply guys. Thanks...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts