cours_progra/bac2/os/chap4/ex4.c

35 lines
625 B
C
Raw Normal View History

2023-12-21 23:39:12 +01:00
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#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);
}