Search code examples
cprocessforkpid

Processes not running correctly


My assignment is to Write a C program ("procs.c") that creates three processes: a parent process that creates two child processes.

The first child should do the following:

  • display "First child is born, my pid is ..."

  • display ten times the message "First child executes iteration X", where X is the number of the iteration

  • display "First child dies quietly."

The second child should do the following:

  • display "Second child is born, my pid is ..."

  • display ten times the message "Second child executes iteration X", where X is the number of the iteration

  • display "Second child dies quietly."

The parent process should do the following:

  • display "Parent process is born, my pid is ..."

  • create the first child

  • create the second child

  • display "Parent process dies quietly."

Compile the program using gcc and name the executable "procs". Execute the program several times and notice how the output of the two children interlace. All files belonging to this part of the project should be placed in your nova home directory and also posted in Webtycho under the H3 assignment.

A possible output of this program is:

nova> ./procs

Parent process is born, my pid is 7847
First child is born, my pid is 7848    
First child executes iteration: 1
First child executes iteration: 2
First child executes iteration: 3
First child executes iteration: 4
First child executes iteration: 5
Second child is born, my pid is 7849
Second child executes iteration 1
Second child executes iteration 2
Second child executes iteration 3
First child executes iteration: 6
Second child executes iteration 4
Second child executes iteration 5
Second child executes iteration 6
First child executes iteration: 7
Second child executes iteration 7
Second child executes iteration 8
Second child executes iteration 9
Second child executes iteration 10
Second child dies quietly.
First child executes iteration: 8
First child executes iteration: 9
First child executes iteration: 10
First child dies quietly.    
Parent process dies quietly.

However my current output I get is:

nova2> ./procs
Parent process is born, my pid is: 1977
First child is born, my pid is: 1978
First child executes iteration: 1
First child executes iteration: 2
First child executes iteration: 3
First child executes iteration: 4
First child executes iteration: 5
First child executes iteration: 6
First child executes iteration: 7
First child executes iteration: 8
First child executes iteration: 9
First child dies quietly
Second child is born, my pid is: 1979
Second child executes iteration: 1
Second child executes iteration: 2
Second child executes iteration: 3
Second child executes iteration: 4
Second child executes iteration: 5
Second child executes iteration: 6
Second child executes iteration: 7
Second child executes iteration: 8
Second child executes iteration: 9
Second child dies quietly
Bus Error (core dumped)

My code right now is:

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

int main() {
    int pid, i;

    printf("Parent process is born, my pid is: %d\n", getpid());

    pid = fork();
    switch(pid) {
    case -1:
        /* an error occurred */
        printf("Fork error");
        break;

    case 0:
        /* this code is executed by child process #1*/
        printf("First child is born, my pid is: %d\n", getpid());

        for(i=1; i<10; i++)
            printf("First child executes iteration: %d\n", i);

        printf("First child dies quietly\n");

        exit(0);

    default: /* this code is executed by the parent process */

        if(fork()==0) {
            printf("Second child is born, my pid is: %d\n", getpid());

            for(i=1; i<10; i++)
                printf("Second child executes iteration: %d\n", i);

            printf("Second child dies quietly\n");
            exit(0);
        }

        wait();

        printf("Parent process dies quietly.\n");

    }
}

Can anyone help me out I'm not sure what is wrong because I keep going over the coding and it seems right to me.


Solution

  • The wait function takes a parameter, a pointer to an int. Omitting the parameter means some random value may be passed, which means wait will try to write the exit status of the child process to some random memory location, crashing the program. A simple fix is passing NULL:

    wait(NULL);
    

    If you compiled your program with warnings enabled the compiler should warn you about this. For example gcc would tell you:

    test.c: In function ‘main’:
    test.c:40:9: warning: implicit declaration of function ‘wait’ [-Wimplicit-function-declaration]
    test.c:45:1: warning: control reaches end of non-void function [-Wreturn-type]