Compare commits

..

No commits in common. "bc7045ddccf3762d749b97f8d8d41c1a7d76826e" and "a2a6040d5457274c70bf7b12fb3a5a1d3a932ced" have entirely different histories.

8 changed files with 19 additions and 83 deletions

3
.gitignore vendored
View File

@ -3,6 +3,3 @@ cmake-build-debug
CMakeLists.txt
.idea
*.o
# Apple
.DS_Store

View File

@ -1,5 +1,5 @@
//
// Written for Computer Networks and Systems lab classes
// Wrote for Computer Networks and Systems lab classes
// AUTHOR : Sergiusz Warga
void child_task() {

View File

@ -1,5 +1,5 @@
//
// Written for Computer Networks and Systems lab classes
// Wrote for Computer Networks and Systems lab classes
// AUTHOR : Sergiusz Warga
#ifndef AAE_CNAS_LABS_CHILD_FUNCTIONS_H

View File

@ -1,5 +1,5 @@
//
// Written for Computer Networks and Systems lab classes
// Wrote for Computer Networks and Systems lab classes
// AUTHOR : Sergiusz Warga
#include <stdio.h>

View File

@ -1,5 +1,5 @@
//
// Written for Computer Networks and Systems lab classes
// Wrote for Computer Networks and Systems lab classes
// AUTHOR : Sergiusz Warga
#include <stdio.h>

View File

@ -1,5 +1,5 @@
//
// Written for Computer Networks and Systems lab classes
// Wrote for Computer Networks and Systems lab classes
// AUTHOR : Sergiusz Warga
#include <stdio.h>

View File

@ -1,82 +1,57 @@
//
// Written for Computer Networks and Systems lab classes
// Wrote for Computer Networks and Systems lab classes
// AUTHOR : Sergiusz Warga
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
const char * get_timestamp() {
struct timeval now;
gettimeofday(&now, NULL);
char buff[127];
sprintf(&buff, "us : %d", now.tv_usec);
return buff;
}
void parent_before_child() {
pid_t pid;
printf("\nparent_before_child()\n");
printf("parent_before_child()\n");
pid = fork();
if (pid == -1) { // If something went wrong.
perror("Fork: ");
exit(EXIT_FAILURE);
}
if (pid != 0) { // If this is a parent process.
printf("===\n%s\nI am a parent.\nMy PID is: %d\n===", get_timestamp(), getpid());
sleep(1);
kill(getpid(), SIGTERM);
printf("I am a parent.\n");
kill(getpid(), SIGKILL);
} 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\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).
printf("I am a child\n"); // This line will not be printed, as (usually) parent process will finish
// its execution in less than 1 second.
}
}
void child_before_parent() {
pid_t pid;
printf("\nchild_before_parent()\n");
printf("PID before fork: %d\n", getpid());
printf("child_before_parent()\n");
pid = fork();
if (pid == -1) { // If something went wrong.
perror("Fork: ");
exit(EXIT_FAILURE);
}
if (pid == 0) { // If this is a parent process.
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);
printf("I am a child.\n");
kill(getpid(), SIGKILL);
} 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===", get_timestamp(), getpid());
sleep(1);
printf("===\n%s\nI am a parent.\nMy PID is: %d\n===", get_timestamp(), getpid());
printf("I am a parent.\n");
}
}
int main() {
int status;
pid_t pid;
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", get_timestamp(), getpid());
if (pid == 0) {
child_before_parent();
sleep(5);
sleep(1);
parent_before_child();
sleep(5);
} else { // If parent process
printf("%s: Grandparent's child process has PID: %d\n", get_timestamp(), pid);
sleep(12);
sleep(1);
}
sleep(5);
return 0;
}

View File

@ -1,36 +0,0 @@
//
// Written for Computer Networks and Systems lab classes
// AUTHOR : Sergiusz Warga
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
void child_task() {
for (int i = 1; i <= 10; ++i) {
printf("%d\n", i);
fflush(stdout);
if (i < 10) sleep(1);
}
}
int main() {
pid_t pid;
pid = fork();
if (pid == -1) { // If something went wrong.
perror("Fork: ");
exit(EXIT_FAILURE);
}
if (pid == 0) { // If this is a child process.
child_task();
exit(EXIT_SUCCESS);
}
wait();
printf("END OF WORK\n");
return 0;
}