2021-03-10 17:42:09 +01:00
|
|
|
//
|
2021-03-10 22:56:48 +01:00
|
|
|
// Written for Computer Networks and Systems lab classes
|
2021-03-10 17:42:09 +01:00
|
|
|
// AUTHOR : Sergiusz Warga
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
void child_task() {
|
|
|
|
printf("I am a child\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_pids() {
|
|
|
|
printf("My PID is %d\nMy parent's PID is %d\n", getpid(), getppid());
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_pids_child() {
|
|
|
|
printf("CHILD\nMy PID is %d\nMy parent's PID is %d\n", getpid(), getppid());
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
for (int i = 0; i < 5; ++i) {
|
|
|
|
pid = fork();
|
|
|
|
if (pid == -1) { // If something went wrong.
|
|
|
|
perror("Fork: ");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (pid == 0){
|
|
|
|
print_pids_child();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
printf("I am a parent.\n");
|
|
|
|
print_pids();
|
|
|
|
sleep(1); // This may be required as the parent process won't wait for the child process to finish its execution.
|
|
|
|
return 0;
|
|
|
|
}
|