Home

Search IconIcon to open search

Managing processes in C

UNIX processes

# Functions

sigsuspend(const sigset_t *mask) - tbd
fork(), wait(), etc. - see pipex

# Notes

There’s a catch when fork()ing after printf(). The output from printf() may be stored in a buffer, and that buffer gets copied after fork(). So the buffer may be flushed twice (and printing something twice). Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>

int main(int argc, char const *argv[])
{
	printf("Wat");
	int id = fork();
	if (id != 0)
		wait(NULL);
	return 0;
}

Output:

1
WatWat

It doesn’t happen if you call exec() after fork() though, because exec() starts with fresh stdio buffers. More here.