From e7e743923db8505091c775bb2ea8823ffd829ea3 Mon Sep 17 00:00:00 2001 From: EdwardEisenhauer Date: Wed, 24 Mar 2021 14:44:17 +0100 Subject: [PATCH] Added README.md files for each Topic and finished task_3_3 --- README.md | 24 +++-------------- Topic-1/README.md | 22 +++++++++++++++ Topic-2/README.md | 34 +++++++++++++++++++++++ Topic-2/task_2_2.c | 2 -- Topic-2/task_2_3.c | 1 - Topic-3/README.md | 39 +++++++++++++++++++++++++++ Topic-3/task_3_2.c | 40 +++++++++++++++++++++++++++ Topic-3/task_3_3 | Bin 0 -> 17144 bytes Topic-3/task_3_3.c | 66 +++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 205 insertions(+), 23 deletions(-) create mode 100644 Topic-1/README.md create mode 100644 Topic-2/README.md create mode 100644 Topic-3/README.md create mode 100644 Topic-3/task_3_2.c create mode 100755 Topic-3/task_3_3 create mode 100644 Topic-3/task_3_3.c diff --git a/README.md b/README.md index f09a299..203ec58 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,7 @@ # AAE-CNAS-Labs -Repository with tasks for Computer Networks and Systems laboratory classes +Tasks and solutions for WUST ETEA00008L. -## Topic 1 – Little warmup -I'm sure you are all proficient C/C++ programmers, but - just in case you had a long break and forgot some things - lets do a little warmup. - -Write your own version of Unix "cp" (copy) command. - -As a bare minimum - please write a program that: - - [x] accepts source and destination filenames as its arguments (you DO remember how to use argc and argv ?) - - [x] makes a copy of indicated file - - [x] returns 0 on success, any non-zero value on error - - [x] uses ONLY POSIX I/O functions (open, close, read, write - consult appropriate manuals) - -NOTICE: Do not assume, that you can read entire file into memory. Please read manuals carefully - pay attention to each function's return value! - -I'm sure this task will prove to be to easy to some of you - in this case you can extend your program - for example you can add features to allow: - - [ ] copy source_file dest_dir (copy source file to the indicated directory, keeping its name) - - copy file1 file2 file3 dest_dir (copy files file1...file3 into destination directory) - - ... ? - -As a solution - provide your source code here. \ No newline at end of file +Sergiusz Warga +Wrocław University of Science and Technology +ETEA00008L: Computer Networks and Systems laboratory classes diff --git a/Topic-1/README.md b/Topic-1/README.md new file mode 100644 index 0000000..f774108 --- /dev/null +++ b/Topic-1/README.md @@ -0,0 +1,22 @@ +# Topic 1 – Little warmup + +## Task + +I'm sure you are all proficient C/C++ programmers, but - just in case you had a long break and forgot some things - lets do a little warmup. + +Write your own version of Unix "cp" (copy) command. + +As a bare minimum - please write a program that: + - [x] accepts source and destination filenames as its arguments (you DO remember how to use argc and argv ?) + - [x] makes a copy of indicated file + - [x] returns 0 on success, any non-zero value on error + - [x] uses ONLY POSIX I/O functions (open, close, read, write - consult appropriate manuals) + +NOTICE: Do not assume, that you can read entire file into memory. Please read manuals carefully - pay attention to each function's return value! + +I'm sure this task will prove to be to easy to some of you - in this case you can extend your program - for example you can add features to allow: + - [x] copy source_file dest_dir (copy source file to the indicated directory, keeping its name) + - copy file1 file2 file3 dest_dir (copy files file1...file3 into destination directory) + - ... ? + +As a solution - provide your source code here. \ No newline at end of file diff --git a/Topic-2/README.md b/Topic-2/README.md new file mode 100644 index 0000000..64c9dd6 --- /dev/null +++ b/Topic-2/README.md @@ -0,0 +1,34 @@ +# Topic 2 – Forking + +## Task 1 + +Write a simple program, where you create one child process. + +Parent proces should write a "I am a parent" message, while your child process should write "I am a child". + +man 2 fork + +## Task 2 + +Please extend last program - make both processes display their process ID (PID) and their parent's PID. + +man 2 getpid +man 2 getppid + +## Task 3 + +Modify last program, so that it creates not one but 5 child processes. + +## Task 4 + +Go back to the code from Task 2 (with one child process) + +Please check, what happens when + +1. parent finishes execution before its child + +2. child finishes execution before its parent + +Use sleep(..) function to force a delaay. + +You can check process state using `ps -f ` command diff --git a/Topic-2/task_2_2.c b/Topic-2/task_2_2.c index 0803099..1789997 100644 --- a/Topic-2/task_2_2.c +++ b/Topic-2/task_2_2.c @@ -18,8 +18,6 @@ void print_pids() { int main() { pid_t pid; - - pid = fork(); if (pid == -1) { // If something went wrong. diff --git a/Topic-2/task_2_3.c b/Topic-2/task_2_3.c index ab0f5f5..93bcf60 100644 --- a/Topic-2/task_2_3.c +++ b/Topic-2/task_2_3.c @@ -13,7 +13,6 @@ void child_task() { void print_pids() { printf("My PID is %d\nMy parent's PID is %d\n", getpid(), getppid()); - } void print_pids_child() { diff --git a/Topic-3/README.md b/Topic-3/README.md new file mode 100644 index 0000000..6519019 --- /dev/null +++ b/Topic-3/README.md @@ -0,0 +1,39 @@ +# Topic 3 – More forking + +## Task 1 + +The main program creates one child process. The offspring is supposed to write numbers from 1 to 10 (every second) to the screen. +The parent process should wait for the child process to finish and then output the message "END OF WORK" + +man 3 sleep +man 2 wait + +## Task 2 + +The main program generates two random integers: +a - from the range 0...10 +b - from the range 20...30 + +Then it creates a child process. + +The child process calculates the sum of a+b values and returns it as its exit status. + +The parent process waits for the child process to finish calculating, receives its execution status, and displays returned sum. + +man 2 exit +man 2 wait + +Please read the description of WEXITSTATUS macro carefully. + +## Task 3 + +The main program allocates a 1000000-element integer array on the heap (free-store) +Then it fills it with random values from the range 0....100 + +After preparing the array it creates 10 child processes. The task of each of the child processes is to determine the average value of the array fragment (rounded to the nearest integer): +descendant 0 - from index 0 to 99999 +descendant 1 - from index 100000 to 199999 +descendant 2 - from index 200000 to 299999 +... + +The parent process waits for the descendant processes to complete their calculations and then determines the average value of the array elements based on the obtained partial averages. \ No newline at end of file diff --git a/Topic-3/task_3_2.c b/Topic-3/task_3_2.c new file mode 100644 index 0000000..02c59a5 --- /dev/null +++ b/Topic-3/task_3_2.c @@ -0,0 +1,40 @@ +// +// Written for Computer Networks and Systems lab classes +// AUTHOR : Sergiusz Warga + +#include +#include +#include +#include +#include +#include + +int child_task(int a, int b) { + return a + b; +} + +int main() { + srandom(time(NULL)); + int a = random() % 11; + int b = random() % 11 + 20; + int sum = 0; + + 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. + sum = child_task(a, b); + exit(sum); + } + + wait(&sum); + while (!(WIFEXITED(sum))); + printf("%d\n", WEXITSTATUS(sum)); + return 0; +} \ No newline at end of file diff --git a/Topic-3/task_3_3 b/Topic-3/task_3_3 new file mode 100755 index 0000000000000000000000000000000000000000..72d9b3cb2e016df39ec3ff9cd043fb5b9b5d6058 GIT binary patch literal 17144 zcmeHOe{fXA9pB3(a1pp18YDuM6Lp%XIRZosILKYdg_llHgrrIZAD7%Ex#50!_f8Tk z7>q%$=UMuPBQv$rSf^7Rohf!k$J#*>s6c<&;8bhJv5gK)FKTUw+NL&e{eJiD_vO8p zyP@s$pSHJ|_rCl6e1GijclYh>=DmH;+uB@Wu`o(jb}K`!f1!%Fg`nXUkpbdn4Qv72 zm$9qZ9Nht_m}G=$BNO5v3X;MY}?&N)$kpIaE!wE5oed z&Gal6GDei-T6s=1hUDaTjUGk?NJRB^7@Jrv7qi%UBOMZPc~(%eLl*7&gwY?2%YS+}e5>eMGUInZd zmVv`&({1ex+zcZo!pQrgQt&80+R@nm!1C=6o^ia?Gw<7bezW-2(SGnr7+$z^iOm1o0Ec*lTKzA^q1mbO*H-@5- zK%1{4gg&~%(TMONtEzbw)lp}Kv4^7%@ARi~*jx)>7NGQ;RR)6#aP;0Qu;h|pIck3$ zl_AmmNAu1u5!_xezY*>fxXsPYw0Vm7cQ&XvJ`4KiWK4&Pshlfjb+~^1n9$)+aV<^h zaD474Ju2|I0ARZ*9n|6U?4#_E4yQQ`@gW_Kz9>Db!zmUhJFLSYpjvuGhhL(hFbU~! z9P^Y;>G1gk0v*-ib{$^K79&uMKrsTv2oxhwjKKeH1U{{}>I-S$W1EyN``rr0q`_gO zBsVGz{M2@wH#&FIR)8mS*KGq*v5cX94@xG--_PZ82RV-mqRH{o8jlOJ$?*}5$A!`4 z_=_5k3!=&Kr!^iIY?I^P(s*2`O^)x^cw8t=j^C^CxImg5_h~#Xj3&qLARcOaC9iE0 zyw(BfwjBf~{6Q+K=x6A!gdaPD<^~rjln)dZ)0O zY44eoclx6%)81+6kd&z$L{Lh5vyx>r%`D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ic>8(@kDW7fo&vv+HdyO0tkupRBnbR{5%M7?2ORH2; +#include +#include +#include +#include +#include + +#define N_OF_ELEMENTS 1e6 +#define N_OF_CHILDREN 10 +#define SPAN (int)(N_OF_ELEMENTS/N_OF_CHILDREN) + +int child_task(int * array, int start) { + int sum = 0; + for (int i = start; i < start + SPAN; ++i ) { + sum += array[i]; + } + return (int)(sum/SPAN); +} + +int avg(int * array) { + int sum = 0; + for (int i = 0; i < N_OF_CHILDREN; ++i) { + sum += array[i]; + } + return sum/N_OF_CHILDREN; +} + +int main() { + srandom(time(NULL)); + int status[N_OF_CHILDREN]; + int sums[N_OF_CHILDREN] = {0}; + int children_pids[N_OF_CHILDREN]; + int * array = malloc(N_OF_ELEMENTS * sizeof(int)); + + for (int i = 0; i < N_OF_ELEMENTS; ++i) { + array[i] = random() % 101; + } + + pid_t pid; + + for (int i = 0; i < N_OF_CHILDREN; ++i) { + pid = fork(); + if (pid == -1) { // If something went wrong. + perror("Fork: "); + exit(EXIT_FAILURE); + } + if (pid == 0){ // If this is a child process. + int sum = child_task(array, (int)(i*SPAN)); + exit(sum); + } + children_pids[i] = pid; + } + + for (int i = 0; i < N_OF_CHILDREN; ++i) { + waitpid(children_pids[i], &status[i], 0); + sums[i] = WEXITSTATUS(status[i]); + } + + printf("%d\n", avg(sums)); + + return 0; +} \ No newline at end of file