fork() problems

From: shellcode (shellcode_at_adelphia.net)
Date: 02/29/04


Date: Sat, 28 Feb 2004 23:26:50 -0600

the code:
------fork.c------
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
        pid_t forkval;
        int rv, exitcode;

        forkval=fork();
        switch(forkval)
        {
                case -1:
                        perror("fork");
                        exit(1);
                case 0:
                        printf("CHILD: This is the child process!!\n");
                        printf("CHILD: My PID is: %d\n", getpid());
                        printf("CHILD: My parent's PID is: %d\n", getppid());
                        printf("CHILD: Enter my exit code: ");
                        scanf("%d", &exitcode);
                        printf("CHILD: Exiting with exitcode.. %d\n", exitcode);
                        exit(exitcode);
                default:
                        printf("PARENT: This is the parent process!!\n");
                        printf("PARENT: My PID is: %d\n", getpid());
                        printf("PARENT: My child's PID is: %d\n", forkval);
                        printf("PARENT: Waiting for child to exit....\n");
                        wait(&rv);
                        printf("PARENT: Child died!! Exit code: %d\n",
WEXITSTATUS(rv));
                        printf("PARENT: Exiting....\n");
                        break;
        }
        return 0;
}
------fork.c------

the compilation: gcc -Wall -o fork fork.c

the results:
$ ./fork
CHILD: This is the child process!!
CHILD: My PID is: 1557
CHILD: My parent's PID is: 1556
CHILD: Enter my exit code: PARENT: This is the parent process!!
PARENT: My PID is: 1556
PARENT: My child's PID is: 1557
PARENT: Waiting for child to exit....

im a bit confused at this point. i've looked over the code and the man
pages and i really think i did it right, yet for some reason the CHILD
gets executed before the parent and i'm never given a chance to enter
the exitcode. thank's for help.



Relevant Pages

  • Non-random PIDs
    ... new process ID's, in the way that OpenBSD does. ... I'm the child and my pid is 21116. ... I'm the parent and my pid is 21115. ...
    (RedHat)
  • Re: Creatng 100% separate process from Parent
    ... the "child" process still maintains its ... So another process could start with the same pid as the "parent" - should ... > configuration tool) is typically just run from the start menu. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: forking in c
    ... the very last child it prints out says it has a parent id ... each child process should have a child process of their own. ... The next statement the child executes is ...
    (comp.unix.programmer)
  • Re: Basic Inter process communication question
    ... I have some general questions about UNIX/Solaris IPC. ... child process and the wait for the child process to complete. ... The twist is that I don¹t want the memory of the parent process to be ...
    (comp.unix.programmer)
  • Re: question on fork function
    ... int var; ... when I invoke the forkand create a process whose pid is 3132,my ... fork returns the pid of the child in the parent process. ... not return any pid in the child process - it returns 0. ...
    (comp.unix.programmer)