diff --git a/bac2/os/chap4/Makefile b/bac2/os/chap4/Makefile new file mode 100644 index 0000000..65cc495 --- /dev/null +++ b/bac2/os/chap4/Makefile @@ -0,0 +1,27 @@ +.PHONY: clean, mrproper, run +CC = gcc +CFLAGS = -g -Wall + +exos = ex4 group + +all: $(exos) + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< + +ex1: ex1.o RingBuffer.o + $(CC) $(CFLAGS) -o $@ $+ + +ex%: ex%.o + $(CC) $(CFLAGS) -o $@ $+ + +group: group.o + $(CC) $(CFLAGS) -o $@ $+ + +clean: + rm -f *.o core.* + rm -f out group + rm -f ${exos} + +run: group + ./$< diff --git a/bac2/os/chap4/RingBuffer.c b/bac2/os/chap4/RingBuffer.c new file mode 100644 index 0000000..7c24b18 --- /dev/null +++ b/bac2/os/chap4/RingBuffer.c @@ -0,0 +1,20 @@ +#include "RingBuffer.h" + +void rb_push(rb_t* rb, int el){ + if(rb->filled) + return; + rb->data[rb->w_pos] = el; + rb->w_pos = (rb->w_pos + 1) % rb->size; + if(rb->w_pos == rb->r_pos) + rb->filled = true; +} +int rb_pop(rb_t* rb){ + if(rb->w_pos == rb->r_pos && !rb->filled){ + rb->error = true; + return 0; + } + int ret = rb->data[rb->r_pos]; + rb->r_pos = (rb->r_pos + 1) % rb->size; + rb->filled = false; + return ret; +} diff --git a/bac2/os/chap4/RingBuffer.h b/bac2/os/chap4/RingBuffer.h new file mode 100644 index 0000000..dab22b4 --- /dev/null +++ b/bac2/os/chap4/RingBuffer.h @@ -0,0 +1,27 @@ +#pragma once +#include +#include +#include +#include + +#define rb_init(size, type)\ + {(size),\ + 0, 0, false, false,\ + type,\ + malloc(sizeof(int)*(size)),\ + PTHREAD_MUTEX_INITIALIZER}; + +#define rb_free(rb) free(rb.data) + +typedef struct rb{ + size_t size; + size_t w_pos, r_pos; + bool filled; + bool error; + enum {DROP, WAIT} type; + int* data; + pthread_mutex_t m; +} rb_t; + +void rb_push(rb_t*, int); +int rb_pop(rb_t*); diff --git a/bac2/os/chap4/ex1.c b/bac2/os/chap4/ex1.c new file mode 100644 index 0000000..07b7cd7 --- /dev/null +++ b/bac2/os/chap4/ex1.c @@ -0,0 +1,46 @@ +#include "RingBuffer.h" +#include +#include +#include +#include + +#define THREADS 10 + +void* process(void* args); + +int main(void) +{ + srand(time(NULL)); + rb_t rb = rb_init(50, DROP); + pthread_t threads[THREADS]; + + int even, odd = {0}; + + for (int i = 0; i < THREADS; ++i) { + pthread_create(threads+i, NULL, process, (void*) &rb); + } + int val; + while(true){ + usleep(250); + val = rb_pop(&rb); + printf("got -> %d\n", val); + + if(val % 2) + odd++; + else + even++; + + printf("%d nombres pairs et %d nombre impairs\n", even, odd); + } + + rb_free(rb); +} + +void* process(void* args){ + rb_t *rb = (rb_t*) args; + + while(true){ + usleep((rand() % 4500) + 500); + rb_push(rb, rand()); + } +} diff --git a/bac2/os/chap4/ex2.c b/bac2/os/chap4/ex2.c new file mode 100644 index 0000000..7cea8a3 --- /dev/null +++ b/bac2/os/chap4/ex2.c @@ -0,0 +1,46 @@ +#include "RingBuffer.h" +#include +#include +#include +#include + +#define THREADS 10 + +void* process(void* args); + +int main(void) +{ + srand(time(NULL)); + rb_t rb = rb_init(50, WAIT); + pthread_t threads[THREADS]; + + int even, odd = {0}; + + for (int i = 0; i < THREADS; ++i) { + pthread_create(threads+i, NULL, process, (void*) &rb); + } + int val; + while(true){ + usleep(250); + val = rb_pop(&rb); + printf("got -> %d\n", val); + + if(val % 2) + odd++; + else + even++; + + printf("%d nombres pairs et %d nombre impairs\n", even, odd); + } + + rb_free(rb); +} + +void* process(void* args){ + rb_t *rb = (rb_t*) args; + + while(true){ + usleep((rand() % 4500) + 500); + rb_push(rb, rand()); + } +} diff --git a/bac2/os/chap4/ex4.c b/bac2/os/chap4/ex4.c new file mode 100644 index 0000000..ff8063e --- /dev/null +++ b/bac2/os/chap4/ex4.c @@ -0,0 +1,34 @@ +#include +#include +#include + +#define SIZE 32768 + +pthread_t child_thread; +pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + +int table[SIZE]; + +int cmp(const void* a1, const void* a2){ + return *((int*)a2) - *((int*)a1); +} + +void* child(void* args){ + pthread_mutex_lock(&mutex); + for (int i = 0; i < SIZE; ++i) { + table[i] = rand(); + } + pthread_mutex_unlock(&mutex); + sleep(1); + pthread_mutex_lock(&mutex); + return NULL; +} + +int main(void) +{ + pthread_create(&child_thread, NULL, child, NULL); + + pthread_mutex_lock(&mutex); + qsort(table, SIZE, sizeof(int), cmp); + pthread_mutex_unlock(&mutex); +} diff --git a/bac2/os/chap4/group.c b/bac2/os/chap4/group.c new file mode 100644 index 0000000..bbb142a --- /dev/null +++ b/bac2/os/chap4/group.c @@ -0,0 +1,45 @@ +/*************************************************************************** +* Le dinner des philosophes * +* Two phase locking: * +* - array de baguettes * +* - manger(): tente l'acquisition des deux. attente passive si non dispo * +****************************************************************************/ +#include +#include +#include +#define TABLE_SIZE 5 + +#define RIGHT(x) ((x)) % TABLE_SIZE +#define LEFT(x) ((x)+1) % TABLE_SIZE + +typedef unsigned int PHILOSOPHE; // philosophe's id in the table + +pthread_mutex_t baguettes[TABLE_SIZE], tableLock; +int main(void) +{ + +} + +void penser(){ + usleep(100 + rand() % 500); +} + +void manger(){ + usleep(100 + rand() % 500); +} + +void prendre(){ + +} +void deposer(){ + +} + +void* thread_philosophe(void* arg){ + while(42){ + penser(); + prendre(); + manger(); + deposer(); + } +} diff --git a/bac2/os/chap4/philosophes.h b/bac2/os/chap4/philosophes.h new file mode 100644 index 0000000..023d2c1 --- /dev/null +++ b/bac2/os/chap4/philosophes.h @@ -0,0 +1,11 @@ +#ifndef PHILOSOPHES_H +#define PHILOSOPHES_H + + + + +#ifndef PHILOSPHES_IMPLEMENTATION + +#endif /* end of implementation */ + +#endif /* end of include guard: PHILOSOPHES_H */