2021-03-10 17:42:09 +01:00
//
// Wrote for Computer Networks and Systems lab classes
// 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:08:21 +01:00
const char * time_from_main_exec ( ) {
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 ( " \n parent_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:08:21 +01:00
printf ( " === \n %s \n I am a parent. \n My PID is: %d \n === " , time_from_main_exec ( ) , getpid ( ) ) ;
2021-03-10 19:46:05 +01:00
kill ( getpid ( ) , SIGTERM ) ;
2021-03-10 17:42:09 +01:00
} else {
sleep ( 1 ) ;
2021-03-10 22:08:21 +01:00
printf ( " === \n %s \n I am a child. \n My PID is: %d \n === " , time_from_main_exec ( ) , getpid ( ) ) ; // This line will not be printed, as (usually) parent process will finish
2021-03-10 19:46:05 +01:00
// its execution in less than 1 second
2021-03-10 17:42:09 +01:00
}
}
void child_before_parent ( ) {
pid_t pid ;
2021-03-10 19:46:05 +01:00
printf ( " \n child_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:08:21 +01:00
printf ( " === \n %s \n I am a child. \n My PID is: %d \n === " , time_from_main_exec ( ) , getpid ( ) ) ;
2021-03-10 19:46:05 +01:00
kill ( getpid ( ) , SIGTERM ) ;
2021-03-10 17:42:09 +01:00
} else {
sleep ( 1 ) ;
2021-03-10 22:08:21 +01:00
printf ( " === \n %s \n I am a parent. \n My PID is: %d \n === " , time_from_main_exec ( ) , 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:08:21 +01:00
2021-03-10 17:42:09 +01:00
pid_t pid ;
2021-03-10 22:08:21 +01:00
printf ( " %s: Grandparent process has PID: %d \n " , time_from_main_exec ( ) , 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:08:21 +01:00
printf ( " %s: Process with PID %d will execute funs. \n " , time_from_main_exec ( ) , getpid ( ) ) ;
2021-03-10 17:42:09 +01:00
child_before_parent ( ) ;
sleep ( 1 ) ;
parent_before_child ( ) ;
sleep ( 1 ) ;
2021-03-10 19:46:05 +01:00
} else { // If parent process
2021-03-10 22:08:21 +01:00
printf ( " %s: Grandparent's child process has PID: %d \n " , time_from_main_exec ( ) , pid ) ;
2021-03-10 19:46:05 +01:00
sleep ( 5 ) ;
2021-03-10 17:42:09 +01:00
}
return 0 ;
}