-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntro_to_PIDs.c
52 lines (51 loc) · 1.43 KB
/
Intro_to_PIDs.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int num, char *n[])
{
int N = atoi(n[1]);
if (num == 1)
{
printf("\nERROR: No Parameters is specified.\nUsage: ./filename <int parameter>\n");
exit(1);
}
else if (num > 2)
{
printf("\nERROR: There are too many Parameters.\nUsage: ./filename <int parameter>\n");
exit(1);
}
else if (N == 0)
{
printf("\nERROR: Invalid Parameter.\nUsage: ./filename <int parameter>\n");
}
else
{
pid_t ppid = getpid();
for (int i = 0; i < N; i++)
{
for (int k = 0; k < 3; k++)
{
pid_t pid = fork();
if (pid > 0)
{
if (getpid() == ppid) //Is the Parent Process
{
printf("This is the main process, my PID is: %d\n", ppid);
sleep(2);
}
}
else
{
if (getppid() == ppid) //Is the child process
{
printf("This is a child process, my PID is: %d, my parent PID is: %d\n", getpid(), getppid());
sleep(2);
}
}
}
}
}
}