diff --git a/.gitignore b/.gitignore index fc315ca..9324439 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ #CLion cmake-build-debug CMakeLists.txt -.idea \ No newline at end of file +.idea +*.o diff --git a/Topic-2/Makefile b/Topic-2/Makefile new file mode 100644 index 0000000..81ceed5 --- /dev/null +++ b/Topic-2/Makefile @@ -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 \ No newline at end of file diff --git a/Topic-2/child_functions.c b/Topic-2/child_functions.c new file mode 100644 index 0000000..0567e49 --- /dev/null +++ b/Topic-2/child_functions.c @@ -0,0 +1,7 @@ +// +// Wrote for Computer Networks and Systems lab classes +// AUTHOR : Sergiusz Warga + +void child_task() { + printf("I am a child\n"); +} \ No newline at end of file diff --git a/Topic-2/child_functions.h b/Topic-2/child_functions.h new file mode 100644 index 0000000..bcbaacd --- /dev/null +++ b/Topic-2/child_functions.h @@ -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 + +void child_task(); + +#endif //AAE_CNAS_LABS_CHILD_FUNCTIONS_H diff --git a/Topic-2/task_2_1.c b/Topic-2/task_2_1.c new file mode 100644 index 0000000..55e7df8 --- /dev/null +++ b/Topic-2/task_2_1.c @@ -0,0 +1,29 @@ +// +// Wrote for Computer Networks and Systems lab classes +// AUTHOR : Sergiusz Warga + +#include +#include +#include +#include +#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; +} \ No newline at end of file diff --git a/Topic-2/task_2_2.c b/Topic-2/task_2_2.c new file mode 100644 index 0000000..5f5d4ff --- /dev/null +++ b/Topic-2/task_2_2.c @@ -0,0 +1,40 @@ +// +// Wrote for Computer Networks and Systems lab classes +// AUTHOR : Sergiusz Warga + +#include +#include +#include +#include + +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; +} \ No newline at end of file diff --git a/Topic-2/task_2_3.c b/Topic-2/task_2_3.c new file mode 100644 index 0000000..ceea33c --- /dev/null +++ b/Topic-2/task_2_3.c @@ -0,0 +1,43 @@ +// +// Wrote for Computer Networks and Systems lab classes +// AUTHOR : Sergiusz Warga + +#include +#include +#include +#include + +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; +} \ No newline at end of file diff --git a/Topic-2/task_2_4.c b/Topic-2/task_2_4.c new file mode 100644 index 0000000..65d2806 --- /dev/null +++ b/Topic-2/task_2_4.c @@ -0,0 +1,57 @@ +// +// Wrote for Computer Networks and Systems lab classes +// AUTHOR : Sergiusz Warga + +#include +#include +#include +#include +#include + +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; +} \ No newline at end of file