Compare commits

..

4 Commits

8 changed files with 85 additions and 21 deletions

3
.gitignore vendored
View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

36
Topic-3/task_3_1.c Normal file
View File

@ -0,0 +1,36 @@
//
// 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;
}