Added README.md files for each Topic and finished task_3_3
This commit is contained in:
parent
bc7045ddcc
commit
e7e743923d
24
README.md
24
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.
|
||||
Sergiusz Warga
|
||||
Wrocław University of Science and Technology
|
||||
ETEA00008L: Computer Networks and Systems laboratory classes
|
||||
|
22
Topic-1/README.md
Normal file
22
Topic-1/README.md
Normal file
@ -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.
|
34
Topic-2/README.md
Normal file
34
Topic-2/README.md
Normal file
@ -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 <pid>` command
|
@ -18,8 +18,6 @@ void print_pids() {
|
||||
int main() {
|
||||
pid_t pid;
|
||||
|
||||
|
||||
|
||||
pid = fork();
|
||||
|
||||
if (pid == -1) { // If something went wrong.
|
||||
|
@ -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() {
|
||||
|
39
Topic-3/README.md
Normal file
39
Topic-3/README.md
Normal file
@ -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.
|
40
Topic-3/task_3_2.c
Normal file
40
Topic-3/task_3_2.c
Normal file
@ -0,0 +1,40 @@
|
||||
//
|
||||
// Written for Computer Networks and Systems lab classes
|
||||
// AUTHOR : Sergiusz Warga
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
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;
|
||||
}
|
BIN
Topic-3/task_3_3
Executable file
BIN
Topic-3/task_3_3
Executable file
Binary file not shown.
66
Topic-3/task_3_3.c
Normal file
66
Topic-3/task_3_3.c
Normal file
@ -0,0 +1,66 @@
|
||||
//
|
||||
// Written for Computer Networks and Systems lab classes
|
||||
// AUTHOR : Sergiusz Warga
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user