cours_progra/bac2/os/chap4/ex1.c
2023-12-21 23:39:21 +01:00

47 lines
718 B
C

#include "RingBuffer.h"
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#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());
}
}