chap4
This commit is contained in:
46
bac2/os/chap4/ex2.c
Normal file
46
bac2/os/chap4/ex2.c
Normal file
@ -0,0 +1,46 @@
|
||||
#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, 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());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user