AAE-CNAS-Labs/Topic-3-Forking/task_2_4.c

82 lines
2.6 KiB
C
Raw Permalink Normal View History

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>
2021-03-10 22:08:21 +01:00
#include <sys/time.h>
2021-03-10 17:42:09 +01:00
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
2021-03-10 19:46:05 +01:00
#include <time.h>
2021-03-10 17:42:09 +01:00
2021-03-10 22:56:48 +01:00
const char * get_timestamp() {
2021-03-10 22:08:21 +01:00
struct timeval now;
gettimeofday(&now, NULL);
char buff[127];
sprintf(&buff, "us : %d", now.tv_usec);
return buff;
}
2021-03-10 17:42:09 +01:00
void parent_before_child() {
pid_t pid;
2021-03-10 19:46:05 +01:00
printf("\nparent_before_child()\n");
2021-03-10 17:42:09 +01:00
pid = fork();
if (pid == -1) { // If something went wrong.
perror("Fork: ");
exit(EXIT_FAILURE);
}
if (pid != 0) { // If this is a parent process.
2021-03-10 22:56:48 +01:00
printf("===\n%s\nI am a parent.\nMy PID is: %d\n===", get_timestamp(), getpid());
sleep(1);
2021-03-10 19:46:05 +01:00
kill(getpid(), SIGTERM);
2021-03-10 17:42:09 +01:00
} else {
2021-03-10 22:56:48 +01:00
printf("===\n%s\nI am a child.\nMy PID is: %d\nMy PPID is: %d\n===", get_timestamp(), getpid(), getppid());
sleep(1);
printf("===\n%s\nI am a child.\nMy PID is: %d\nMy PPID is: %d\n===", get_timestamp(), getpid(), getppid());
2021-03-10 17:42:09 +01:00
sleep(1);
2021-03-10 22:56:48 +01:00
printf("===\n%s\nI am a child.\nMy PID is: %d\nMy PPID is: %d\n===", get_timestamp(), getpid(), getppid());
// When parent is killed PPID of its child process becomes 1 (and so the init process becomes a new parent).
2021-03-10 17:42:09 +01:00
}
}
void child_before_parent() {
pid_t pid;
2021-03-10 19:46:05 +01:00
printf("\nchild_before_parent()\n");
printf("PID before fork: %d\n", getpid());
2021-03-10 17:42:09 +01:00
pid = fork();
if (pid == -1) { // If something went wrong.
perror("Fork: ");
exit(EXIT_FAILURE);
}
if (pid == 0) { // If this is a parent process.
2021-03-10 22:56:48 +01:00
printf("===\n%s\nI am a child.\nMy PID is: %d\nMy PPID is: %d\n===", get_timestamp(), getpid(), getppid());
sleep(1);
2021-03-10 19:46:05 +01:00
kill(getpid(), SIGTERM);
2021-03-10 17:42:09 +01:00
} else {
2021-03-10 22:56:48 +01:00
printf("===\n%s\nI am a parent.\nMy PID is: %d\n===", get_timestamp(), getpid());
2021-03-10 17:42:09 +01:00
sleep(1);
2021-03-10 22:56:48 +01:00
printf("===\n%s\nI am a parent.\nMy PID is: %d\n===", get_timestamp(), getpid());
sleep(1);
printf("===\n%s\nI am a parent.\nMy PID is: %d\n===", get_timestamp(), getpid());
2021-03-10 17:42:09 +01:00
}
}
2021-03-10 19:46:05 +01:00
2021-03-10 17:42:09 +01:00
int main() {
2021-03-10 22:56:48 +01:00
int status;
2021-03-10 17:42:09 +01:00
pid_t pid;
2021-03-10 22:56:48 +01:00
printf("%s: Grandparent process has PID: %d\n", get_timestamp(), getpid());
2021-03-10 17:42:09 +01:00
pid = fork();
2021-03-10 19:46:05 +01:00
if (pid == 0) { // If child process
2021-03-10 22:56:48 +01:00
printf("%s: Process with PID %d will execute funs.\n", get_timestamp(), getpid());
2021-03-10 17:42:09 +01:00
child_before_parent();
2021-03-10 22:56:48 +01:00
sleep(5);
2021-03-10 17:42:09 +01:00
parent_before_child();
2021-03-10 19:46:05 +01:00
sleep(5);
2021-03-10 22:56:48 +01:00
} else { // If parent process
printf("%s: Grandparent's child process has PID: %d\n", get_timestamp(), pid);
sleep(12);
2021-03-10 17:42:09 +01:00
}
return 0;
}