Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Child process starts after parent process

Tags:

c

linux

fork

I have a simple code to test the fork() function.

    #include<stdio.h>
    #include<unistd.h>
    #define MAX_COUNT 10
    void main(void)
    {
        pid_t pid;
        int i;
        fork();
        pid = getpid();
        for(i = 1; i <= MAX_COUNT; i++)
        {
            printf("PID = %d, i = %d\n", pid, i);
        }
    }

It didn't work as I expected.

My expectation is: parent's result and child's result appear alternately. Can someone explain this and teach me how to fix it? Thanks!

    PID = 3663, i = 1
    PID = 3663, i = 2
    PID = 3663, i = 3
    PID = 3663, i = 4
    PID = 3663, i = 5
    PID = 3663, i = 6
    PID = 3663, i = 7
    PID = 3663, i = 8
    PID = 3663, i = 9
    PID = 3663, i = 10
    PID = 3664, i = 1
    PID = 3664, i = 2
    PID = 3664, i = 3
    PID = 3664, i = 4
    PID = 3664, i = 5
    PID = 3664, i = 6
    PID = 3664, i = 7
    PID = 3664, i = 8
    PID = 3664, i = 9
    PID = 3664, i = 10
like image 640
Tai Nguyen Avatar asked Jul 03 '15 03:07

Tai Nguyen


People also ask

Does child process run before parent process?

The child process is an almost exact copy of the parent process. Both processes continue executing from the point where the fork( ) calls returns execution to the main program. Since UNIX is a time-shared operating system, the two processes can execute concurrently.

Do child and parent processes run concurrently?

Father and child processes do run concurrently, and it is not predictable which process is running at what time. From a first inspection you have a loop that starts from the father process and creates 8 child processes, that on their hand each creates other child processes!

What happens when a parent process creates a child process?

A child process is a process created by a parent process in operating system using a fork() system call. A child process may also be called a subprocess or a subtask. A child process is created as its parent process's copy and inherits most of its attributes.

What happens when a parent process reap its children?

It ceases to exist as a running process, and most/all of its resources (memory, open files, etc.) are released, but it still remains in the process table.


2 Answers

The explanation is simple. Scheduling of processes is up to the kernel. If this is a single core processor then in this run it decided to suspend the child execution and allow the parent process to run first. The parent ran for a few loops before being suspended in favor of the child, and so forth.
In a multiprocessor system both process can run in tandem but the console device will alternate the output depending on timing of interrupts.

There is no assurance a different run will result in the same output. There is also no assurance that different kernel version won't do something else.

If you want the processes to alternate between loops now is the time to learn interprocess communication.

like image 160
Eli Algranti Avatar answered Sep 23 '22 21:09

Eli Algranti


The main process isn't giving up control immediately, and the for loop executes so fast that it is done before the second process gets scheduled. When I execute your code I get non-sequential prints, but the behavior is system dependent.

It sometimes helps to throw sleep() commands around when you're playing with fork(). Try this code...

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

#define MAX_COUNT 10
int main(void)
{
  pid_t pid;
  int i;
  fork();

  pid = getpid();

  srand(pid); // Make sure each process has a different seed

  for(i = 1; i <= MAX_COUNT; i++)
  {
    printf("PID = %d, i = %d\n", pid, i);

    // Sleep 1-3 seconds.
    unsigned int sleep_seconds = rand() % 3 + 1;
    sleep(sleep_seconds);
  }
}
like image 41
QuestionC Avatar answered Sep 25 '22 21:09

QuestionC