forked from smuos/fork1.c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.c
30 lines (27 loc) · 853 Bytes
/
hello.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define SUCCESS 1
#define FAILURE -1
int
main(int argc, char* argv[])
{
if (argc != 1) {
fprintf(stdout, "Program %s takes no parameters.\n", argv[0]);
exit(FAILURE);
}
printf("Hi stranger! I'm (pid:%d)\n", (int) getpid());
int rc = fork(); //slice off another process
if (rc < -1) {
// Could not cut another process
fprintf(stdout, "OS too hard, could not cut.\n");
exit(0);
} else if (rc == 0) {
fprintf(stderr, "Child can't talk to strangers.\n"); exit(1); printf("Hello, I am child (pid:%d)\n", (int) rc); sleep(1);
} else {
int wc = wait(NULL); //is child finished?
printf("Please leave my child alone, I am %d (wc:%d) (pid:%d)\n",
getpid(), wc, (int) rc);
}
return SUCCESS;
}