UNIX & GNU/Linux - System calls - Using wait()

The wait() syscall is often used with the fork() syscall.

Indeed, with the wait() syscall we can be sure that the child will be executed before the parent process.
In a trivial vision, this is like mutexes for threads.

It means that without the following line, the parent and the child will be executed at the same time:

wait(&status);

If you want to try it, change the line:

while (i < 10)

by this one on both parent and child loops:

while (i < 100000)

They will be executed at the same time!

Let's see it in this wait() tutorial.

Using the wait() syscall

main.c

/**
 * Badprog.com
 * main.c
 */

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

typedef struct s_forky
{
    int value;
} t_forky;

int     main()
{
  int i;
  int status;
  pid_t f;
  t_forky forky;

  forky.value = 0;
  i = 0;
  status = 1;
  f = fork();
  if (f < 0)
    {
      fprintf(stderr, "Error: %s - fork() < 0 (%d)\n", strerror(errno), f);
    }
  else if (f > 0)
    {
      wait(&status);
      printf("\n===== Begin Parent =====\n\n");
      printf("fork() = %d\n", f);
      printf("getpid() = %d\n", getpid());
      while (i < 10)
      {
          printf(" Parent - forky.value = %d\n", forky.value);
          ++forky.value;
          ++i;
      }
    }
  else
    {
      printf("\n===== Begin Child =====\n\n");
      printf("fork() = %d\n", f);
      printf("getpid() = %d\n", getpid());
      while (i < 10)
      {
          printf("  Child - forky.value = %d\n", forky.value);
          ++forky.value;
          ++i;
      }
    }
  printf("status = %d\n", status);
  printf("forky.value = %d\n\n", forky.value);
  printf("===== End =====\n\n");
  return 0;
}

Compiling

gcc main.c -o forkIt ; ./forkIt

Output

===== Begin Child =====

fork() = 0
getpid() = 23817
  Child - forky.value = 0
  Child - forky.value = 1
  Child - forky.value = 2
  Child - forky.value = 3
  Child - forky.value = 4
  Child - forky.value = 5
  Child - forky.value = 6
  Child - forky.value = 7
  Child - forky.value = 8
  Child - forky.value = 9
status = 1
forky.value = 10

===== End =====


===== Begin Parent =====

fork() = 23817
getpid() = 23816
 Parent - forky.value = 0
 Parent - forky.value = 1
 Parent - forky.value = 2
 Parent - forky.value = 3
 Parent - forky.value = 4
 Parent - forky.value = 5
 Parent - forky.value = 6
 Parent - forky.value = 7
 Parent - forky.value = 8
 Parent - forky.value = 9
status = 0
forky.value = 10

===== End =====

So, are you wait() or not? wink

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.