-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
30 lines (27 loc) · 802 Bytes
/
main.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
/* Includes */
#include <unistd.h> /* Symbolic Constants */
#include <sys/types.h> /* Primitive System Data Types */
#include <errno.h> /* Errors */
#include <stdio.h> /* Input/Output */
#include <sys/wait.h> /* Wait for Process Termination */
#include <stdlib.h> /* General Utilities */
void myFork (int);
int main()
{
myFork(10); //forking 10 children
}
void myFork (int nChildren) {
int i;
pid_t pid;
for (i = 1; i <= nChildren; i++) {
pid = fork();
if (pid == -1) {
printf("Can't fork child number %d\n",i); //some error handling
return;
}
if (pid == 0) {
printf("I am a child: %d PID: %d\n",i, getpid()); //here you can handle your newly forked child
return;
}
}
}