Fixed a typo and finished task_2_4
This commit is contained in:
parent
79fc63135b
commit
34e453a786
@ -1,5 +1,5 @@
|
||||
//
|
||||
// Wrote for Computer Networks and Systems lab classes
|
||||
// Written for Computer Networks and Systems lab classes
|
||||
// AUTHOR : Sergiusz Warga
|
||||
|
||||
void child_task() {
|
||||
|
@ -1,5 +1,5 @@
|
||||
//
|
||||
// Wrote for Computer Networks and Systems lab classes
|
||||
// Written for Computer Networks and Systems lab classes
|
||||
// AUTHOR : Sergiusz Warga
|
||||
|
||||
#ifndef AAE_CNAS_LABS_CHILD_FUNCTIONS_H
|
||||
|
@ -1,5 +1,5 @@
|
||||
//
|
||||
// Wrote for Computer Networks and Systems lab classes
|
||||
// Written for Computer Networks and Systems lab classes
|
||||
// AUTHOR : Sergiusz Warga
|
||||
|
||||
#include <stdio.h>
|
||||
|
@ -1,5 +1,5 @@
|
||||
//
|
||||
// Wrote for Computer Networks and Systems lab classes
|
||||
// Written for Computer Networks and Systems lab classes
|
||||
// AUTHOR : Sergiusz Warga
|
||||
|
||||
#include <stdio.h>
|
||||
|
@ -1,5 +1,5 @@
|
||||
//
|
||||
// Wrote for Computer Networks and Systems lab classes
|
||||
// Written for Computer Networks and Systems lab classes
|
||||
// AUTHOR : Sergiusz Warga
|
||||
|
||||
#include <stdio.h>
|
||||
|
@ -1,5 +1,5 @@
|
||||
//
|
||||
// Wrote for Computer Networks and Systems lab classes
|
||||
// Written for Computer Networks and Systems lab classes
|
||||
// AUTHOR : Sergiusz Warga
|
||||
|
||||
#include <stdio.h>
|
||||
@ -10,7 +10,7 @@
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
|
||||
const char * time_from_main_exec() {
|
||||
const char * get_timestamp() {
|
||||
struct timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
char buff[127];
|
||||
@ -27,12 +27,16 @@ void parent_before_child() {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (pid != 0) { // If this is a parent process.
|
||||
printf("===\n%s\nI am a parent.\nMy PID is: %d\n===", time_from_main_exec(), getpid());
|
||||
printf("===\n%s\nI am a parent.\nMy PID is: %d\n===", get_timestamp(), getpid());
|
||||
sleep(1);
|
||||
kill(getpid(), SIGTERM);
|
||||
} else {
|
||||
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\n===", time_from_main_exec(), getpid()); // This line will not be printed, as (usually) parent process will finish
|
||||
// its execution in less than 1 second
|
||||
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());
|
||||
// When parent is killed PPID of its child process becomes 1 (and so the init process becomes a new parent).
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,29 +50,33 @@ void child_before_parent() {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (pid == 0) { // If this is a parent process.
|
||||
printf("===\n%s\nI am a child.\nMy PID is: %d\n===", time_from_main_exec(), getpid());
|
||||
printf("===\n%s\nI am a child.\nMy PID is: %d\nMy PPID is: %d\n===", get_timestamp(), getpid(), getppid());
|
||||
sleep(1);
|
||||
kill(getpid(), SIGTERM);
|
||||
} else {
|
||||
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===", time_from_main_exec(), getpid());
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
int status;
|
||||
pid_t pid;
|
||||
printf("%s: Grandparent process has PID: %d\n", time_from_main_exec(), getpid());
|
||||
printf("%s: Grandparent process has PID: %d\n", get_timestamp(), getpid());
|
||||
pid = fork();
|
||||
if (pid == 0) { // If child process
|
||||
printf("%s: Process with PID %d will execute funs.\n", time_from_main_exec(), getpid());
|
||||
printf("%s: Process with PID %d will execute funs.\n", get_timestamp(), getpid());
|
||||
child_before_parent();
|
||||
sleep(1);
|
||||
parent_before_child();
|
||||
sleep(1);
|
||||
} else { // If parent process
|
||||
printf("%s: Grandparent's child process has PID: %d\n", time_from_main_exec(), pid);
|
||||
sleep(5);
|
||||
parent_before_child();
|
||||
sleep(5);
|
||||
} else { // If parent process
|
||||
printf("%s: Grandparent's child process has PID: %d\n", get_timestamp(), pid);
|
||||
sleep(12);
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user