Browse Source

week4

master
RinRi 1 year ago
parent
commit
0a019444ec
14 changed files with 158 additions and 0 deletions
  1. BIN
      week04/ex1
  2. +34
    -0
      week04/ex1.c
  3. +3
    -0
      week04/ex1.sh
  4. BIN
      week04/ex2
  5. +47
    -0
      week04/ex2.c
  6. +3
    -0
      week04/ex2.sh
  7. BIN
      week04/ex3
  8. +16
    -0
      week04/ex3.c
  9. +26
    -0
      week04/ex3.sh
  10. +5
    -0
      week04/ex3.txt
  11. BIN
      week04/ex4
  12. +11
    -0
      week04/ex4.c
  13. +3
    -0
      week04/ex4.sh
  14. +10
    -0
      week04/temp.txt

BIN
week04/ex1 View File


+ 34
- 0
week04/ex1.c View File

@@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>

int main() {
clock_t pt = clock(), t1, t2;
int status;

printf("Main. PID: %d\nParent PID: %d\n\n", getpid(), getppid());

pid_t child1 = fork(), child2;
t1 = clock();

if (child1 != 0) {
child2 = fork();
t2 = clock();
}

if (child1 == 0) {
printf("Child1. PID: %d\nParent PID: %d\n", getpid(), getppid());
t1 = clock() - t1;
printf("Child1. Time took: %fms\n\n", ((double)t1)/CLOCKS_PER_SEC*1000);
} else if (child2 == 0) {
printf("Child2. PID: %d\nParent PID: %d\n", getpid(), getppid());
t2 = clock() - t2;
printf("Child2. Time took: %fms\n\n", ((double)t2)/CLOCKS_PER_SEC*1000);
} else {
pt = clock() - pt;
printf("Main. Time took: %fms\n\n", ((double)pt)/CLOCKS_PER_SEC*1000);
}
}

+ 3
- 0
week04/ex1.sh View File

@@ -0,0 +1,3 @@
#!/bin/sh
gcc ex1.c -o ex1
./ex1

BIN
week04/ex2 View File


+ 47
- 0
week04/ex2.c View File

@@ -0,0 +1,47 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>

int main() {
srand(time(NULL));
FILE *out = fopen("temp.txt", "w");
int u[120], v[120];
for (int i = 0; i < 120; ++i)
u[i] = rand() % 100, v[i] = rand() % 100;

int n;
scanf("%d", &n);
pid_t id[10];
int start = -1, sz = 120 / n;
for (int i = 0; i < n; ++i) {
id[i] = fork();
if (id[i] == 0) {
start = i * sz;
break;
}
}

if (start != -1) {
int res = 0;
for (int i = 0; i < sz; ++i) {
res += u[start + i] * v[start + i];
}
fprintf(out, "%d\n", res);
} else {
int status;
wait(&status);
fclose(out);
int res = 0;
FILE *in = fopen("temp.txt", "r");
for (int i = 0; i < n; ++i) {
int a;
fscanf(in, "%d", &a);
res += a;
}
fclose(in);
printf("Result: %d\n", res);
}
}

+ 3
- 0
week04/ex2.sh View File

@@ -0,0 +1,3 @@
#!/bin/sh
gcc ex2.c -o ex2
./ex2

BIN
week04/ex3 View File


+ 16
- 0
week04/ex3.c View File

@@ -0,0 +1,16 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv) {
if (argc != 2) {
printf("Usage: %s [n]\n", argv[0]);
exit(1);
}

int n = atoi(argv[1]);
for (int i = 0; i < n; ++i) {
fork();
sleep(5);
}
}

+ 26
- 0
week04/ex3.sh View File

@@ -0,0 +1,26 @@
#!/bin/sh

gcc ex3.c -o ex3
./ex3 3 &
sleep 1

i=0
while [ $i -lt 3 ]
do
pstree
sleep 5
i=$(( i + 1 ))
done

./ex3 5 &
sleep 1

i=0
while [ $i -lt 5 ]
do
pstree
sleep 5
i=$(( i + 1 ))
done



+ 5
- 0
week04/ex3.txt View File

@@ -0,0 +1,5 @@
During the experiment, I have got in total 8 processes for n=3, and 32 processes for n=5.

If we do fork() in a loop, in each iteration of a loop (but not at the same time), the number of processes increases by two (x * 2, where x is the current number of processes).

That is why, in the end, the total number of processes will be 2^n.

BIN
week04/ex4 View File


+ 11
- 0
week04/ex4.c View File

@@ -0,0 +1,11 @@
#include <stdio.h>
#include <stdlib.h>

int main() {
while(1) {
printf("$ ");
char command[256];
fgets(command, 256, stdin);
system(command);
}
}

+ 3
- 0
week04/ex4.sh View File

@@ -0,0 +1,3 @@
#!/bin/sh
gcc ex4.c -o ex4
./ex4

+ 10
- 0
week04/temp.txt View File

@@ -0,0 +1,10 @@
20985
27368
35342
11568
31843
21304
34481
33455
39728
28530

Loading…
Cancel
Save