-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBS4.c
More file actions
48 lines (38 loc) · 1017 Bytes
/
BS4.c
File metadata and controls
48 lines (38 loc) · 1017 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
Write a program in which the parent creates exactly 1 child process. The child process should print its
pid to the standard output and then finish. The parent process waits until it is sure that the child has
terminated. For this students are required to check in the man page of fork to see whether a parent is notified
of child termination via any signal. The parent terminates after it has waited for the child process.
*/
int main(int argc, char* argv[])
{
pid_t pid;
pid = fork();
if(pid < 0)
{
perror("Execvp error");
}
if(pid == 0)
{
printf("I am the child: %d\n", getpid());
exit(0);
}
else
{
printf("Waiting for child to execute...\n");
waitpid(pid, NULL, 2);
printf("A child witd %d has been created!\n", pid);
}
return EXIT_SUCCESS;
}
/*
Output:
Waiting for children to execute...
I am the child: 12102
A child witd 12102 has been created!
*/