Added Topic-2

This commit is contained in:
Sergiusz Warga 2021-03-10 17:42:09 +01:00
parent 53de348216
commit a2a6040d54
8 changed files with 202 additions and 1 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
cmake-build-debug cmake-build-debug
CMakeLists.txt CMakeLists.txt
.idea .idea
*.o

12
Topic-2/Makefile Normal file
View File

@ -0,0 +1,12 @@
# target : source files
task_2_1 : task_2_1.o child_functions.o
gcc -Wall -o task_2_1 task_2_1.o child_functions.o
task_2_1.o : task_2_1.c
gcc -Wall -o task_2_1.c
child_functions.o : child_functions.c
gcc -Wall -c child_functions.c
clean:
rm -f task_2_1 task_2_1.o child_functions.o

View File

@ -0,0 +1,7 @@
//
// Wrote for Computer Networks and Systems lab classes
// AUTHOR : Sergiusz Warga
void child_task() {
printf("I am a child\n");
}

12
Topic-2/child_functions.h Normal file
View File

@ -0,0 +1,12 @@
//
// Wrote for Computer Networks and Systems lab classes
// AUTHOR : Sergiusz Warga
#ifndef AAE_CNAS_LABS_CHILD_FUNCTIONS_H
#define AAE_CNAS_LABS_CHILD_FUNCTIONS_H
#include <stdio.h>
void child_task();
#endif //AAE_CNAS_LABS_CHILD_FUNCTIONS_H

29
Topic-2/task_2_1.c Normal file
View File

@ -0,0 +1,29 @@
//
// Wrote for Computer Networks and Systems lab classes
// AUTHOR : Sergiusz Warga
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include "child_functions.h"
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);
}
printf("I am a parent.\n");
sleep(1); // This may be required as the parent process won't wait for the child process to finish its execution.
return 0;
}

40
Topic-2/task_2_2.c Normal file
View File

@ -0,0 +1,40 @@
//
// Wrote 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() {
printf("I am a child\n");
}
void print_pids() {
printf("My PID is %d\nMy parent's PID is %d\n", getpid(), getppid());
}
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();
print_pids();
exit(EXIT_SUCCESS);
}
printf("I am a parent.\n");
print_pids();
sleep(1); // This may be required as the parent process won't wait for the child process to finish its execution.
return 0;
}

43
Topic-2/task_2_3.c Normal file
View File

@ -0,0 +1,43 @@
//
// Wrote 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() {
printf("I am a child\n");
}
void print_pids() {
printf("My PID is %d\nMy parent's PID is %d\n", getpid(), getppid());
}
void print_pids_child() {
printf("CHILD\nMy PID is %d\nMy parent's PID is %d\n", getpid(), getppid());
}
int main() {
pid_t pid;
for (int i = 0; i < 5; ++i) {
pid = fork();
if (pid == -1) { // If something went wrong.
perror("Fork: ");
exit(EXIT_FAILURE);
}
if (pid == 0){
print_pids_child();
exit(EXIT_SUCCESS);
}
}
printf("I am a parent.\n");
print_pids();
sleep(1); // This may be required as the parent process won't wait for the child process to finish its execution.
return 0;
}

57
Topic-2/task_2_4.c Normal file
View File

@ -0,0 +1,57 @@
//
// Wrote for Computer Networks and Systems lab classes
// AUTHOR : Sergiusz Warga
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
void parent_before_child() {
pid_t pid;
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("I am a parent.\n");
kill(getpid(), SIGKILL);
} else {
sleep(1);
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("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("I am a child.\n");
kill(getpid(), SIGKILL);
} else {
sleep(1);
printf("I am a parent.\n");
}
}
int main() {
pid_t pid;
pid = fork();
if (pid == 0) {
child_before_parent();
sleep(1);
parent_before_child();
sleep(1);
}
sleep(5);
return 0;
}