Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practice04 / ex4.c
blob: 1014f5cf662d70edb65866080c666f0516e3246f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <unistd.h> // fork, execlp
#include <sys/types.h> // pid_t
#include <sys/wait.h> // wait
#include <stdio.h> // printf

int main(int argc, char* argv[]) {
	pid_t fork1 = fork();
	pid_t fork2 = fork();
	if (fork1 == 0) { // in child 1
		execlp(argv[1], argv[1], (char*)NULL);
	}
	else if (fork2 == 0) { // in child 2
		execlp(argv[2], argv[2], (char*)NULL);
	}
	else {
		int firstStatus, secondStatus;
		pid_t firstPID = wait(&firstStatus);
		pid_t secondPID = wait(&secondStatus);
		printf("%s %d\n", argv[1 + (firstPID != fork1)], firstStatus);
		printf("%s %d\n", argv[1 + (secondPID != fork1)], secondStatus);
	}
}