Joseph Do
Joseph Do

Reputation: 23

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:

The second child should do the following:

The parent process should do the following:

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.

Upvotes: 0

Views: 412

Answers (1)

Joni
Joni

Reputation: 111389

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]

Upvotes: 1

Related Questions