This commit is contained in:
Debucquoy
2023-12-21 23:39:12 +01:00
parent babed7eb09
commit df9e43e534
8 changed files with 256 additions and 0 deletions

34
bac2/os/chap4/ex4.c Normal file
View File

@ -0,0 +1,34 @@
#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);
}