2

I am trying to write a program that will print numbers from 1 to 10, but I want the first five numbers to be printed by the child processes, and the last 5 should be printed by the parent processes:

#include <unistd.h>
#include <stdio.h>
#include "process.h"

/**
 * main - Entry point for my program
 *
 * Return: On success, it returns 0.
 * On error, it returns 1
 */
int main(void)
{
        int id = fork();
        int n, i;

        if (id == 0)
                n = 1;
        else
                n = 6;
        for (i = n; i < n + 5; i++)
                printf("%d\n", i);
        return (0);
}

The output is:

6
7
8
9
10
1
2
3
4
5

I am new to UNIX processes, so I dont understand why the parent process output (from 6 - 10) is being printed first. Does the execution of the parent process take precedence over the child process? If I want the child processes to run first (i.e. 1 - 5 printed first), how can I do it?

Leuel Asfaw
  • 316
  • 1
  • 14
  • 2
    By default, process scheduling is controlled by the system, not by the user. You may even get interleaved sequences. That said, there are system calls for the parent to wait for the child to terminate. – mouviciel Sep 07 '22 at 09:14
  • 1
    You have no control over the order of which processes are run by the OS kernel. They can run in any order. If you want to enforce a specific order you need to add synchronization between the processes, through some kind of [inter-process communication (IPC)](https://en.wikipedia.org/wiki/Inter-process_communication). – Some programmer dude Sep 07 '22 at 09:15
  • @mouviciel Can you explain it a bit more? I am new to UNIX processes. – Leuel Asfaw Sep 07 '22 at 09:15
  • for a simple task like this one, wait() ou waitpid() should be enough – Bolo Sep 07 '22 at 09:16
  • 2
    More hints in that related question: [Using Forks with waitpid](https://stackoverflow.com/q/20594411/45249) – mouviciel Sep 07 '22 at 09:17
  • 2
    Multi-process and multithreaded programming are advanced topics. A lot of caveats exist, don't expect to be proficient any time soon. Pick up some books before continuing, because learning it the hard way is very inefficient. – Cheatah Sep 07 '22 at 09:35
  • 1
    @JamesRisner Yes, it did. I am just starting to use stackoverflow that is why I did not know that I can accept an answer, yes it did help me. – Leuel Asfaw Oct 18 '22 at 05:26

1 Answers1

1

This does what you wish:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>

/**
 * main - Entry point for my program
 *
 * Return: On success, it returns 0.
 * On error, it returns 1
*/
int main(void)
{
    int id = fork();
    int n, i;

    if (id < 0) {
        perror("fork failed: ");
        exit(1);
    } else if (id == 0) {
        n = 1;
    } else {
        int status, cid;
        n = 6;
        cid = wait(&status);
        if (cid != id) {
            perror("fork failed: ");
            exit(1);
        }
    }
    for (i = n; i < n + 5; i++) {
        printf("%d\n", i);
    }
    return (0);
}

A couple changes were made to have the children go first.

  • Added a check for id < 0 with fork(), to print the error.
  • Added a call to wait() to allow the child to exit before the parent loop.
  • Check the return from wait to make sure it succeeded.
  • Several headers were added for functions used.
  • Formatting changes, largely irrelevant.

In testing, this is the output:

1
2
3
4
5
6
7
8
9
10
James Risner
  • 5,451
  • 11
  • 25
  • 47