Home > Enterprise >  Will this function always output the same?
Will this function always output the same?

Time:10-07

I have the following piece of code

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

int main() {
    int pid, status, number = 300; // Create a process (fork)
    pid=fork();
    if(pid==0) {
        // Child process
        number = 400;
        printf("Child process: Number is %d\n",number); // Terminate OK
        exit(0);
    } else {
        // Father process
        number = 500;
        printf("Father process: Number is %d\n",number); // Wait for child to finish
        wait(&status);
    }
    return 0; // Terminate OK
}

I'm doubting if wether or not the scheduler might affect the output of this code. I believe the scheduler might execute an entire code block first and then execute the other one, hence changing the value of number and changing the print of any of the two threads to the other thread's number.

Is this true? Or is the result deterministic and will always output 400 and 500 in their respective execution threads?

CodePudding user response:

This is not multi-threading. fork() creates a duplicate of the process.

When you run the program, the operating system loads it into memory, creates a process and starts it. The process uses (at least) one memory block that contains the code and one that contains its data (n.b. it is not that simple but the full explanation is beyond the scope of this question).
The code segment contains the code of the program and it is read-only. The data segment contains the data (the variables) and its content can be modified.

The fork() system call creates a new process that is a clone of the original process. The new process uses the same code segment as the original process but it has its own data segment. The data segment is a clone of the original data segment.

The new process does not start running the code from the beginning but its status is the same as the one of the parent process; just after its creation, the new process is running inside the fork() function.

The new process runs independent of its parent process. In the parent process, the function fork() returns a value that is greater than 0 (the process ID of the child process). Inside the child process, fork() returns 0. This way the code can continue with different things in the parent and in the child process.

Each of the two processes then live its own life, without influencing the life of the other process. In your example, the parent changes the value of number to 400 in its data segment then prints it. This change does not affect in any way the value of number (or of any other variable) of the child process; the child process has its own data segment. The child process changes its copy of number to 500 and prints it.

Because the two processes run in parallel, the order of the two displayed values is not determined. It can be 400 then 500 on one execution and the other way around on another execution.

CodePudding user response:

Welp, we just asked the teacher lol. The memory spaces are totally different, which means the other thread shouldn't affect the print output.

  • Related