diff --git a/bac2/os/chap3/Makefile b/bac2/os/chap3/Makefile index 959a532..160b8a9 100644 --- a/bac2/os/chap3/Makefile +++ b/bac2/os/chap3/Makefile @@ -2,12 +2,12 @@ CC = gcc CFLAGS = -g -Wall -all: ex1 +all: ex1 ex2 ex3 ex4 ex5 %.o: %.c $(CC) $(CFLAGS) -c -o $@ $< -ex1: ex1.o +%: %.o $(CC) $(CFLAGS) -o $@ $+ clean: @@ -16,5 +16,9 @@ clean: mrproper: clean rm -f ex1 -run: ex1 +run: ex6 ./$< + +graph: run + graph -Tpng plot > plot.png + nsxiv plot.png diff --git a/bac2/os/chap3/ex2.c b/bac2/os/chap3/ex2.c new file mode 100644 index 0000000..79a55d7 --- /dev/null +++ b/bac2/os/chap3/ex2.c @@ -0,0 +1,14 @@ +#include +#include + +#define printvi(X) printf("%s = %d\n", #X, X); + +int main(void) +{ + printvi(getpid()); + + pid_t f = fork(); + if(f != 0) + getchar(); +} + diff --git a/bac2/os/chap3/ex3.c b/bac2/os/chap3/ex3.c new file mode 100644 index 0000000..e4aa541 --- /dev/null +++ b/bac2/os/chap3/ex3.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include + +#define printvi(X) printf("%s = %d\n", #X, X); + +int main(void) +{ + pid_t f = fork(); + if(f == 0){ + srand(time(NULL)); + int delay = 1 + rand() % 10; + int value = rand() % 256; + printvi(delay); + printvi(value); + sleep(delay); + return value; + } + + int child_ret = 0; + wait(&child_ret); + printvi(WEXITSTATUS(child_ret)); +} diff --git a/bac2/os/chap3/ex4.c b/bac2/os/chap3/ex4.c new file mode 100644 index 0000000..b6f1480 --- /dev/null +++ b/bac2/os/chap3/ex4.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include + +#define printvi(X) printf("%s = %d\n", #X, X); +#define printvf(X) printf("%s = %lld\n", #X, X); + +typedef struct timespec timespec_t; + +timespec_t start, end; + +long long int dtime(timespec_t *start, timespec_t *end){ + return (end->tv_sec - start->tv_sec) * 1e9 + end->tv_nsec - start->tv_nsec; +} + +int main(void) +{ + FILE* fd = fopen("plot", "w"); + long long int time, time_sum; + for (int i = 0; i < 10000; ++i) { + clock_gettime(CLOCK_REALTIME, &start); + usleep(1); + clock_gettime(CLOCK_REALTIME, &end); + time = dtime(&start, &end); + time_sum += time; + fprintf(fd, "%d %lld\n", i, time); + } +} diff --git a/bac2/os/chap3/ex5.c b/bac2/os/chap3/ex5.c new file mode 100644 index 0000000..4d423bf --- /dev/null +++ b/bac2/os/chap3/ex5.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include + +#define printvi(X) printf("%s = %d\n", #X, X); +#define printvf(X) printf("%s = %lld\n", #X, X); + +typedef struct timespec timespec_t; + +timespec_t start, end, s = {0, 1}; + +long long int dtime(timespec_t *start, timespec_t *end){ + return (end->tv_sec - start->tv_sec) * 1e9 + end->tv_nsec - start->tv_nsec; +} + +int main(void) +{ + FILE* fd = fopen("plot", "w"); + long long int time, time_sum; + for (int i = 0; i < 10000; ++i) { + clock_gettime(CLOCK_REALTIME, &start); + nanosleep(&s, NULL); + clock_gettime(CLOCK_REALTIME, &end); + time = dtime(&start, &end); + time_sum += time; + fprintf(fd, "%d %lld\n", i, time); + } +} diff --git a/bac2/os/chap3/ex6.c b/bac2/os/chap3/ex6.c new file mode 100644 index 0000000..82c789a --- /dev/null +++ b/bac2/os/chap3/ex6.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include + +#define THREADS 9 + +int cmp(const void * first, const void *second){ + return (*(int*)first) - (*(int*)second); +} + +void* threaded(){ + int delay = rand() % 6; + int value = rand() % 256; + int *ret = malloc(sizeof(int)); + *ret = value; + sleep(delay); + return ret; +} + +int main(void) +{ + srand(time(NULL)); + pthread_t threads[THREADS]; + int returned[THREADS]; + for (int i = 0; i < THREADS; ++i) { + pthread_create(threads+i, NULL, threaded, NULL); + } + + int *ret= NULL; + for (int i = 0; i < THREADS; ++i) { + pthread_join(threads[i], (void*)&ret); + returned[i] = *ret; + } + + qsort((void* ) returned, THREADS, sizeof(int), cmp); + + for (int i = 0; i < THREADS; ++i) { + printf("%d\n", returned[i]); + } + + return 0; +}