Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rdemeter
GitHub Repository: rdemeter/so
Path: blob/master/lab3/forktest.c
221 views
1
#include <stdio.h>
2
#include <sys/types.h>
3
#include <sys/wait.h>
4
#include <unistd.h>
5
6
int main(int argc, char **argv)
7
{
8
pid_t childPID = fork();
9
10
if (childPID < 0)
11
{
12
// An error occured
13
fprintf(stderr, "Could not fork!\n");
14
return -1;
15
}
16
else if (childPID == 0)
17
{
18
// We are in the child process
19
printf("The child process is executing...\n");
20
sleep(2);
21
}
22
else
23
{
24
// We are in the parent process
25
if (wait(NULL) < 0)
26
{
27
fprintf(stderr, "Could not wait for child!\n");
28
return -1;
29
}
30
printf("Everything is done!\n");
31
}
32
return 0;
33
}
34
35