This commit is contained in:
Debucquoy 2023-11-08 09:42:46 +01:00
parent dfe4fd3729
commit 58acfde2b4
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
6 changed files with 148 additions and 3 deletions

View File

@ -2,12 +2,12 @@
CC = gcc CC = gcc
CFLAGS = -g -Wall CFLAGS = -g -Wall
all: ex1 all: ex1 ex2 ex3 ex4 ex5
%.o: %.c %.o: %.c
$(CC) $(CFLAGS) -c -o $@ $< $(CC) $(CFLAGS) -c -o $@ $<
ex1: ex1.o %: %.o
$(CC) $(CFLAGS) -o $@ $+ $(CC) $(CFLAGS) -o $@ $+
clean: clean:
@ -16,5 +16,9 @@ clean:
mrproper: clean mrproper: clean
rm -f ex1 rm -f ex1
run: ex1 run: ex6
./$< ./$<
graph: run
graph -Tpng plot > plot.png
nsxiv plot.png

14
bac2/os/chap3/ex2.c Normal file
View File

@ -0,0 +1,14 @@
#include <unistd.h>
#include <stdio.h>
#define printvi(X) printf("%s = %d\n", #X, X);
int main(void)
{
printvi(getpid());
pid_t f = fork();
if(f != 0)
getchar();
}

25
bac2/os/chap3/ex3.c Normal file
View File

@ -0,0 +1,25 @@
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <time.h>
#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));
}

29
bac2/os/chap3/ex4.c Normal file
View File

@ -0,0 +1,29 @@
#include <sys/time.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#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);
}
}

29
bac2/os/chap3/ex5.c Normal file
View File

@ -0,0 +1,29 @@
#include <sys/time.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#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);
}
}

44
bac2/os/chap3/ex6.c Normal file
View File

@ -0,0 +1,44 @@
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#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;
}