chap4
This commit is contained in:
34
bac2/os/chap4/ex4.c
Normal file
34
bac2/os/chap4/ex4.c
Normal 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);
|
||||
}
|
Reference in New Issue
Block a user