| | text,label |
| | "#include <stdio.h> |
| | #include <string.h> |
| | #include <stdlib.h> |
| | #include <pthread.h> |
| | #include <signal.h> |
| | #include <unistd.h> |
| |
|
| | |
| | |
| | struct DataForThread { |
| | char name[36]; |
| | int limit; |
| | int grouping; |
| | }; |
| |
|
| | |
| | |
| | void* workerThreadFunc(void* data); |
| |
|
| | |
| | |
| | pthread_mutex_t * mutex; |
| |
|
| | int main() { |
| | |
| | int numThreads = 5; |
| |
|
| | |
| | pthread_t threadIds[numThreads]; |
| |
|
| | |
| | mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); |
| | pthread_mutex_init(mutex,NULL); |
| |
|
| | |
| | for(int i=1;i<=numThreads;i++) { |
| | |
| | |
| | struct DataForThread * dft = (struct DataForThread *) malloc(sizeof(struct DataForThread)); |
| | sprintf(dft->name,""Thread%d"",i); |
| | dft->limit = i * 10; |
| | dft->grouping = i; |
| |
|
| | |
| | pthread_create(&threadIds[i - 1],NULL,workerThreadFunc,dft); |
| | } |
| |
|
| | |
| | for(int i=1;i<=numThreads;i++) { |
| | printf(""Waiting for thread %d\\n"",i); |
| | pthread_join(threadIds[i - 1],0); |
| | printf(""Thread %d exited\\n"",i); |
| | } |
| | free(mutex); |
| | printf(""Program complete...\\n""); |
| | } |
| |
|
| | |
| | void* workerThreadFunc(void* data) { |
| | struct DataForThread* info = (struct DataForThread*)data; |
| |
|
| | int count = 1; |
| | while(count <= info->limit) { |
| | |
| | pthread_mutex_lock(mutex); |
| | |
| | for(int i=0;i<info->grouping;i++) { |
| | printf(""%s message %d of %d\\n"",info->name,count++,info->limit); |
| | sleep(1); |
| | } |
| | |
| | pthread_mutex_unlock(mutex); |
| | } |
| |
|
| | free(info); |
| | return 0; |
| | }",1 |
| | "#include <pthread.h> |
| | #include <stdio.h> |
| | #include <stdlib.h> |
| |
|
| | #define NUM_THREADS 4 |
| | #define TOTAL_COUNT 10 |
| | #define COUNT_LIMIT 12 |
| |
|
| | int count = 0; |
| | pthread_mutex_t count_mutex; |
| | pthread_cond_t count_threshold_cv; |
| |
|
| | void *inc_count(void *t) |
| | { |
| | int i; |
| | long my_id = (long)t; |
| |
|
| | for (i = 0; i < TOTAL_COUNT; i++) { |
| | pthread_mutex_lock(&count_mutex); |
| | count++; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | if (count == COUNT_LIMIT) { |
| | printf(""inc_count(): thread %ld, count = %d Threshold reached. "", |
| | my_id, count); |
| | pthread_cond_signal(&count_threshold_cv); |
| | printf(""Just sent signal.\\n""); |
| | } |
| | printf(""inc_count(): thread %ld, count = %d, unlocking mutex\\n"", |
| | my_id, count); |
| | pthread_mutex_unlock(&count_mutex); |
| |
|
| | |
| | sleep(1); |
| | } |
| | pthread_exit(NULL); |
| | } |
| |
|
| | void *watch_count(void *t) |
| | { |
| | long my_id = (long)t; |
| |
|
| | printf(""watch_count(): thread %ld\\n"", my_id); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | pthread_mutex_lock(&count_mutex); |
| | while (count < COUNT_LIMIT) { |
| | printf(""watch_count(): thread %ld Count= %d. Going into wait...\\n"", my_id, count); |
| | pthread_cond_wait(&count_threshold_cv, &count_mutex); |
| | printf(""watch_count(): thread %ld Condition signal received. Count= %d\\n"", my_id, count); |
| | } |
| | printf(""watch_count(): thread %ld Updating the value of count...\\n"", my_id); |
| | count += 125; |
| | printf(""watch_count(): thread %ld count now = %d.\\n"", my_id, count); |
| | printf(""watch_count(): thread %ld Unlocking mutex.\\n"", my_id); |
| | pthread_mutex_unlock(&count_mutex); |
| | pthread_exit(NULL); |
| | } |
| |
|
| | int main(int argc, char *argv[]) |
| | { |
| | int i; |
| | long t1=1, t2=2, t3=3, t4=4; |
| | pthread_t threads[NUM_THREADS]; |
| | pthread_attr_t attr; |
| |
|
| | |
| | pthread_mutex_init(&count_mutex, NULL); |
| | pthread_cond_init(&count_threshold_cv, NULL); |
| |
|
| | |
| | pthread_attr_init(&attr); |
| | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
| | pthread_create(&threads[0], &attr, watch_count, (void *)t1); |
| | pthread_create(&threads[1], &attr, inc_count, (void *)t2); |
| | pthread_create(&threads[2], &attr, inc_count, (void *)t3); |
| | pthread_create(&threads[3], &attr, inc_count, (void *)t4); |
| |
|
| | |
| | for (i = 0; i < NUM_THREADS; i++) { |
| | pthread_join(threads[i], NULL); |
| | } |
| | printf (""main(): waited and joined with %d threads. Final value of count = %d. Done.\\n"", |
| | NUM_THREADS, count); |
| |
|
| | |
| | pthread_attr_destroy(&attr); |
| | pthread_mutex_destroy(&count_mutex); |
| | pthread_cond_destroy(&count_threshold_cv); |
| | pthread_exit(NULL); |
| | } |
| |
|
| | ",0 |
| | " |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #include <stdio.h> |
| | #include <time.h> |
| | #include <unistd.h> |
| |
|
| | #include <stdlib.h> |
| | #include <pthread.h> |
| |
|
| | void * p1_func( void *ptr ); |
| | void * p2_func( void *ptr ); |
| |
|
| | pthread_t t1, t2; |
| | pthread_mutex_t mutex; |
| | pthread_cond_t cond; |
| | struct timespec ts; |
| |
|
| | main() |
| | { |
| | char *msg1 = ""Hello ""; |
| | char *msg2 = ""World \\n""; |
| |
|
| | |
| | pthread_mutex_init(&mutex, NULL); |
| |
|
| | |
| | if ( (pthread_create( &t1, NULL, p1_func, (void *) msg1)) < 0) { |
| | perror(""pthread_create""); |
| | exit(EXIT_FAILURE); |
| | } |
| |
|
| | if ( (pthread_create( &t2, NULL, p2_func, (void *) msg2) ) < 0) { |
| | perror(""pthread_create""); |
| | exit(EXIT_FAILURE); |
| | } |
| |
|
| | |
| | if ( (pthread_join(t1, NULL)) < 0 ) { |
| | perror(""pthread_join""); |
| | exit(EXIT_FAILURE); |
| | } |
| | if ( (pthread_join(t2, NULL)) < 0 ) { |
| | perror(""pthread_join""); |
| | exit(EXIT_FAILURE); |
| | } |
| |
|
| |
|
| | exit(0); |
| | } |
| |
|
| |
|
| | void * p1_func( void *ptr ) |
| | { |
| | char *message; |
| | message = (char *) ptr; |
| | int rc; |
| | |
| |
|
| | clock_gettime(CLOCK_REALTIME, &ts); |
| | ts.tv_sec += 3; |
| | rc = 0; |
| |
|
| | |
| | while ( rc == 0) |
| | rc = pthread_cond_timedwait(&cond, &mutex, &ts); |
| |
|
| |
|
| | |
| | write(1,message,10); |
| | } |
| |
|
| | void * p2_func( void *ptr ) |
| | { |
| | char *message; |
| | message = (char *) ptr; |
| | fprintf(stderr,""%s "", message); |
| | } |
| | ",1 |
| | "#include <stdio.h> |
| | #include <stdlib.h> |
| | #include <pthread.h> |
| | #include <semaphore.h> |
| | #include <unistd.h> |
| |
|
| | #define THREADS_NUM 3 |
| |
|
| | |
| | typedef struct List { |
| | int value; |
| | struct List *next; |
| | } List; |
| |
|
| | struct List *lista = NULL; |
| |
|
| | pthread_mutex_t mutex; |
| | sem_t sem; |
| |
|
| | |
| | void display(struct List *element) { |
| | printf(""[""); |
| | while (element) { |
| | printf(""%d"", element->value); |
| | if (element->next) { |
| | printf("", ""); |
| | } |
| | element = element->next; |
| | } |
| | printf(""]\\n""); |
| | } |
| |
|
| | |
| | void push_f(int value) { |
| | struct List *newElement; |
| | struct List *cursor = lista; |
| | newElement = (struct List *)malloc(sizeof(struct List)); |
| | newElement->value = value; |
| | newElement->next = NULL; |
| |
|
| | if (lista == NULL) { |
| | lista = newElement; |
| | } else { |
| | |
| | while (cursor->next) { |
| | cursor = cursor->next; |
| | } |
| | cursor->next = newElement; |
| | } |
| | } |
| |
|
| | |
| | int pop_f() { |
| | if (lista == NULL) { |
| | return -1; |
| | } |
| |
|
| | struct List *cursor = lista; |
| | struct List *prev = NULL; |
| |
|
| | |
| | while (cursor->next) { |
| | prev = cursor; |
| | cursor = cursor->next; |
| | } |
| |
|
| | int return_value = cursor->value; |
| |
|
| | |
| | if (prev == NULL) { |
| | free(cursor); |
| | lista = NULL; |
| | } else { |
| | free(cursor); |
| | prev->next = NULL; |
| | } |
| |
|
| | return return_value; |
| | } |
| |
|
| | |
| | void *popThread(void *arg) { |
| | while (1) { |
| | sem_wait(&sem); |
| | pthread_mutex_lock(&mutex); |
| |
|
| | int val = pop_f(); |
| | if (val != -1) { |
| | printf(""Thread removed: %d\\n"", val); |
| | } else { |
| | printf(""Thread tried to remove from an empty list.\\n""); |
| | } |
| | display(lista); |
| |
|
| | pthread_mutex_unlock(&mutex); |
| | sleep(3); |
| | } |
| | return NULL; |
| | } |
| |
|
| | |
| | void *pushThread(void *arg) { |
| | while (1) { |
| | int val = rand() % 100; |
| |
|
| | pthread_mutex_lock(&mutex); |
| |
|
| | push_f(val); |
| | printf(""Thread inserted: %d\\n"", val); |
| | display(lista); |
| |
|
| | pthread_mutex_unlock(&mutex); |
| |
|
| | sem_post(&sem); |
| |
|
| | sleep(1); |
| | } |
| | return NULL; |
| | } |
| |
|
| | int main() { |
| | pthread_t tid[THREADS_NUM]; |
| | pthread_mutex_init(&mutex, NULL); |
| | sem_init(&sem, 0, 0); |
| |
|
| | srand(time(NULL)); |
| |
|
| | |
| | pthread_create(&tid[0], NULL, pushThread, NULL); |
| | pthread_create(&tid[1], NULL, popThread, NULL); |
| | pthread_create(&tid[2], NULL, popThread, NULL); |
| |
|
| | |
| | for (int i = 0; i < THREADS_NUM; i++) { |
| | pthread_join(tid[i], NULL); |
| | } |
| |
|
| | |
| | sem_destroy(&sem); |
| | pthread_mutex_destroy(&mutex); |
| |
|
| | |
| | while (lista) { |
| | pop_f(); |
| | } |
| |
|
| | return 0; |
| | } |
| |
|
| | ",0 |
| | "#include <stdio.h> |
| | #include <stdlib.h> |
| | #include <pthread.h> |
| | #include <sys/time.h> |
| | #include <sys/resource.h> |
| |
|
| | double get_time(){ |
| | struct timeval t; |
| | struct timezone tzp; |
| | gettimeofday(&t, &tzp); |
| | return t.tv_sec + t.tv_usec*1e-6; |
| | } |
| |
|
| |
|
| | void *increment_counter(void *ptr); |
| |
|
| | int counter = 0; |
| | pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; |
| |
|
| | int main(){ |
| | int x; |
| | double startTime = get_time(); |
| | for(x = 0; x < 1000; x++){ |
| | counter = 0; |
| | pthread_t thread1, thread2, thread3, thread4; |
| | int amount1 = 100; |
| | int amount2 = 100; |
| | int amount3 = 100; |
| | int amount4 = 100; |
| | int iret1, iret2, iret3, iret4; |
| | iret1 = pthread_create( &thread1, NULL, increment_counter,(void *) &amount1); |
| | iret2 = pthread_create( &thread2, NULL, increment_counter,(void *) &amount2); |
| | iret3 = pthread_create( &thread3, NULL, increment_counter,(void *) &amount3); |
| | iret4 = pthread_create( &thread4, NULL, increment_counter,(void *) &amount4); |
| | |
| | pthread_join(thread1, NULL); |
| | pthread_join(thread2, NULL); |
| | pthread_join(thread3, NULL); |
| | pthread_join(thread4, NULL); |
| | |
| | if(counter != 400){ |
| | |
| | } |
| | |
| | } |
| | double endTime = get_time(); |
| | double total_time = endTime - startTime; |
| | printf(""%f\\n"", total_time); |
| | } |
| |
|
| | void *increment_counter(void *ptr){ |
| | int *amount; |
| | amount= (int *) ptr; |
| | int i; |
| | for(i = 0; i < *amount; i++){ |
| | |
| | pthread_mutex_lock( &mutex1); |
| | counter++; |
| | pthread_mutex_unlock( &mutex1); |
| | } |
| | } |
| | ",1 |
| | "#include <pthread.h> |
| | #include <stdio.h> |
| | #include <stdlib.h> |
| |
|
| | #define BSIZE 4 |
| | #define NUMITEMS 30 |
| |
|
| | typedef struct { |
| | char buf[BSIZE]; |
| | int occupied; |
| | int nextin, nextout; |
| | pthread_mutex_t mutex; |
| | pthread_cond_t more; |
| | pthread_cond_t less; |
| | } buffer_t; |
| |
|
| | buffer_t buffer; |
| |
|
| | void *producer(void *); |
| | void *consumer(void *); |
| |
|
| | #define NUM_THREADS 2 |
| | pthread_t tid[NUM_THREADS]; |
| |
|
| | int main(int argc, char *argv[]) |
| | { |
| | int i; |
| |
|
| | |
| | buffer.occupied = 0; |
| | buffer.nextin = 0; |
| | buffer.nextout = 0; |
| |
|
| | pthread_mutex_init(&(buffer.mutex), NULL); |
| | pthread_cond_init(&(buffer.more), NULL); |
| | pthread_cond_init(&(buffer.less), NULL); |
| |
|
| | |
| | pthread_create(&tid[0], NULL, producer, NULL); |
| | pthread_create(&tid[1], NULL, consumer, NULL); |
| |
|
| | |
| | for (i = 0; i < NUM_THREADS; i++) { |
| | pthread_join(tid[i], NULL); |
| | } |
| |
|
| | printf(""\\nmain() reporting that all %d threads have terminated\\n"", NUM_THREADS); |
| |
|
| | |
| | pthread_mutex_destroy(&(buffer.mutex)); |
| | pthread_cond_destroy(&(buffer.more)); |
| | pthread_cond_destroy(&(buffer.less)); |
| |
|
| | return 0; |
| | } |
| |
|
| | void *producer(void *parm) |
| | { |
| | char item[NUMITEMS] = ""IT'S A SMALL WORLD, AFTER ALL.""; |
| | int i; |
| |
|
| | printf(""producer started.\\n""); |
| | fflush(stdout); |
| |
|
| | for(i = 0; i < NUMITEMS; i++) { |
| | if (item[i] == '\\0') break; |
| |
|
| | pthread_mutex_lock(&(buffer.mutex)); |
| |
|
| | |
| | while (buffer.occupied >= BSIZE) { |
| | printf(""producer waiting.\\n""); |
| | fflush(stdout); |
| | pthread_cond_wait(&(buffer.less), &(buffer.mutex)); |
| | } |
| |
|
| | |
| | buffer.buf[buffer.nextin++] = item[i]; |
| | buffer.nextin %= BSIZE; |
| | buffer.occupied++; |
| |
|
| | printf(""producer produced: %c\\n"", item[i]); |
| | fflush(stdout); |
| |
|
| | |
| | pthread_cond_signal(&(buffer.more)); |
| | pthread_mutex_unlock(&(buffer.mutex)); |
| |
|
| | |
| | usleep(100000); |
| | } |
| |
|
| | printf(""producer exiting.\\n""); |
| | fflush(stdout); |
| | pthread_exit(0); |
| | } |
| |
|
| | void *consumer(void *parm) |
| | { |
| | char item; |
| | int i; |
| |
|
| | printf(""consumer started.\\n""); |
| | fflush(stdout); |
| |
|
| | for(i = 0; i < NUMITEMS; i++) { |
| | pthread_mutex_lock(&(buffer.mutex)); |
| |
|
| | |
| | while(buffer.occupied <= 0) { |
| | printf(""consumer waiting.\\n""); |
| | fflush(stdout); |
| | pthread_cond_wait(&(buffer.more), &(buffer.mutex)); |
| | } |
| |
|
| | |
| | item = buffer.buf[buffer.nextout++]; |
| | buffer.nextout %= BSIZE; |
| | buffer.occupied--; |
| |
|
| | printf(""consumer consumed: %c\\n"", item); |
| | fflush(stdout); |
| |
|
| | |
| | pthread_cond_signal(&(buffer.less)); |
| | pthread_mutex_unlock(&(buffer.mutex)); |
| |
|
| | |
| | usleep(150000); |
| | } |
| |
|
| | printf(""consumer exiting.\\n""); |
| | fflush(stdout); |
| | pthread_exit(0); |
| | } |
| |
|
| | ",0 |
| | "#include <pthread.h> |
| | #include <stdio.h> |
| | #include <stdlib.h> |
| |
|
| | int arr[100]; |
| | int target; |
| | pthread_mutex_t mutex; |
| | int found = 0; |
| |
|
| | void* binary_search(void* arg) { |
| | int* range = (int*) arg; |
| | int low = range[0], high = range[1]; |
| |
|
| | if (low > high || found) return NULL; |
| |
|
| | int mid = (low + high) / 2; |
| |
|
| | |
| | if (arr[mid] == target) { |
| | found = 1; |
| | printf(""Element found at index %d\\n"", mid); |
| | } |
| | |
| |
|
| | if (!found) { |
| | pthread_t left_thread, right_thread; |
| | int left_range[] = {low, mid - 1}; |
| | int right_range[] = {mid + 1, high}; |
| |
|
| | pthread_create(&left_thread, NULL, binary_search, left_range); |
| | pthread_create(&right_thread, NULL, binary_search, right_range); |
| |
|
| | pthread_join(left_thread, NULL); |
| | pthread_join(right_thread, NULL); |
| | } |
| | return NULL; |
| | } |
| |
|
| | int main() { |
| | int n = 100; |
| | target = 50; |
| | for (int i = 0; i < n; i++) arr[i] = i; |
| |
|
| | pthread_mutex_init(&mutex, NULL); |
| |
|
| | pthread_t thread; |
| | int range[] = {0, n - 1}; |
| | pthread_create(&thread, NULL, binary_search, range); |
| | pthread_join(thread, NULL); |
| |
|
| | pthread_mutex_destroy(&mutex); |
| |
|
| | if (!found) printf(""Element not found\\n""); |
| | return 0; |
| | } |
| | ",1 |
| | "#include <pthread.h> |
| | #include <stdio.h> |
| | #include <stdlib.h> |
| |
|
| | #define NUM_THREADS 3 |
| | #define TCOUNT 10 |
| | #define COUNT_LIMIT 12 |
| |
|
| | int count = 0; |
| | pthread_mutex_t count_mutex; |
| | pthread_cond_t count_threshold_cv; |
| |
|
| | void *inc_count(void *t){ |
| | |
| | int i; |
| | long my_id = (long)t; |
| |
|
| | for(i=0; i < TCOUNT; i++){ |
| | pthread_mutex_lock(&count_mutex); |
| | count++; |
| |
|
| | |
| | |
| | |
| | |
| | if(count == COUNT_LIMIT){ |
| | |
| | printf(""inc_count(): thread %ld, count = %d Threshold reached. "",my_id, count); |
| | pthread_cond_signal(&count_threshold_cv); |
| | printf(""Just sent signal.\\n""); |
| | } |
| | |
| | printf(""inc_count(): thread %ld, count = %d, unlocking mutex\\n"",my_id, count); |
| | pthread_mutex_unlock(&count_mutex); |
| | |
| | sleep(1); |
| | } |
| | |
| | pthread_exit(NULL); |
| | } |
| |
|
| | void *watch_count(void *t){ |
| |
|
| | long my_id = (long)t; |
| |
|
| | printf(""Starting watch_count(): thread %ld\\n"", my_id); |
| |
|
| | |
| | |
| | |
| | |
| | pthread_mutex_lock(&count_mutex); |
| | |
| | while(count < COUNT_LIMIT){ |
| | printf(""watch_count(): thread %ld Count= %d. Going into wait...\\n"", my_id,count); |
| | pthread_cond_wait(&count_threshold_cv, &count_mutex); |
| | printf(""watch_count(): thread %ld Condition signal received. Count= %d\\n"", my_id,count); |
| | printf(""watch_count(): thread %ld Updating the value of count...\\n"", my_id,count); |
| | count += 125; |
| | printf(""watch_count(): thread %ld count now = %d.\\n"", my_id, count); |
| | } |
| | |
| | printf(""watch_count(): thread %ld Unlocking mutex.\\n"", my_id); |
| | pthread_mutex_unlock(&count_mutex); |
| | pthread_exit(NULL); |
| | } |
| |
|
| | void main(){ |
| |
|
| | int i, rc; |
| | long t1=1, t2=2, t3=3; |
| | pthread_t threads[3]; |
| | pthread_attr_t attr; |
| |
|
| | |
| | pthread_mutex_init(&count_mutex, NULL); |
| | pthread_cond_init (&count_threshold_cv, NULL); |
| |
|
| | |
| | |
| | pthread_attr_init(&attr); |
| | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
| | pthread_create(&threads[0], &attr, watch_count, (void *)t1); |
| | pthread_create(&threads[1], &attr, inc_count, (void *)t2); |
| | pthread_create(&threads[2], &attr, inc_count, (void *)t3); |
| |
|
| | |
| | for (i = 0; i < NUM_THREADS; i++) { |
| | pthread_join(threads[i], NULL); |
| | } |
| | |
| | printf (""Main(): Waited and joined with %d threads. Final value of count = %d. Done.\\n"", NUM_THREADS, count); |
| |
|
| | |
| | pthread_attr_destroy(&attr); |
| | pthread_mutex_destroy(&count_mutex); |
| | pthread_cond_destroy(&count_threshold_cv); |
| | |
| | pthread_exit (NULL); |
| | } |
| |
|
| | ",0 |
| | "#include <pthread.h> |
| | #include <stdio.h> |
| | #include <stdlib.h> |
| |
|
| | int arr[100]; |
| | int target; |
| | pthread_mutex_t mutex; |
| | int found = 0; |
| |
|
| | void* binary_search(void* arg) { |
| | int* range = (int*) arg; |
| | int low = range[0], high = range[1]; |
| |
|
| | if (low > high || found) return NULL; |
| |
|
| | int mid = (low + high) / 2; |
| |
|
| | |
| | if (arr[mid] == target) { |
| | found = 1; |
| | printf(""Element found at index %d\\n"", mid); |
| | } |
| | |
| |
|
| | if (!found) { |
| | pthread_t left_thread, right_thread; |
| | int left_range[] = {low, mid - 1}; |
| | int right_range[] = {mid + 1, high}; |
| |
|
| | pthread_create(&left_thread, NULL, binary_search, left_range); |
| | pthread_create(&right_thread, NULL, binary_search, right_range); |
| |
|
| | pthread_join(left_thread, NULL); |
| | pthread_join(right_thread, NULL); |
| | } |
| | return NULL; |
| | } |
| |
|
| | int main() { |
| | int n = 100; |
| | target = 50; |
| | for (int i = 0; i < n; i++) arr[i] = i; |
| |
|
| | pthread_mutex_init(&mutex, NULL); |
| |
|
| | pthread_t thread; |
| | int range[] = {0, n - 1}; |
| | pthread_create(&thread, NULL, binary_search, range); |
| | pthread_join(thread, NULL); |
| |
|
| | pthread_mutex_destroy(&mutex); |
| |
|
| | if (!found) printf(""Element not found\\n""); |
| | return 0; |
| | } |
| | ",1 |
| | "#include <pthread.h> |
| | #include <stdio.h> |
| | #include <stdlib.h> |
| | #include <string.h> |
| | #include <sys/socket.h> |
| | #include <netinet/in.h> |
| | #include <unistd.h> |
| |
|
| | #define FILE_SIZE 671088640 |
| | #define BUFFER_SIZE 8 |
| |
|
| | int thread_count = 0; |
| | int server_file_des, bytes_read; |
| | unsigned long long int block_size = 0, file_pos = 0, total_bytes = 0; |
| | pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; |
| |
|
| | void *receive_data(void *thread_id) { |
| | unsigned long long int start_pos, end_pos; |
| | unsigned long long int local_bytes = 0; |
| | char buffer[BUFFER_SIZE]; |
| |
|
| | pthread_mutex_lock(&mutex1); |
| | start_pos = file_pos; |
| | file_pos += block_size; |
| | pthread_mutex_unlock(&mutex1); |
| |
|
| | end_pos = start_pos + block_size; |
| |
|
| | struct sockaddr_in client_addr; |
| | socklen_t addr_len = sizeof(client_addr); |
| |
|
| | while (start_pos < end_pos) { |
| | bytes_read = recvfrom(server_file_des, buffer, BUFFER_SIZE, 0, (struct sockaddr*)&client_addr, &addr_len); |
| | |
| | if (bytes_read > 0) { |
| | local_bytes += bytes_read; |
| | start_pos += bytes_read; |
| | printf(""Thread %ld transferred %llu bytes (total transferred: %llu bytes)\\n"", (long)thread_id, local_bytes, start_pos); |
| | } else { |
| | perror(""recvfrom""); |
| | break; |
| | } |
| | } |
| |
|
| | pthread_mutex_lock(&mutex1); |
| | total_bytes += local_bytes; |
| | pthread_mutex_unlock(&mutex1); |
| |
|
| | pthread_exit(NULL); |
| | } |
| |
|
| | int main() { |
| | int ch; |
| | printf(""Perform Network Benchmarking on\\n1. 1 Thread\\n2. 2 Threads\\n3. 4 Threads\\n4. 8 Threads\\n""); |
| | scanf(""%d"", &ch); |
| |
|
| | if (ch == 1 || ch == 2 || ch == 3 || ch == 4) { |
| | thread_count = (ch == 4) ? 8 : ch == 3 ? 4 : ch; |
| | printf(""Number of Threads: %d\\n"", thread_count); |
| | } else { |
| | printf(""Invalid Choice\\nProgram terminated\\n""); |
| | return 0; |
| | } |
| |
|
| | struct sockaddr_in address; |
| | int addrlen = sizeof(address); |
| |
|
| | |
| | if ((server_file_des = socket(AF_INET, SOCK_DGRAM, 0)) == 0) { |
| | perror(""Socket failed""); |
| | exit(EXIT_FAILURE); |
| | } |
| |
|
| | |
| | address.sin_family = AF_INET; |
| | address.sin_addr.s_addr = INADDR_ANY; |
| | address.sin_port = htons(5000); |
| |
|
| | |
| | if (bind(server_file_des, (struct sockaddr *)&address, sizeof(address)) < 0) { |
| | perror(""Bind failed""); |
| | exit(EXIT_FAILURE); |
| | } |
| |
|
| | |
| | block_size = FILE_SIZE / thread_count; |
| |
|
| | pthread_t threads[thread_count]; |
| | for (long j = 0; j < thread_count; j++) { |
| | pthread_create(&threads[j], NULL, receive_data, (void*)j); |
| | } |
| |
|
| | for (int k = 0; k < thread_count; k++) { |
| | pthread_join(threads[k], NULL); |
| | } |
| |
|
| | printf(""Total bytes transferred: %llu bytes\\n"", total_bytes); |
| |
|
| | close(server_file_des); |
| | return 0; |
| | } |
| |
|
| | ",0 |
| | "#include <pthread.h> |
| | #include <stdio.h> |
| | #include <stdlib.h> |
| |
|
| | pthread_mutex_t mutex; |
| | int fib_cache[1000]; |
| |
|
| | void* fibonacci(void* arg) { |
| | int n = *(int*) arg; |
| | if (n <= 1) return (void*) (size_t) n; |
| |
|
| | pthread_t thread1, thread2; |
| | int n1 = n - 1, n2 = n - 2; |
| | void* res1; |
| | void* res2; |
| |
|
| | pthread_create(&thread1, NULL, fibonacci, &n1); |
| | pthread_create(&thread2, NULL, fibonacci, &n2); |
| |
|
| | pthread_join(thread1, &res1); |
| | pthread_join(thread2, &res2); |
| |
|
| | int result = (size_t) res1 + (size_t) res2; |
| |
|
| | |
| | fib_cache[n] = result; |
| | |
| |
|
| | return (void*) (size_t) result; |
| | } |
| |
|
| | int main() { |
| | int n = 10; |
| | pthread_mutex_init(&mutex, NULL); |
| |
|
| | pthread_t thread; |
| | pthread_create(&thread, NULL, fibonacci, &n); |
| |
|
| | void* result; |
| | pthread_join(thread, &result); |
| |
|
| | printf(""Fibonacci of %d is %zu\\n"", n, (size_t) result); |
| | pthread_mutex_destroy(&mutex); |
| |
|
| | return 0; |
| | } |
| | ",1 |
| | "#include <stdio.h> |
| | #include <unistd.h> |
| | #include <pthread.h> |
| | #include <string.h> |
| |
|
| | #define BUF_SIZE 16 |
| |
|
| | pthread_mutex_t my_mutex; |
| |
|
| | typedef struct Student { |
| | char name[BUF_SIZE]; |
| | char surname[BUF_SIZE]; |
| | int grade; |
| | } Student; |
| |
|
| | Student temp; |
| |
|
| | void* fun_write(void *arg) { |
| | while (1) { |
| | for (int i = 0; i < 2; i++) { |
| | pthread_mutex_lock(&my_mutex); |
| | if (i % 2) { |
| | strcpy(temp.name, ""Przemyslaw""); |
| | strcpy(temp.surname, ""Nowak""); |
| | temp.grade = 5; |
| | } else { |
| | strcpy(temp.name, ""Adam""); |
| | strcpy(temp.surname, ""Kazimierczak""); |
| | temp.grade = 2; |
| | } |
| | pthread_mutex_unlock(&my_mutex); |
| | sleep(1); |
| | } |
| | } |
| | return NULL; |
| | } |
| |
|
| | void* fun_read(void* arg) { |
| | Student local; |
| | while (1) { |
| | pthread_mutex_lock(&my_mutex); |
| | memcpy(&local, &temp, sizeof(Student)); |
| | pthread_mutex_unlock(&my_mutex); |
| | |
| | printf(""====================================\\n""); |
| | printf(""%s\\n"", local.name); |
| | printf(""%s\\n"", local.surname); |
| | printf(""%d\\n"", local.grade); |
| | sleep(1); |
| | } |
| | return NULL; |
| | } |
| |
|
| | int main() { |
| | pthread_t thread_write, thread_read; |
| | |
| | pthread_mutex_init(&my_mutex, NULL); |
| |
|
| | pthread_create(&thread_write, NULL, fun_write, NULL); |
| | pthread_create(&thread_read, NULL, fun_read, NULL); |
| | |
| | pthread_join(thread_write, NULL); |
| | pthread_join(thread_read, NULL); |
| |
|
| | pthread_mutex_destroy(&my_mutex); |
| | return 0; |
| | } |
| |
|
| | ",0 |
| | "#include <stdio.h> |
| | #include <pthread.h> |
| | #include <stdlib.h> |
| |
|
| | int matrice[10][4] = { {8, 25, 3, 41}, |
| | {11, 18, 3, 4}, |
| | {4, 15, 78, 12}, |
| | {1, 12, 0, 12}, |
| | {7, 9, 13, 15}, |
| | {4, 21, 37, 89}, |
| | {1, 54, 7, 3}, |
| | {15, 78, 7, 1}, |
| | {12, 15, 13, 47}, |
| | {91, 13, 72, 90} }; |
| |
|
| | int vecteur[4] = {1, 2, 3, 4}; |
| |
|
| | int resultat[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| | pthread_mutex_t mutex; |
| |
|
| | void* computeVector(void* arg) { |
| | int column = *((int*)arg); |
| | free(arg); |
| | |
| | for (int i = 0; i < 10; i++) { |
| | |
| | resultat[i] = resultat[i] + matrice[i][column] * vecteur[column]; |
| | |
| | } |
| | |
| | return NULL; |
| | } |
| |
|
| | int main() { |
| | if (pthread_mutex_init(&mutex, NULL) != 0) { |
| | perror(""ERROR INITIALIZING MUTEX""); |
| | return 1; |
| | } |
| |
|
| | pthread_t thread_ids[4]; |
| | |
| | for (int i = 0; i < 4; i++) { |
| | int* arg = malloc(sizeof(*arg)); |
| | if (arg == NULL) { |
| | perror(""ERROR ALLOCATING MEMORY""); |
| | return 1; |
| | } |
| | |
| | |
| | if (pthread_create(&thread_ids[i], NULL, computeVector, arg) != 0) { |
| | perror(""ERROR CREATING THREAD""); |
| | return 1; |
| | } |
| | } |
| |
|
| | for (int i = 0; i < 4; i++) { |
| | if (pthread_join(thread_ids[i], NULL) != 0) { |
| | perror(""ERROR JOINING THREAD""); |
| | return 1; |
| | } |
| | } |
| |
|
| | |
| | for (int i = 0; i < 10; i++) { |
| | printf(""\\n%d"", resultat[i]); |
| | } |
| |
|
| | pthread_mutex_destroy(&mutex); |
| | return 0; |
| | } |
| |
|
| | ",1 |
| | "#include <pthread.h> |
| | #include <stdio.h> |
| | #include <stdlib.h> |
| | #include <string.h> |
| | #include <sys/types.h> |
| | #include <unistd.h> |
| |
|
| | #define errExit(str) \\ |
| | do { perror(str); exit(-1); } while(0) |
| |
|
| | pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
| |
|
| | static size_t i_gloabl_n = 0; |
| |
|
| | void *thr_demo(void *args) |
| | { |
| | printf(""this is thr_demo thread, pid: %d, tid: %lu\\n"", getpid(), pthread_self()); |
| | pthread_mutex_lock(&mutex); |
| | |
| | int i; |
| | int max = *(int *)args; |
| | size_t tid = pthread_self(); |
| | setbuf(stdout, NULL); |
| | for (i = 0; i < max; ++i) { |
| | printf(""(tid: %lu):%lu\\n"", tid, ++i_gloabl_n); |
| | } |
| |
|
| | pthread_mutex_unlock(&mutex); |
| | return (void *)10; |
| | } |
| |
|
| | void *thr_demo2(void *args) |
| | { |
| | printf(""this is thr_demo2 thread, pid: %d, tid: %lu\\n"", getpid(), pthread_self()); |
| | pthread_mutex_lock(&mutex); |
| |
|
| | int i; |
| | int max = *(int *)args; |
| | size_t tid = pthread_self(); |
| | setbuf(stdout, NULL); |
| | for (i = 0; i < max; ++i) { |
| | printf(""(tid: %lu):%lu\\n"", tid, ++i_gloabl_n); |
| | } |
| |
|
| | pthread_mutex_unlock(&mutex); |
| | return (void *)10; |
| | } |
| |
|
| | int main() |
| | { |
| | pthread_t pt1, pt2; |
| |
|
| | int arg = 20; |
| | int ret; |
| |
|
| | ret = pthread_create(&pt1, NULL, thr_demo, &arg); |
| | if (ret != 0) { |
| | errExit(""pthread_create""); |
| | } |
| |
|
| | ret = pthread_create(&pt2, NULL, thr_demo2, &arg); |
| | if (ret != 0) { |
| | errExit(""pthread_create""); |
| | } |
| |
|
| | printf(""this is main thread, pid = %d, tid = %lu\\n"", getpid(), pthread_self()); |
| |
|
| | |
| | void *ret_val; |
| | ret = pthread_join(pt1, &ret_val); |
| | if (ret != 0) { |
| | errExit(""pthread_join""); |
| | } |
| | printf(""Thread 1 returned: %ld\\n"", (long)ret_val); |
| |
|
| | |
| | ret = pthread_join(pt2, &ret_val); |
| | if (ret != 0) { |
| | errExit(""pthread_join""); |
| | } |
| | printf(""Thread 2 returned: %ld\\n"", (long)ret_val); |
| |
|
| | |
| | pthread_mutex_destroy(&mutex); |
| |
|
| | return 0; |
| | } |
| |
|
| | ",0 |
| | "#include <stdio.h> |
| | #include <stdlib.h> |
| | #include <pthread.h> |
| |
|
| | |
| |
|
| |
|
| | void *functionC(); |
| | pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; |
| | int counter = 0; |
| |
|
| | int main() |
| | { |
| | int rc1, rc2; |
| | pthread_t thread1, thread2; |
| |
|
| | |
| |
|
| | if ((rc1 = pthread_create(&thread1, NULL, &functionC, NULL))) { |
| | printf(""Thread creation failed: %d\\n"", rc1); |
| | return -1; |
| | } |
| |
|
| | if ((rc2 = pthread_create(&thread2, NULL, &functionC, NULL))) { |
| | printf(""Thread creation failed: %d\\n"", rc2); |
| | return -1; |
| | } |
| |
|
| | |
| | pthread_join(thread1, NULL); |
| | pthread_join(thread2, NULL); |
| |
|
| | return 0; |
| | } |
| |
|
| | void *functionC() |
| | { |
| | |
| | counter++; |
| | printf(""Counter value: %d\\n"", counter); |
| | |
| | return NULL; |
| | } |
| |
|
| | ",1 |
| | "#include <stdio.h> |
| | #include <stdlib.h> |
| | #include <unistd.h> |
| | #include <pthread.h> |
| | #include <string.h> |
| |
|
| | #define CONSUMER_COUNT 2 |
| | #define PRODUCER_COUNT 2 |
| |
|
| | typedef struct node_ { |
| | struct node_ *next; |
| | int num; |
| | } node_t; |
| |
|
| | node_t *head = NULL; |
| | pthread_t thread[CONSUMER_COUNT + PRODUCER_COUNT]; |
| | pthread_cond_t cond; |
| | pthread_mutex_t mutex; |
| |
|
| | void* consume(void* arg) { |
| | int id = *(int*)arg; |
| | free(arg); |
| | while (1) { |
| | pthread_mutex_lock(&mutex); |
| |
|
| | while (head == NULL) { |
| | printf(""%d consume wait\\n"", id); |
| | pthread_cond_wait(&cond, &mutex); |
| | } |
| |
|
| | |
| | node_t *p = head; |
| | head = head->next; |
| | printf(""Consumer %d consumed %d\\n"", id, p->num); |
| |
|
| | pthread_mutex_unlock(&mutex); |
| |
|
| | |
| | free(p); |
| | } |
| | return NULL; |
| | } |
| |
|
| | void* produce(void* arg) { |
| | int id = *(int*)arg; |
| | free(arg); |
| | int i = 0; |
| |
|
| | while (1) { |
| | pthread_mutex_lock(&mutex); |
| |
|
| | node_t *p = (node_t*)malloc(sizeof(node_t)); |
| | memset(p, 0x00, sizeof(node_t)); |
| | p->num = i++; |
| | p->next = head; |
| | head = p; |
| |
|
| | printf(""Producer %d produced %d\\n"", id, p->num); |
| |
|
| | |
| | pthread_cond_signal(&cond); |
| | pthread_mutex_unlock(&mutex); |
| |
|
| | sleep(1); |
| | } |
| | return NULL; |
| | } |
| |
|
| | int main() { |
| | int i = 0; |
| | pthread_cond_init(&cond, NULL); |
| | pthread_mutex_init(&mutex, NULL); |
| |
|
| | |
| | for (; i < CONSUMER_COUNT; i++) { |
| | int *p = (int*)malloc(sizeof(int)); |
| | |
| | pthread_create(&thread[i], NULL, consume, (void*)p); |
| | } |
| |
|
| | |
| | for (i = 0; i < PRODUCER_COUNT; i++) { |
| | int *p = (int*)malloc(sizeof(int)); |
| | |
| | pthread_create(&thread[i + CONSUMER_COUNT], NULL, produce, (void*)p); |
| | } |
| |
|
| | |
| | for (i = 0; i < CONSUMER_COUNT + PRODUCER_COUNT; i++) { |
| | pthread_join(thread[i], NULL); |
| | } |
| |
|
| | pthread_mutex_destroy(&mutex); |
| | pthread_cond_destroy(&cond); |
| |
|
| | return 0; |
| | } |
| |
|
| | ",0 |
| | "#include <stdio.h> |
| | #include <stdlib.h> |
| | #include <string.h> |
| | #include <fcntl.h> |
| | #include <unistd.h> |
| | #include <pthread.h> |
| | #include <stdbool.h> |
| |
|
| | static pthread_once_t g_init = PTHREAD_ONCE_INIT; |
| | static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; |
| | static pthread_mutex_t g_lcd_lock = PTHREAD_MUTEX_INITIALIZER; |
| | static struct light_state_t g_notification; |
| | static struct light_state_t g_battery; |
| |
|
| | struct led_data { |
| | const char *max_brightness; |
| | const char *brightness; |
| | const char *delay_on; |
| | const char *delay_off; |
| | int blink; |
| | }; |
| |
|
| | struct led_data redled = { |
| | .max_brightness = ""/sys/class/leds/red/max_brightness"", |
| | .brightness = ""/sys/class/leds/red/brightness"", |
| | .delay_on = ""/sys/class/leds/red/delay_on"", |
| | .delay_off = ""/sys/class/leds/red/delay_off"", |
| | .blink = 0 |
| | }; |
| |
|
| | struct led_data greenled = { |
| | .max_brightness = ""/sys/class/leds/green/max_brightness"", |
| | .brightness = ""/sys/class/leds/green/brightness"", |
| | .delay_on = ""/sys/class/leds/green/delay_on"", |
| | .delay_off = ""/sys/class/leds/green/delay_off"", |
| | .blink = 0 |
| | }; |
| |
|
| | struct led_data blueled = { |
| | .max_brightness = ""/sys/class/leds/blue/max_brightness"", |
| | .brightness = ""/sys/class/leds/blue/brightness"", |
| | .delay_on = ""/sys/class/leds/blue/delay_on"", |
| | .delay_off = ""/sys/class/leds/blue/delay_off"", |
| | .blink = 0 |
| | }; |
| |
|
| | struct led_data lcd = { |
| | .max_brightness = ""/sys/class/leds/lcd_backlight0/max_brightness"", |
| | .brightness = ""/sys/class/leds/lcd_backlight0/brightness"", |
| | .delay_on = ""/sys/class/leds/lcd_backlight0/delay_on"", |
| | .delay_off = ""/sys/class/leds/lcd_backlight0/delay_off"", |
| | .blink = 0 |
| | }; |
| |
|
| | void init_globals(void) { |
| | pthread_mutex_init(&g_lock, NULL); |
| | pthread_mutex_init(&g_lcd_lock, NULL); |
| | } |
| |
|
| | static int write_int(const char *path, int val) { |
| | int fd = open(path, O_WRONLY); |
| | if (fd < 0) { |
| | fprintf(stderr, ""Failed to open %s\\n"", path); |
| | return -1; |
| | } |
| |
|
| | char buffer[20]; |
| | int bytes = snprintf(buffer, sizeof(buffer), ""%d\\n"", val); |
| | ssize_t amt = write(fd, buffer, (size_t)bytes); |
| | close(fd); |
| | return amt == -1 ? -1 : 0; |
| | } |
| |
|
| | static int read_int(const char *path) { |
| | char buffer[12]; |
| | int fd = open(path, O_RDONLY); |
| | if (fd < 0) { |
| | fprintf(stderr, ""Failed to open %s\\n"", path); |
| | return -1; |
| | } |
| |
|
| | int rc = read(fd, buffer, sizeof(buffer) - 1); |
| | close(fd); |
| | if (rc <= 0) |
| | return -1; |
| |
|
| | buffer[rc] = 0; |
| | return strtol(buffer, NULL, 0); |
| | } |
| |
|
| | static int is_lit(struct light_state_t const *state) { |
| | return state->color & 0x00ffffff; |
| | } |
| |
|
| | static int rgb_to_brightness(struct light_state_t const *state) { |
| | int color = state->color & 0x00ffffff; |
| | return ((77 * ((color >> 16) & 0x00ff)) + |
| | (150 * ((color >> 8) & 0x00ff)) + |
| | (29 * (color & 0x00ff))) >> 8; |
| | } |
| |
|
| | static int set_light_backlight(struct light_device_t *dev, |
| | struct light_state_t const *state) { |
| | if (!dev) |
| | return -1; |
| |
|
| | int brightness = rgb_to_brightness(state) * 39; |
| | |
| | int err = write_int(lcd.brightness, brightness); |
| | |
| | return err; |
| | } |
| |
|
| | static int set_speaker_light_locked(struct light_device_t *dev, |
| | struct light_state_t const *state) { |
| | if (!dev) |
| | return -1; |
| |
|
| | int red = (state->color >> 16) & 0xFF; |
| | int green = (state->color >> 8) & 0xFF; |
| | int blue = state->color & 0xFF; |
| | int delay_on = state->flashOnMS; |
| | int delay_off = state->flashOffMS; |
| |
|
| | if (delay_on > 0 && delay_off > 0) { |
| | |
| | if (red) { |
| | write_int(redled.delay_on, delay_on); |
| | write_int(redled.delay_off, delay_off); |
| | } |
| | if (green) { |
| | write_int(greenled.delay_on, delay_on); |
| | write_int(greenled.delay_off, delay_off); |
| | } |
| | if (blue) { |
| | write_int(blueled.delay_on, delay_on); |
| | write_int(blueled.delay_off, delay_off); |
| | } |
| | } else { |
| | |
| | write_int(redled.brightness, red); |
| | write_int(greenled.brightness, green); |
| | write_int(blueled.brightness, blue); |
| | } |
| |
|
| | return 0; |
| | } |
| |
|
| | static int close_lights(struct light_device_t *dev) { |
| | if (dev) |
| | free(dev); |
| |
|
| | return 0; |
| | } |
| |
|
| | static int open_lights(const struct hw_module_t *module, char const *name, |
| | struct hw_device_t **device) { |
| | int (*set_light)(struct light_device_t *dev, |
| | struct light_state_t const *state); |
| |
|
| | if (0 == strcmp(LIGHT_ID_BACKLIGHT, name)) |
| | set_light = set_light_backlight; |
| | else |
| | return -EINVAL; |
| |
|
| | pthread_once(&g_init, init_globals); |
| |
|
| | struct light_device_t *dev = malloc(sizeof(struct light_device_t)); |
| | if (!dev) |
| | return -ENOMEM; |
| |
|
| | memset(dev, 0, sizeof(*dev)); |
| | dev->common.tag = HARDWARE_DEVICE_TAG; |
| | dev->common.version = 0; |
| | dev->common.module = (struct hw_module_t *)module; |
| | dev->common.close = (int (*)(struct hw_device_t *))close_lights; |
| | dev->set_light = set_light; |
| |
|
| | |
| | return 0; |
| | } |
| |
|
| | static struct hw_module_methods_t lights_module_methods = { |
| | .open = open_lights, |
| | }; |
| |
|
| | struct hw_module_t HAL_MODULE_INFO_SYM = { |
| | .tag = HARDWARE_MODULE_TAG, |
| | .version_major = 1, |
| | .version_minor = 0, |
| | .id = LIGHTS_HARDWARE_MODULE_ID, |
| | .name = ""Honor 8 LED driver"", |
| | .author = ""Honor 8 Dev Team."", |
| | .methods = &lights_module_methods, |
| | }; |
| |
|
| | ",1 |
| | "#include <stdio.h> |
| | #include <pthread.h> |
| | #include <semaphore.h> |
| | #include <string.h> |
| |
|
| | #define DATA_MAX 5 |
| | #define PRO_INIT_VAL (DATA_MAX / 2) |
| | #define CON_INIT_VAL (DATA_MAX - PRO_INIT_VAL) |
| | static int data; |
| | static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
| | static pthread_mutex_t mutex; |
| | static sem_t sem_pro; |
| | static sem_t sem_con; |
| |
|
| | static void *pro_handler(void *arg) |
| | { |
| | ssize_t i = 100; |
| |
|
| | for (; i > 0; --i) { |
| | |
| | pthread_mutex_lock(&mutex); |
| |
|
| | ++data; |
| | printf(""producter: data = %d\\n"", data); |
| |
|
| | pthread_mutex_unlock(&mutex); |
| | |
| | pthread_cond_signal(&cond); |
| | usleep(100000); |
| | } |
| |
|
| | } |
| |
|
| | static void *con_handler(void *arg) |
| | { |
| | ssize_t i = 100; |
| |
|
| | |
| |
|
| | for (; i > 0; --i) { |
| | |
| | pthread_mutex_lock(&mutex); |
| | while (data == 0) { |
| | pthread_cond_wait(&cond, &mutex); |
| | } |
| | while (data > 0) { |
| | --data; |
| | printf(""consumer: data = %d\\n"", data); |
| | } |
| | pthread_mutex_unlock(&mutex); |
| | |
| | sleep(1); |
| | } |
| | } |
| |
|
| | int main() |
| | { |
| | pthread_mutex_init(&mutex, NULL); |
| |
|
| | sem_init(&sem_pro, 0, PRO_INIT_VAL); |
| | sem_init(&sem_con, 0, CON_INIT_VAL); |
| |
|
| | data = CON_INIT_VAL; |
| |
|
| | pthread_t pro_id, con_id; |
| | |
| | int pro_ret = 0; |
| | if (pro_ret = pthread_create(&pro_id, NULL, pro_handler, NULL)) { |
| | fprintf(stderr, ""pthread_create producter: %s"", strerror(pro_ret)); |
| | goto err_create_producter; |
| | } |
| |
|
| | int con_ret = 0; |
| | if (con_ret = pthread_create(&con_id, NULL, con_handler, NULL)) { |
| | fprintf(stderr, ""pthread_create consumer: %s"", strerror(con_ret)); |
| | goto err_create_consumer; |
| | } |
| | #if 0 |
| | sleep(3); |
| | if (pthread_cancel(con_id)) { |
| | fprintf(stderr, ""error cancel\\n""); |
| | } |
| | #endif |
| | pthread_join(pro_id, NULL); |
| | pthread_join(con_id, NULL); |
| |
|
| | sem_destroy(&sem_con); |
| | sem_destroy(&sem_pro); |
| | pthread_mutex_destroy(&mutex); |
| | return 0; |
| |
|
| | |
| | err_create_consumer: |
| | pthread_join(pro_id, NULL); |
| | err_create_producter: |
| | sem_destroy(&sem_con); |
| | sem_destroy(&sem_pro); |
| | pthread_mutex_destroy(&mutex); |
| | return -1; |
| | } |
| | ",0 |
| | "#include <stdio.h> |
| | #include <stdlib.h> |
| | #include <string.h> |
| | #include <unistd.h> |
| | #include <pthread.h> |
| | #include <sys/socket.h> |
| | #include <linux/netlink.h> |
| | #include <errno.h> |
| |
|
| | struct udp_splice_handle { |
| | pthread_mutex_t lock; |
| | int sock; |
| | uint16_t id; |
| | }; |
| |
|
| | static int udp_splice_get_family_id(int sock) { |
| | struct { |
| | struct nlmsghdr nl; |
| | char buf[4096]; |
| | } buf; |
| | struct genlmsghdr *genl; |
| | struct rtattr *rta; |
| | struct sockaddr_nl addr = { |
| | .nl_family = AF_NETLINK, |
| | }; |
| | int len; |
| |
|
| | memset(&buf.nl, 0, sizeof(buf.nl)); |
| | buf.nl.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN); |
| | buf.nl.nlmsg_flags = NLM_F_REQUEST; |
| | buf.nl.nlmsg_type = GENL_ID_CTRL; |
| |
|
| | genl = (struct genlmsghdr *)buf.buf; |
| | memset(genl, 0, sizeof(*genl)); |
| | genl->cmd = CTRL_CMD_GETFAMILY; |
| |
|
| | rta = (struct rtattr *)(genl + 1); |
| | rta->rta_type = CTRL_ATTR_FAMILY_NAME; |
| | rta->rta_len = RTA_LENGTH(sizeof(UDP_SPLICE_GENL_NAME)); |
| | memcpy(RTA_DATA(rta), UDP_SPLICE_GENL_NAME, sizeof(UDP_SPLICE_GENL_NAME)); |
| | buf.nl.nlmsg_len += rta->rta_len; |
| |
|
| | if (sendto(sock, &buf, buf.nl.nlmsg_len, 0, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| | perror(""sendto failed""); |
| | return -1; |
| | } |
| |
|
| | len = recv(sock, &buf, sizeof(buf), 0); |
| | if (len < 0) { |
| | perror(""recv failed""); |
| | return -1; |
| | } |
| | if (len < sizeof(buf.nl) || buf.nl.nlmsg_len != len) { |
| | errno = EBADMSG; |
| | return -1; |
| | } |
| | if (buf.nl.nlmsg_type == NLMSG_ERROR) { |
| | struct nlmsgerr *errmsg = (struct nlmsgerr *)buf.buf; |
| | errno = -errmsg->error; |
| | return -1; |
| | } |
| |
|
| | len -= sizeof(buf.nl) + sizeof(*genl); |
| | while (RTA_OK(rta, len)) { |
| | if (rta->rta_type == CTRL_ATTR_FAMILY_ID) { |
| | return *(uint16_t *)RTA_DATA(rta); |
| | } |
| | rta = RTA_NEXT(rta, len); |
| | } |
| |
|
| | errno = EBADMSG; |
| | return -1; |
| | } |
| |
|
| | void *udp_splice_open(void) { |
| | struct udp_splice_handle *h; |
| | int retval; |
| | struct sockaddr_nl addr; |
| |
|
| | h = malloc(sizeof(*h)); |
| | if (!h) { |
| | perror(""malloc failed""); |
| | return NULL; |
| | } |
| |
|
| | retval = pthread_mutex_init(&h->lock, NULL); |
| | if (retval) { |
| | errno = retval; |
| | free(h); |
| | return NULL; |
| | } |
| |
|
| | h->sock = socket(PF_NETLINK, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, NETLINK_GENERIC); |
| | if (h->sock < 0) { |
| | perror(""socket creation failed""); |
| | pthread_mutex_destroy(&h->lock); |
| | free(h); |
| | return NULL; |
| | } |
| |
|
| | memset(&addr, 0, sizeof(addr)); |
| | addr.nl_family = AF_NETLINK; |
| | if (bind(h->sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| | perror(""bind failed""); |
| | close(h->sock); |
| | pthread_mutex_destroy(&h->lock); |
| | free(h); |
| | return NULL; |
| | } |
| |
|
| | retval = udp_splice_get_family_id(h->sock); |
| | if (retval < 0) { |
| | close(h->sock); |
| | pthread_mutex_destroy(&h->lock); |
| | free(h); |
| | return NULL; |
| | } |
| |
|
| | h->id = retval; |
| | return h; |
| | } |
| |
|
| | int udp_splice_add(void *handle, int sock, int sock2, uint32_t timeout) { |
| | struct { |
| | struct nlmsghdr nl; |
| | struct genlmsghdr genl; |
| | char attrs[RTA_LENGTH(4) * 3]; |
| | } req; |
| | struct { |
| | struct nlmsghdr nl; |
| | struct nlmsgerr err; |
| | } res; |
| | struct rtattr *rta; |
| | struct sockaddr_nl addr = { .nl_family = AF_NETLINK }; |
| | int len; |
| | struct udp_splice_handle *h = handle; |
| |
|
| | memset(&req, 0, sizeof(req.nl) + sizeof(req.genl)); |
| | req.nl.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN); |
| | req.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; |
| | req.nl.nlmsg_type = h->id; |
| |
|
| | req.genl.cmd = UDP_SPLICE_CMD_ADD; |
| |
|
| | rta = (struct rtattr *)req.attrs; |
| | rta->rta_type = UDP_SPLICE_ATTR_SOCK; |
| | rta->rta_len = RTA_LENGTH(4); |
| | |
| | req.nl.nlmsg_len += rta->rta_len; |
| |
|
| | rta = (struct rtattr *)(((char *)rta) + rta->rta_len); |
| | rta->rta_type = UDP_SPLICE_ATTR_SOCK2; |
| | rta->rta_len = RTA_LENGTH(4); |
| | |
| | req.nl.nlmsg_len += rta->rta_len; |
| |
|
| | if (timeout) { |
| | rta = (struct rtattr *)(((char *)rta) + rta->rta_len); |
| | rta->rta_type = UDP_SPLICE_ATTR_TIMEOUT; |
| | rta->rta_len = RTA_LENGTH(4); |
| | |
| | req.nl.nlmsg_len += rta->rta_len; |
| | } |
| |
|
| | pthread_mutex_lock(&h->lock); |
| | if (sendto(h->sock, &req, req.nl.nlmsg_len, 0, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| | pthread_mutex_unlock(&h->lock); |
| | perror(""sendto failed""); |
| | return -1; |
| | } |
| |
|
| | len = recv(h->sock, &res, sizeof(res), 0); |
| | pthread_mutex_unlock(&h->lock); |
| |
|
| | if (len < 0) { |
| | perror(""recv failed""); |
| | return -1; |
| | } |
| | if (len != sizeof(res) || res.nl.nlmsg_type != NLMSG_ERROR) { |
| | errno = EBADMSG; |
| | return -1; |
| | } |
| | if (res.err.error) { |
| | errno = -res.err.error; |
| | return -1; |
| | } |
| |
|
| | return 0; |
| | } |
| |
|
| | |
| |
|
| |
|
| | ",1 |
| | "#include <stdio.h> |
| | #include <unistd.h> |
| | #include <stdlib.h> |
| | #include <sys/types.h> |
| | #include <sys/stat.h> |
| | #include <string.h> |
| | #include <pthread.h> |
| |
|
| |
|
| |
|
| |
|
| | typedef struct node |
| | { |
| | int data; |
| | struct node* next; |
| | }Node; |
| |
|
| |
|
| | Node *head = NULL; |
| |
|
| |
|
| | pthread_mutex_t mutex; |
| |
|
| | pthread_cond_t cond; |
| |
|
| |
|
| | int produced_num = 0; |
| |
|
| | void* th_producer(void *args) |
| | { |
| | while(1) |
| | { |
| | Node *node = (Node *)malloc(sizeof(Node)); |
| | node->data = rand() % 100; |
| | |
| | pthread_mutex_lock(&mutex); |
| | node->next = head; |
| | head = node; |
| | printf(""%lu生产了一个烧饼:%d\\n"",pthread_self(),head->data); |
| | pthread_mutex_unlock(&mutex); |
| |
|
| |
|
| | if(++produced_num==3) |
| | { |
| | pthread_cond_signal(&cond); |
| | } |
| | sleep(rand() % 3); |
| | } |
| | return NULL; |
| | } |
| |
|
| | void* th_consumer(void *args) |
| | { |
| | while(1) |
| | { |
| | pthread_mutex_lock(&mutex); |
| |
|
| | if(head == NULL) |
| | { |
| | |
| | pthread_cond_wait(&cond,&mutex); |
| | |
| | |
| | } |
| | Node * node = head; |
| | head = head->next; |
| | printf(""%lu消费了一个烧饼:%d\\n"",pthread_self(),node->data); |
| | free(node); |
| | produced_num--; |
| | pthread_mutex_unlock(&mutex); |
| | } |
| | return NULL; |
| | } |
| | int main() |
| | { |
| |
|
| | pthread_t thid_producer,thid_consumer; |
| |
|
| | |
| | pthread_mutex_init(&mutex,NULL); |
| | pthread_cond_init(&cond,NULL); |
| |
|
| |
|
| | pthread_create(&thid_producer,NULL,th_producer,NULL); |
| |
|
| | pthread_create(&thid_consumer,NULL,th_consumer,NULL); |
| |
|
| |
|
| | pthread_join(thid_producer,NULL); |
| | pthread_join(thid_consumer,NULL); |
| |
|
| | |
| | pthread_mutex_destroy(&mutex); |
| | pthread_cond_destroy(&cond); |
| |
|
| | return 0; |
| | } |
| | ",0 |
| | "#include <stdio.h> |
| | #include <stdlib.h> |
| | #include <stdint.h> |
| | #include <unistd.h> |
| | #include <fcntl.h> |
| | #include <sys/ioctl.h> |
| | #include <pthread.h> |
| | #include <linux/spi/spidev.h> |
| | #include <string.h> |
| | #include <errno.h> |
| |
|
| | |
| | #define SPI_MODE SPI_MODE_0 |
| | #define SPI_BITS_PER_WORD 8 |
| | #define SPI_SPEED_HZ 500000 |
| | #define SPI_DELAY 0 |
| | #define FPGA_SPI_DEV ""/dev/spidev0.0"" |
| |
|
| | |
| | #define CMD_SERVO 0x01 |
| | #define CMD_SPEED_ACC_SWITCH 0x02 |
| | #define CMD_AS 0x03 |
| | #define CMD_LED 0x04 |
| | #define CMD_SPEED 0x05 |
| | #define CMD_SPEEDPOLL 0x06 |
| | #define CMD_SPEEDRAW 0x07 |
| | #define SPI_PREAMBLE 0xAA |
| |
|
| | |
| | #define HIGHBYTE(x) ((x) >> 8) |
| | #define LOWBYTE(x) ((x) & 0xFF) |
| |
|
| | int fd; |
| | FILE *logfd = NULL; |
| |
|
| | static uint8_t spi_mode = SPI_MODE; |
| | static uint8_t spi_bits = SPI_BITS_PER_WORD; |
| | static uint32_t spi_speed = SPI_SPEED_HZ; |
| | static uint16_t spi_delay = SPI_DELAY; |
| |
|
| | pthread_mutex_t spi_mutex; |
| |
|
| | int fpga_open() { |
| | int ret; |
| |
|
| | pthread_mutex_init(&spi_mutex, NULL); |
| |
|
| | printf(""Will use SPI to send commands to FPGA\\n""); |
| | printf(""SPI configuration:\\n""); |
| | printf("" + dev: %s\\n"", FPGA_SPI_DEV); |
| | printf("" + mode: %d\\n"", spi_mode); |
| | printf("" + bits per word: %d\\n"", spi_bits); |
| | printf("" + speed: %d Hz (%d KHz)\\n\\n"", spi_speed, spi_speed / 1000); |
| |
|
| | if ((fd = open(FPGA_SPI_DEV, O_RDWR)) < 0) { |
| | perror(""E: fpga: spi: Failed to open dev""); |
| | return -1; |
| | } |
| |
|
| | if ((ret = ioctl(fd, SPI_IOC_WR_MODE, &spi_mode)) < 0) { |
| | perror(""E: fpga: spi: can't set spi mode wr""); |
| | return -1; |
| | } |
| |
|
| | if ((ret = ioctl(fd, SPI_IOC_RD_MODE, &spi_mode)) < 0) { |
| | perror(""E: fpga: spi: can't set spi mode rd""); |
| | return -1; |
| | } |
| |
|
| | if ((ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &spi_bits)) < 0) { |
| | perror(""E: fpga: spi: can't set bits per word wr""); |
| | return -1; |
| | } |
| |
|
| | if ((ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &spi_bits)) < 0) { |
| | perror(""E: fpga: spi: can't set bits per word rd""); |
| | return -1; |
| | } |
| |
|
| | if ((ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &spi_speed)) < 0) { |
| | perror(""E: fpga: spi: can't set speed wr""); |
| | return -1; |
| | } |
| |
|
| | if ((ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &spi_speed)) < 0) { |
| | perror(""E: fpga: spi: can't set speed rd""); |
| | return -1; |
| | } |
| |
|
| | if (fpga_logopen() < 0) { |
| | fprintf(stderr, ""E: fpga: could not open log\\n""); |
| | return -1; |
| | } |
| |
|
| | return 0; |
| | } |
| |
|
| | void fpga_close() { |
| | if (fd >= 0) { |
| | close(fd); |
| | } |
| | if (logfd) { |
| | fclose(logfd); |
| | } |
| | pthread_mutex_destroy(&spi_mutex); |
| | } |
| |
|
| | int fpga_logopen() { |
| | logfd = fopen(""/tmp/ourlog"", ""a""); |
| | if (!logfd) { |
| | perror(""E: fpga: could not open log file""); |
| | return -1; |
| | } |
| | fprintf(logfd, ""--------reopened--------\\n""); |
| | return 0; |
| | } |
| |
|
| | int spisend(unsigned char *rbuf, unsigned char *wbuf, int len) { |
| | int ret; |
| |
|
| | |
| |
|
| | struct spi_ioc_transfer tr = { |
| | .tx_buf = (unsigned long)wbuf, |
| | .rx_buf = (unsigned long)rbuf, |
| | .len = len, |
| | .delay_usecs = spi_delay, |
| | .speed_hz = spi_speed, |
| | .bits_per_word = spi_bits, |
| | }; |
| |
|
| | ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); |
| | if (ret < 1) { |
| | perror(""E: fpga: can't send SPI message""); |
| | |
| | return -1; |
| | } |
| |
|
| | |
| | return ret; |
| | } |
| |
|
| | |
| | |
| |
|
| | void fpga_testservos() { |
| | if (fpga_open() < 0) { |
| | fprintf(stderr, ""E: FPGA: Could not open SPI to FPGA\\n""); |
| | exit(EXIT_FAILURE); |
| | } |
| |
|
| | printf(""Moving servo left\\n""); |
| | fpga_setservo(1, 0); |
| | sleep(2); |
| |
|
| | printf(""Moving servo centre\\n""); |
| | fpga_setservo(1, 4000); |
| | sleep(2); |
| |
|
| | printf(""Moving servo right\\n""); |
| | fpga_setservo(1, 8000); |
| | sleep(2); |
| |
|
| | printf(""Moving servo centre\\n""); |
| | fpga_setservo(1, 4000); |
| |
|
| | fpga_close(); |
| | } |
| |
|
| | ",1 |
| | "#include <stdio.h> |
| | #include <stdlib.h> |
| | #include <unistd.h> |
| | #include <pthread.h> |
| |
|
| | #define NUM_THREAD 3 |
| | #define TCOUNT 5 |
| | #define COUNT_LIMIT 7 |
| | int count = 0; |
| |
|
| | pthread_mutex_t count_mutex; |
| | pthread_cond_t count_threshold_cv; |
| |
|
| | |
| | void *inc_count(void *t) |
| | { |
| | int i; |
| | long my_id = (long)t; |
| |
|
| | for (i = 0; i < TCOUNT; ++i) |
| | { |
| | pthread_mutex_lock(&count_mutex); |
| | count++; |
| | |
| | if (count >= COUNT_LIMIT) |
| | { |
| | printf(""inc_count(): thread %ld, count = %d Threshold reached. "", |
| | my_id, count); |
| | |
| | pthread_cond_signal(&count_threshold_cv); |
| | printf(""Just sent signal.\\n""); |
| | } |
| |
|
| | printf(""inc_count(): thread %ld, count = %d, unlocking mutex.\\n"", |
| | my_id, count); |
| | pthread_mutex_unlock(&count_mutex); |
| |
|
| | sleep(1); |
| | } |
| |
|
| | pthread_exit(NULL); |
| | } |
| |
|
| | |
| | void *watch_count(void *t) |
| | { |
| | long my_id = (long)t; |
| | printf(""Starting watch_count(): thread %ld.\\n"", my_id); |
| |
|
| | |
| | pthread_mutex_lock(&count_mutex); |
| | while (count < COUNT_LIMIT) |
| | { |
| | printf(""watch_count(): thread %ld going into wait...\\n"", my_id); |
| | pthread_cond_wait(&count_threshold_cv, &count_mutex); |
| | printf(""watch_count(): thread %ld Condition signal received.\\n"", my_id); |
| |
|
| | printf(""watch_count(): thread %ld count now = %d.\\n"", my_id, count); |
| | } |
| | pthread_mutex_unlock(&count_mutex); |
| | |
| | pthread_exit(NULL); |
| | } |
| |
|
| | int main() |
| | { |
| | long t1 = 1, t2 = 2, t3 = 3; |
| | pthread_t threads[NUM_THREAD]; |
| | pthread_attr_t attr; |
| |
|
| | |
| | pthread_mutex_init(&count_mutex, NULL); |
| | pthread_cond_init(&count_threshold_cv, NULL); |
| |
|
| | |
| | pthread_attr_init(&attr); |
| | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
| |
|
| | |
| | pthread_create(&threads[0], &attr, watch_count, (void *)t1); |
| | |
| | pthread_create(&threads[1], &attr, inc_count, (void *)t2); |
| | pthread_create(&threads[2], &attr, inc_count, (void *)t3); |
| |
|
| | |
| | for (int i = 0; i < NUM_THREAD; ++i) |
| | { |
| | pthread_join(threads[i], NULL); |
| | } |
| |
|
| | printf(""Main(): Waited on %d threads, final value of count = %d. Done\\n"", |
| | NUM_THREAD, count); |
| |
|
| | |
| | pthread_attr_destroy(&attr); |
| | pthread_mutex_destroy(&count_mutex); |
| | pthread_cond_destroy(&count_threshold_cv); |
| | pthread_exit(NULL); |
| | } |
| |
|
| | ",0 |
| | "#include <stdio.h> |
| | #include <stdlib.h> |
| | #include <pthread.h> |
| |
|
| | #define MAX_NODES 10 |
| |
|
| | pthread_mutex_t dist_mutex; |
| | int dist[MAX_NODES]; |
| |
|
| | void* dijkstra(void* arg) { |
| | int u = *(int*)arg; |
| | |
| | |
| | printf(""Processing node %d\\n"", u); |
| | |
| | free(arg); |
| | return NULL; |
| | } |
| |
|
| | int main() { |
| | pthread_mutex_init(&dist_mutex, NULL); |
| |
|
| | |
| |
|
| | pthread_t threads[MAX_NODES]; |
| | for (int i = 0; i < MAX_NODES; i++) { |
| | int* node = malloc(sizeof(int)); |
| | if (node == NULL) { |
| | perror(""Failed to allocate memory""); |
| | exit(1); |
| | } |
| | |
| | if (pthread_create(&threads[i], NULL, dijkstra, node) != 0) { |
| | perror(""Failed to create thread""); |
| | exit(1); |
| | } |
| | } |
| |
|
| | for (int i = 0; i < MAX_NODES; i++) { |
| | pthread_join(threads[i], NULL); |
| | } |
| |
|
| | pthread_mutex_destroy(&dist_mutex); |
| | return 0; |
| | } |
| |
|
| | ",1 |
| | "#include <stdio.h> |
| | #include <stdlib.h> |
| | #include <string.h> |
| | #include <unistd.h> |
| | #include <pthread.h> |
| | #include <fcntl.h> |
| | #include <time.h> |
| | #include <sys/socket.h> |
| | #include <arpa/inet.h> |
| | #include <errno.h> |
| |
|
| | char sourceFilename[500], destinationFilename[500]; |
| | char nonFlatSourceFilename[500], nonFlatDestinationFilename[500]; |
| |
|
| | off_t offset = 0; |
| | pthread_mutex_t offsetMutex = PTHREAD_MUTEX_INITIALIZER; |
| | int copyingDone = 0; |
| |
|
| | |
| | void *copyWorker(void *arg); |
| | void copyNonFlatFile(); |
| | void runDestinationVM(); |
| | void *sendOffset(void *recvConnection); |
| | void connectionHandler(int connection); |
| | void *runServer(void *arg); |
| | void getVMsInfo(int debugMode); |
| | void sendMessage(int connection, const char *message); |
| | void createServer(void (*handler)(int)); |
| |
|
| | void *copyWorker(void *arg) { |
| | char buf[4096]; |
| | time_t startedAt = time(NULL); |
| |
|
| | int sourceFd = open(sourceFilename, O_RDONLY); |
| | if (sourceFd < 0) { |
| | perror(""Error opening source file""); |
| | pthread_exit(NULL); |
| | } |
| |
|
| | int destinationFd = open(destinationFilename, O_WRONLY | O_CREAT, 0666); |
| | if (destinationFd < 0) { |
| | perror(""Error opening destination file""); |
| | close(sourceFd); |
| | pthread_exit(NULL); |
| | } |
| |
|
| | int res; |
| | do { |
| | pthread_mutex_lock(&offsetMutex); |
| |
|
| | res = pread(sourceFd, buf, sizeof(buf), offset); |
| | if (res <= 0) { |
| | pthread_mutex_unlock(&offsetMutex); |
| | break; |
| | } |
| |
|
| | if (pwrite(destinationFd, buf, res, offset) < 0) { |
| | perror(""Error writing to destination file""); |
| | pthread_mutex_unlock(&offsetMutex); |
| | close(sourceFd); |
| | close(destinationFd); |
| | pthread_exit(NULL); |
| | } |
| |
|
| | offset += res; |
| |
|
| | pthread_mutex_unlock(&offsetMutex); |
| | } while (res == sizeof(buf)); |
| |
|
| | close(sourceFd); |
| | close(destinationFd); |
| |
|
| | time_t endedAt = time(NULL); |
| | printf(""Copying Done! in %ld seconds\\n"", (endedAt - startedAt)); |
| |
|
| | copyingDone = 1; |
| | pthread_exit(NULL); |
| | } |
| |
|
| | void copyNonFlatFile() { |
| | char buf[4096]; |
| | off_t nonFlatOffset = 0; |
| |
|
| | time_t startedAt = time(NULL); |
| |
|
| | int nonFlatSourceFd = open(nonFlatSourceFilename, O_RDONLY); |
| | if (nonFlatSourceFd < 0) { |
| | perror(""Error opening non-flat source file""); |
| | return; |
| | } |
| |
|
| | int nonFlatDestinationFd = open(nonFlatDestinationFilename, O_WRONLY | O_CREAT, 0666); |
| | if (nonFlatDestinationFd < 0) { |
| | perror(""Error opening non-flat destination file""); |
| | close(nonFlatSourceFd); |
| | return; |
| | } |
| |
|
| | int res; |
| | do { |
| | res = pread(nonFlatSourceFd, buf, sizeof(buf), nonFlatOffset); |
| | if (res <= 0) { |
| | break; |
| | } |
| |
|
| | if (pwrite(nonFlatDestinationFd, buf, res, nonFlatOffset) < 0) { |
| | perror(""Error writing to non-flat destination file""); |
| | break; |
| | } |
| |
|
| | nonFlatOffset += res; |
| | } while (res == sizeof(buf)); |
| |
|
| | close(nonFlatSourceFd); |
| | close(nonFlatDestinationFd); |
| |
|
| | time_t endedAt = time(NULL); |
| | printf(""Non-Flat Copying Done! in %ld seconds\\n"", (endedAt - startedAt)); |
| | } |
| |
|
| | void runDestinationVM() { |
| | char storageCommandBuffer[500], destinationChangeUUIDCommand[500]; |
| |
|
| | time_t startedAt = time(NULL); |
| |
|
| | sprintf(destinationChangeUUIDCommand, ""VBoxManage internalcommands sethduuid \\""%s\\"""", nonFlatDestinationFilename); |
| | sprintf(storageCommandBuffer, ""VBoxManage storageattach Destination --medium \\""%s\\"" --storagectl \\""IDE\\"" --port 0 --device 0 --type hdd"", nonFlatDestinationFilename); |
| |
|
| | system(destinationChangeUUIDCommand); |
| | system(storageCommandBuffer); |
| |
|
| | time_t endedAt = time(NULL); |
| | printf(""Running Destination in %ld seconds\\n"", (endedAt - startedAt)); |
| | } |
| |
|
| | void *sendOffset(void *recvConnection) { |
| | int connection = (intptr_t)recvConnection; |
| | char offsetBuffer[50]; |
| | char clientMessage[2000]; |
| |
|
| | do { |
| | memset(offsetBuffer, 0, sizeof(offsetBuffer)); |
| | memset(clientMessage, 0, sizeof(clientMessage)); |
| |
|
| | recv(connection, clientMessage, sizeof(clientMessage), 0); |
| |
|
| | if (!copyingDone) { |
| | pthread_mutex_lock(&offsetMutex); |
| |
|
| | sprintf(offsetBuffer, ""%zu"", offset); |
| | sendMessage(connection, offsetBuffer); |
| |
|
| | recv(connection, clientMessage, sizeof(clientMessage), 0); |
| | pthread_mutex_unlock(&offsetMutex); |
| | } else { |
| | if (strcmp(clientMessage, ""SUSPENDING"") != 0) { |
| | strcpy(offsetBuffer, ""DONE""); |
| | sendMessage(connection, offsetBuffer); |
| | } |
| | } |
| | } while (strcmp(clientMessage, ""CLOSE"") != 0); |
| |
|
| | close(connection); |
| | printf(""\\tConnection Closed\\n""); |
| |
|
| | copyNonFlatFile(); |
| | runDestinationVM(); |
| |
|
| | pthread_exit(NULL); |
| | } |
| |
|
| | void connectionHandler(int connection) { |
| | pthread_t thread; |
| | pthread_create(&thread, NULL, sendOffset, (void *)(intptr_t)connection); |
| | } |
| |
|
| | void *runServer(void *arg) { |
| | createServer(connectionHandler); |
| | pthread_exit(NULL); |
| | } |
| |
|
| | void getVMsInfo(int debugMode) { |
| | if (debugMode) { |
| | strcpy(sourceFilename, ""/path/to/source-flat.vmdk""); |
| | strcpy(nonFlatSourceFilename, ""/path/to/source.vmdk""); |
| | strcpy(destinationFilename, ""/path/to/destination-flat.vmdk""); |
| | strcpy(nonFlatDestinationFilename, ""/path/to/destination.vmdk""); |
| | } else { |
| | printf(""Source File Path: ""); |
| | fgets(sourceFilename, sizeof(sourceFilename), stdin); |
| | printf(""Source File Path (non-flat): ""); |
| | fgets(nonFlatSourceFilename, sizeof(nonFlatSourceFilename), stdin); |
| | printf(""Destination File Path (Will be created automatically): ""); |
| | fgets(destinationFilename, sizeof(destinationFilename), stdin); |
| | printf(""Destination File Path (non-flat) (Will be created automatically): ""); |
| | fgets(nonFlatDestinationFilename, sizeof(nonFlatDestinationFilename), stdin); |
| |
|
| | sourceFilename[strcspn(sourceFilename, ""\\n"")] = 0; |
| | nonFlatSourceFilename[strcspn(nonFlatSourceFilename, ""\\n"")] = 0; |
| | destinationFilename[strcspn(destinationFilename, ""\\n"")] = 0; |
| | nonFlatDestinationFilename[strcspn(nonFlatDestinationFilename, ""\\n"")] = 0; |
| | } |
| | } |
| |
|
| | int main() { |
| | pthread_t copyWorkerThread, serverThread; |
| |
|
| | getVMsInfo(1); |
| |
|
| | pthread_create(©WorkerThread, NULL, copyWorker, NULL); |
| | pthread_create(&serverThread, NULL, runServer, NULL); |
| |
|
| | pthread_join(copyWorkerThread, NULL); |
| | pthread_join(serverThread, NULL); |
| |
|
| | printf(""Done!\\n""); |
| | return 0; |
| | } |
| |
|
| | ",0 |
| | "#include <stdio.h> |
| | #include <stdlib.h> |
| | #include <pthread.h> |
| | #include <math.h> |
| | #include <sys/time.h> |
| | #include <string.h> |
| |
|
| | |
| | pthread_mutex_t minimum_value_lock; |
| | long int partial_list_size; |
| | int minimum_value; |
| | long int *list; |
| | long int NumElements, CLASS_SIZE; |
| | int NumThreads; |
| |
|
| | void *find_min(void *) ; |
| |
|
| | pthread_mutex_t minimum_value_lock; |
| |
|
| | long int partial_list_size; |
| | int minimum_value; |
| | long int *list; |
| | long int NumElements, CLASS_SIZE; |
| | int NumThreads; |
| |
|
| | int main (int argc,char * argv[]) |
| | { |
| |
|
| | pthread_t *threads; |
| | pthread_attr_t pta; |
| | int iteration,THREADS,ret_count; |
| | double time_start, time_end; |
| | struct timeval tv; |
| | struct timezone tz; |
| | double MemoryUsed = 0.0; |
| | char * CLASS; |
| |
|
| | int counter; |
| | printf(""\\n\\t\\t--------------------------------------------------------------------------""); |
| | printf(""\\n\\t\\t Centre for Development of Advanced Computing (C-DAC)""); |
| | printf(""\\n\\t\\t C-DAC Multi Core Benchmark Suite 1.0""); |
| | printf(""\\n\\t\\t Email : RarchK""); |
| | printf(""\\n\\t\\t---------------------------------------------------------------------------""); |
| | printf(""\\n\\t\\t Objective : Sorting Single Dimension Array (Integer Operations)\\n ""); |
| | printf(""\\n\\t\\t Performance of Sorting a Minimum value in a large Single Dimension Array ""); |
| | printf(""\\n\\t\\t on Multi Socket Multi Core Processor using 1/2/4/8 threads \\n""); |
| | printf(""\\n\\t\\t..........................................................................\\n""); |
| |
|
| | if( argc != 3 ){ |
| |
|
| | printf(""\\t\\t Very Few Arguments\\n ""); |
| | printf(""\\t\\t Syntax : exec <Class-Size> <Threads>\\n""); |
| | exit(-1); |
| | } |
| | else { |
| | CLASS = argv[1]; |
| | THREADS = atoi(argv[2]); |
| | } |
| | if( strcmp(CLASS, ""A"" )==0){ |
| | CLASS_SIZE = 10000000; |
| | } |
| | if( strcmp(CLASS, ""B"" )==0){ |
| | CLASS_SIZE = 100000000; |
| | } |
| | if( strcmp(CLASS, ""C"" )==0){ |
| | CLASS_SIZE = 1000000000; |
| | } |
| |
|
| | NumElements = CLASS_SIZE; |
| | NumThreads = THREADS; |
| | printf(""\\n\\t\\t Array Size : %ld"",NumElements); |
| | printf(""\\n\\t\\t Threads : %d"",NumThreads); |
| | printf(""\\n""); |
| |
|
| | if (NumThreads < 1 ) |
| | { |
| | printf(""\\n Number of threads must be greater than zero. Aborting ...\\n""); |
| | return 0; |
| | } |
| |
|
| | if ((NumThreads != 1) && (NumThreads != 2) && (NumThreads != 4) && (NumThreads != 8)) |
| | { |
| | printf(""\\n Number of Threads must be 1 or 2 or 4 or 8. Aborting ...\\n""); |
| | return 0; |
| | } |
| |
|
| | if ( ( NumElements % NumThreads ) != 0 ) |
| | { |
| | printf(""\\n Number of threads not a factor of Integer List size. Aborting.\\n""); |
| | return 0 ; |
| | } |
| |
|
| |
|
| | partial_list_size = NumElements / NumThreads; |
| |
|
| | list = (long int *)malloc(sizeof(long int) * NumElements); |
| | MemoryUsed += ( NumElements * sizeof(long int)); |
| |
|
| | gettimeofday(&tv, &tz); |
| | time_start = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0; |
| |
|
| | for(counter = 0 ; counter < NumElements ; counter++){ |
| | srand48((unsigned int)NumElements); |
| | list[counter] = (rand()%1000)+1.0; |
| | } |
| |
|
| | threads = (pthread_t *)malloc(sizeof(pthread_t)*NumThreads); |
| |
|
| | minimum_value = list[0]; |
| |
|
| | ret_count=pthread_mutex_init(&minimum_value_lock, 0); |
| | if(ret_count) |
| | { |
| | printf(""\\n ERROR : Return code from pthread_mutex_init() is %d "",ret_count); |
| | exit(-1); |
| | } |
| |
|
| | ret_count=pthread_attr_init(&pta); |
| | if(ret_count) |
| | { |
| | printf(""\\n ERROR : Return code from pthread_attr_init() is %d "",ret_count); |
| | exit(-1); |
| | } |
| |
|
| | pthread_attr_setscope(&pta,PTHREAD_SCOPE_SYSTEM); |
| |
|
| | for(counter = 0 ; counter < NumThreads ; counter++) |
| | { |
| | ret_count=pthread_create(&threads[counter],&pta,(void *(*) (void *)) find_min,(void *) (counter+1)); |
| | if(ret_count) |
| | { |
| | printf(""\\n ERROR : Return code from pthread_create() is %d "",ret_count); |
| | exit(-1); |
| | } |
| | } |
| |
|
| | for(counter = 0 ; counter < NumThreads ; counter++) |
| | { |
| | ret_count=pthread_join(threads[counter],0); |
| | if(ret_count) |
| | { |
| | printf(""\\n ERROR : Return code from pthread_join() is %d "",ret_count); |
| | exit(-1); |
| | } |
| | } |
| | ret_count=pthread_attr_destroy(&pta); |
| | if(ret_count) |
| | { |
| | printf(""\\n ERROR : Return code from pthread_attr_destroy() is %d "",ret_count); |
| | exit(-1); |
| | } |
| |
|
| | gettimeofday(&tv, &tz); |
| | time_end = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0; |
| |
|
| | printf(""\\n\\t\\t Minimum Value found in the Integer list : %d"",minimum_value); |
| | printf(""\\n\\t\\t Memory Utilised : %lf MB"",(MemoryUsed / (1024*1024))); |
| | printf(""\\n\\t\\t Time Taken in Seconds (T) : %lf Seconds"",( time_end - time_start)); |
| | printf(""\\n\\t\\t..........................................................................\\n""); |
| |
|
| |
|
| | free(list); |
| | free(threads); |
| | return 0; |
| |
|
| | } |
| | void *find_min(void * myid ) { |
| |
|
| | int my_min; |
| | long int counter; |
| |
|
| | int myId = (int)myid; |
| |
|
| | my_min = list[(myId-1)*partial_list_size]; |
| |
|
| | for (counter = ((myId - 1) * partial_list_size); counter <= ((myId * partial_list_size) - 1); counter++){ |
| | if (list[counter] < my_min) |
| | my_min = list[counter]; |
| | } |
| |
|
| |
|
| |
|
| |
|
| | pthread_mutex_lock(&minimum_value_lock) ; |
| | if (my_min < minimum_value) |
| | minimum_value = my_min; |
| |
|
| |
|
| | pthread_mutex_unlock(&minimum_value_lock) ; |
| |
|
| | pthread_exit(0); |
| | } |
| | ",1 |
| | "#include <stdio.h> |
| | #include <stdlib.h> |
| | #include <string.h> |
| | #include <pthread.h> |
| | #include <unistd.h> |
| | #include <sys/time.h> |
| | #include <fcntl.h> |
| | #include <stdint.h> |
| | #include <sys/stat.h> |
| | #include <errno.h> |
| |
|
| |
|
| | static char *root; |
| | static int workers; |
| | static int trials; |
| | static int record_absolute; |
| | static struct timeval asbolute_start; |
| |
|
| | static pthread_mutex_t worker_sync_lock = PTHREAD_MUTEX_INITIALIZER; |
| | static pthread_cond_t worker_sync_cond = PTHREAD_COND_INITIALIZER; |
| | static volatile int worker_sync_t = -1; |
| | static volatile int workers_alive = 0; |
| |
|
| | int timeval_subtract(struct timeval *result, struct timeval *x, |
| | struct timeval *y) { |
| | if(x->tv_usec < y->tv_usec) { |
| | int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; |
| | y->tv_usec -= 1000000 * nsec; |
| | y->tv_sec += nsec; |
| | } |
| | if(x->tv_usec - y->tv_usec > 1000000) { |
| | int nsec = (x->tv_usec - y->tv_usec) / 1000000; |
| | y->tv_usec += 1000000 * nsec; |
| | y->tv_sec -= nsec; |
| | } |
| |
|
| | result->tv_sec = x->tv_sec - y->tv_sec; |
| | result->tv_usec = x->tv_usec - y->tv_usec; |
| |
|
| | return x->tv_sec < y->tv_sec; |
| | } |
| |
|
| | static void pthread_usleep(unsigned int usecs) { |
| | int result; |
| | pthread_cond_t timercond = PTHREAD_COND_INITIALIZER; |
| | pthread_mutex_t timerlock = PTHREAD_MUTEX_INITIALIZER; |
| | struct timespec timerexpires; |
| |
|
| | clock_gettime(CLOCK_REALTIME, &timerexpires); |
| | timerexpires.tv_nsec += usecs * 1000; |
| | if(timerexpires.tv_nsec >= 1000000000) { |
| | timerexpires.tv_sec += timerexpires.tv_nsec / 1000000000; |
| | timerexpires.tv_nsec = timerexpires.tv_nsec % 1000000000; |
| | } |
| |
|
| | pthread_mutex_lock(&timerlock); |
| | result = ~ETIMEDOUT; |
| | while(result != ETIMEDOUT) |
| | result = pthread_cond_timedwait(&timercond, &timerlock, &timerexpires); |
| | pthread_mutex_unlock(&timerlock); |
| | } |
| |
|
| | void *worker_run(void *data) { |
| | int id = (int) data; |
| |
|
| | char clkpath[256]; |
| | sprintf(clkpath, ""%s/clock"", root); |
| |
|
| | int clkfd = open(clkpath, O_RDONLY); |
| | if(clkfd < 0) { |
| | perror(""open""); |
| | exit(1); |
| | } |
| |
|
| | char testpath[256]; |
| | sprintf(testpath, ""%s/%d"", root, id); |
| |
|
| | int fd = open(testpath, O_RDWR | O_CREAT, 0777); |
| | if(fd < 0) { |
| | perror(""open""); |
| | exit(1); |
| | } |
| |
|
| | char buf[1024]; |
| | memset(buf, 'x', sizeof(buf)); |
| | ((int *) buf)[0] = id; |
| |
|
| | uint64_t *deltas = malloc(sizeof(uint64_t) * trials * 2); |
| |
|
| | pthread_mutex_lock(&worker_sync_lock); |
| | workers_alive++; |
| | pthread_mutex_unlock(&worker_sync_lock); |
| |
|
| |
|
| | if(id == 0) { |
| | while(workers_alive < workers) |
| | pthread_usleep(100000); |
| |
|
| | struct stat statbuf; |
| | if(fstat(clkfd, &statbuf) < 0) { |
| | perror(""fstat""); |
| | exit(1); |
| | } |
| | } |
| |
|
| | int t; |
| | for(t = 0; ; t++) { |
| | if(id == 0) { |
| | if(t >= trials && workers_alive == 1) |
| | break; |
| | } else { |
| | if(t >= trials) |
| | break; |
| |
|
| | pthread_mutex_lock(&worker_sync_lock); |
| | while(worker_sync_t < t) |
| | pthread_cond_wait(&worker_sync_cond, &worker_sync_lock); |
| | pthread_mutex_unlock(&worker_sync_lock); |
| | } |
| |
|
| | struct timeval before; |
| | gettimeofday(&before, 0); |
| |
|
| | if(lseek(fd, 0, 0) < 0) { |
| | perror(""lseek""); |
| | exit(1); |
| | } |
| | if(write(fd, buf, sizeof(buf)) < 0) { |
| | perror(""write""); |
| | exit(1); |
| | } |
| |
|
| | struct timeval after; |
| | gettimeofday(&after, 0); |
| |
|
| | struct timeval diff; |
| | if(record_absolute) |
| | timeval_subtract(&diff, &after, &asbolute_start); |
| | else |
| | timeval_subtract(&diff, &after, &before); |
| |
|
| | deltas[t] = (diff.tv_sec * 1000000) + diff.tv_usec; |
| |
|
| | if(id == 0) { |
| | pthread_mutex_lock(&worker_sync_lock); |
| | worker_sync_t = t; |
| | pthread_cond_broadcast(&worker_sync_cond); |
| | pthread_mutex_unlock(&worker_sync_lock); |
| |
|
| | pthread_usleep(49000); |
| | } |
| | } |
| |
|
| | pthread_mutex_lock(&worker_sync_lock); |
| | workers_alive--; |
| | pthread_mutex_unlock(&worker_sync_lock); |
| |
|
| | return deltas; |
| | } |
| |
|
| | int main(int argc, char *argv[]) { |
| | if(argc < 4 || argc > 5) { |
| | printf(""Usage: concurio [mount-point] [workers] [trials] [-a]\\n""); |
| | exit(1); |
| | } |
| |
|
| | root = argv[1]; |
| | workers = strtol(argv[2], 0, 10); |
| | trials = strtol(argv[3], 0, 10); |
| | if(argc == 5 && strcmp(argv[4], ""-a"") == 0) |
| | record_absolute = 1; |
| | else |
| | record_absolute = 0; |
| |
|
| | gettimeofday(&asbolute_start, 0); |
| |
|
| | pthread_t *worker_threads = malloc(sizeof(pthread_t) * workers); |
| |
|
| | int w; |
| | for(w = 0; w < workers; w++) |
| | pthread_create(&worker_threads[w], 0, worker_run, (void *) w); |
| |
|
| | uint64_t **worker_deltas = malloc(sizeof(uint64_t *) * workers); |
| | for(w = 0; w < workers; w++) |
| | pthread_join(worker_threads[w], (void **) &worker_deltas[w]); |
| |
|
| | if(record_absolute) |
| | printf(""absolute\\n""); |
| | else |
| | printf(""write-time\\n""); |
| |
|
| | int t; |
| | for(w = 0; w < workers; w++) { |
| | for(t = 0; t < trials; t++) |
| | printf(""%d: %llu\\n"", w, worker_deltas[w][t]); |
| | free(worker_deltas[w]); |
| | } |
| |
|
| | exit(0); |
| | } |
| | ",0 |
| | "#include <stdio.h> |
| | #include <stdlib.h> |
| | #include <string.h> |
| | #include <pthread.h> |
| | #include <time.h> |
| |
|
| | |
| | static int hash_index; |
| | static char buffer_postdata[1024 * 1024 * 100]; |
| | |
| | static void analysis_web_from_job(struct Job *current_job); |
| | static void analysis_web_from_http_list(struct Http_List *list,time_t current_time, |
| | struct tuple4 addr); |
| | static void analysis_web_from_http(struct Http *http,time_t current_time, |
| | struct tuple4 addr); |
| | static void process_function_actual(int job_type); |
| | static int process_judege(struct Job *job); |
| | static void *process_function(void *); |
| |
|
| | static int hash_index; |
| | static char buffer_postdata[1024*1024*100]; |
| |
|
| | static void analysis_web_from_job(struct Job *current_job){ |
| |
|
| | struct Http_RR *http_rr = current_job->http_rr; |
| |
|
| | if(http_rr == 0) |
| | return; |
| |
|
| | analysis_web_from_http_list(http_rr->request_list,current_job->time, |
| | current_job->ip_and_port); |
| | analysis_web_from_http_list(http_rr->response_list,current_job->time, |
| | current_job->ip_and_port); |
| | } |
| |
|
| | static void analysis_web_from_http_list(struct Http_List *list,time_t current_time, |
| | struct tuple4 addr){ |
| |
|
| | if(list == 0) |
| | return; |
| |
|
| | struct Http * point = list->head; |
| | while(point != 0){ |
| |
|
| | analysis_web_from_http(point,current_time, addr); |
| | point = point->next; |
| | } |
| | } |
| |
|
| | static void analysis_web_from_http(struct Http *http,time_t current_time, |
| | struct tuple4 addr){ |
| |
|
| | if(http->type != PATTERN_REQUEST_HEAD) |
| | return; |
| |
|
| | struct WebInformation webinfo; |
| | webinfo.request = http->method; |
| | webinfo.host = http->host; |
| | webinfo.url = http->uri; |
| | webinfo.referer = http->referer; |
| | webinfo.time = current_time; |
| | webinfo.data_length = 0; |
| | webinfo.data_type = 0; |
| | webinfo.data = 0; |
| |
|
| | strcpy(webinfo.srcip, int_ntoa(addr.saddr)); |
| | strcpy(webinfo.dstip, int_ntoa(addr.daddr)); |
| |
|
| | char segment[] = ""\\n""; |
| |
|
| | if(strstr(webinfo.request,""POST"")!= 0){ |
| |
|
| | int length = 0; |
| | struct Entity_List *entity_list; |
| | struct Entity *entity; |
| |
|
| | entity_list = http->entity_list; |
| | if(entity_list != 0){ |
| | entity = entity_list->head; |
| | while(entity != 0){ |
| |
|
| | if(entity->entity_length <= 0 || entity->entity_length > 1024*1024*100){ |
| | entity = entity->next; |
| | continue; |
| | } |
| |
|
| | length += entity->entity_length; |
| | length += 1; |
| | entity = entity->next; |
| | } |
| | } |
| |
|
| |
|
| | if(length > 1 && length < 1024*1024*100){ |
| |
|
| | memset(buffer_postdata,0,length+1); |
| |
|
| | entity_list = http->entity_list; |
| |
|
| |
|
| | if(entity_list != 0){ |
| |
|
| | entity = entity_list->head; |
| | while(entity != 0){ |
| | if(entity->entity_length <= 0 || entity->entity_length > 1024*1024*100){ |
| | entity = entity->next; |
| | continue; |
| | } |
| | memcpy(buffer_postdata + length,entity->entity_content,entity->entity_length); |
| | length += entity->entity_length; |
| |
|
| | memcpy(buffer_postdata + length,segment,1); |
| | length += 1; |
| | entity = entity->next; |
| | } |
| | } |
| | webinfo.data_length = length; |
| | webinfo.data_type = """"; |
| | webinfo.data = buffer_postdata; |
| | } |
| |
|
| | } |
| |
|
| | sql_factory_add_web_record(&webinfo,hash_index); |
| | } |
| |
|
| | static void *process_function(void *arg){ |
| | int job_type = JOB_TYPE_WEB; |
| | while(1){ |
| | pthread_mutex_lock(&(job_mutex_for_cond[job_type])); |
| | pthread_cond_wait(&(job_cond[job_type]),&(job_mutex_for_cond[job_type])); |
| | pthread_mutex_unlock(&(job_mutex_for_cond[job_type])); |
| |
|
| | process_function_actual(job_type); |
| | } |
| | return 0; |
| | } |
| |
|
| | static void process_function_actual(int job_type){ |
| | struct Job_Queue private_jobs; |
| | private_jobs.front = 0; |
| | private_jobs.rear = 0; |
| | get_jobs(job_type,&private_jobs); |
| | struct Job current_job; |
| |
|
| | while(!jobqueue_isEmpty(&private_jobs)){ |
| |
|
| | jobqueue_delete(&private_jobs,¤t_job); |
| | hash_index = current_job.hash_index; |
| | analysis_web_from_job(¤t_job); |
| | if(current_job.http_rr != 0) |
| | free_http_rr(current_job.http_rr); |
| | } |
| | } |
| |
|
| | static int process_judege(struct Job *job){ |
| | return 1; |
| | } |
| |
|
| | extern void web_analysis_init(){ |
| | register_job(JOB_TYPE_WEB,process_function,process_judege,CALL_BY_HTTP_ANALYSIS); |
| | } |
| | ",1 |
| | "#include <stdio.h> |
| | #include <stdlib.h> |
| | #include <string.h> |
| | #include <unistd.h> |
| | #include <pthread.h> |
| | #include <sys/socket.h> |
| | #include <linux/netlink.h> |
| | #include <errno.h> |
| |
|
| | enum mode { |
| | MODE_MB, |
| | MODE_MEMBARRIER, |
| | MODE_COMPILER_BARRIER, |
| | MODE_MEMBARRIER_MISSING_REGISTER, |
| | }; |
| |
|
| | enum mode mode; |
| |
|
| | struct map_test { |
| | int x, y; |
| | int ref; |
| | int r2, r4; |
| | int r2_ready, r4_ready; |
| | int killed; |
| | pthread_mutex_t lock; |
| | }; |
| |
|
| | static void check_parent_regs(struct map_test *map_test, int r2) |
| | { |
| | pthread_mutex_lock(&map_test->lock); |
| | if (map_test->r4_ready) { |
| | if (r2 == 0 && map_test->r4 == 0) { |
| | fprintf(stderr, ""Error detected!\\n""); |
| | CMM_STORE_SHARED(map_test->killed, 1); |
| | abort(); |
| | } |
| | map_test->r4_ready = 0; |
| | map_test->x = 0; |
| | map_test->y = 0; |
| | } else { |
| | map_test->r2 = r2; |
| | map_test->r2_ready = 1; |
| | } |
| | pthread_mutex_unlock(&map_test->lock); |
| | } |
| |
|
| | static void check_child_regs(struct map_test *map_test, int r4) |
| | { |
| | pthread_mutex_lock(&map_test->lock); |
| | if (map_test->r2_ready) { |
| | if (r4 == 0 && map_test->r2 == 0) { |
| | fprintf(stderr, ""Error detected!\\n""); |
| | CMM_STORE_SHARED(map_test->killed, 1); |
| | abort(); |
| | } |
| | map_test->r2_ready = 0; |
| | map_test->x = 0; |
| | map_test->y = 0; |
| | } else { |
| | map_test->r4 = r4; |
| | map_test->r4_ready = 1; |
| | } |
| | pthread_mutex_unlock(&map_test->lock); |
| | } |
| |
|
| | static void loop_parent(struct map_test *map_test) |
| | { |
| | int i, r2; |
| |
|
| | for (i = 0; i < 100000000; i++) { |
| | uatomic_inc(&map_test->ref); |
| | while (uatomic_read(&map_test->ref) < 2 * (i + 1)) { |
| | if (map_test->killed) |
| | abort(); |
| | caa_cpu_relax(); |
| | } |
| | CMM_STORE_SHARED(map_test->x, 1); |
| | switch (mode) { |
| | case MODE_MB: |
| | cmm_smp_mb(); |
| | break; |
| | case MODE_MEMBARRIER: |
| | case MODE_MEMBARRIER_MISSING_REGISTER: |
| | if (-ENOSYS) { |
| | perror(""membarrier""); |
| | CMM_STORE_SHARED(map_test->killed, 1); |
| | abort(); |
| | } |
| | break; |
| | case MODE_COMPILER_BARRIER: |
| | cmm_barrier(); |
| | break; |
| | } |
| | r2 = CMM_LOAD_SHARED(map_test->y); |
| | check_parent_regs(map_test, r2); |
| | } |
| | } |
| |
|
| | static void loop_child(struct map_test *map_test) |
| | { |
| | int i, r4; |
| |
|
| | switch (mode) { |
| | case MODE_MEMBARRIER: |
| | if (-ENOSYS) { |
| | perror(""membarrier""); |
| | CMM_STORE_SHARED(map_test->killed, 1); |
| | abort(); |
| | } |
| | break; |
| | default: |
| | break; |
| | } |
| | for (i = 0; i < 100000000; i++) { |
| | uatomic_inc(&map_test->ref); |
| | while (uatomic_read(&map_test->ref) < 2 * (i + 1)) { |
| | if (map_test->killed) |
| | abort(); |
| | caa_cpu_relax(); |
| | } |
| |
|
| | CMM_STORE_SHARED(map_test->y, 1); |
| | switch (mode) { |
| | case MODE_MB: |
| | cmm_smp_mb(); |
| | break; |
| | case MODE_MEMBARRIER: |
| | case MODE_MEMBARRIER_MISSING_REGISTER: |
| |
|
| | cmm_barrier(); |
| | break; |
| | case MODE_COMPILER_BARRIER: |
| | cmm_barrier(); |
| | break; |
| | } |
| | r4 = CMM_LOAD_SHARED(map_test->x); |
| | check_child_regs(map_test, r4); |
| | } |
| | } |
| |
|
| | void print_arg_error(void) |
| | { |
| | fprintf(stderr, ""Please specify test mode: <m>: paired mb, <s>: sys-membarrier, <c>: compiler barrier (error), <n>: sys-membarrier with missing registration (error).\\n""); |
| | } |
| |
|
| | int main(int argc, char **argv) |
| | { |
| | char namebuf[PATH_MAX]; |
| | pid_t pid; |
| | int fd, ret = 0; |
| | void *buf; |
| | struct map_test *map_test; |
| | pthread_mutexattr_t attr; |
| |
|
| | if (argc < 2) { |
| | print_arg_error(); |
| | return -1; |
| | } |
| | if (!strcmp(argv[1], ""-m"")) { |
| | mode = MODE_MB; |
| | } else if (!strcmp(argv[1], ""-s"")) { |
| | mode = MODE_MEMBARRIER; |
| | } else if (!strcmp(argv[1], ""-c"")) { |
| | mode = MODE_COMPILER_BARRIER; |
| | } else if (!strcmp(argv[1], ""-n"")) { |
| | mode = MODE_MEMBARRIER_MISSING_REGISTER; |
| | } else { |
| | print_arg_error(); |
| | return -1; |
| | } |
| |
|
| | buf = mmap(0, 4096, PROT_READ | PROT_WRITE, |
| | MAP_ANONYMOUS | MAP_SHARED, -1, 0); |
| | if (buf == MAP_FAILED) { |
| | perror(""mmap""); |
| | ret = -1; |
| | goto end; |
| | } |
| | map_test = (struct map_test *)buf; |
| | pthread_mutexattr_init(&attr); |
| | pthread_mutexattr_setpshared(&attr, 1); |
| | pthread_mutex_init(&map_test->lock, &attr); |
| | pid = fork(); |
| | if (pid < 0) { |
| | perror(""fork""); |
| | ret = -1; |
| | goto unmap; |
| | } |
| | if (!pid) { |
| |
|
| | loop_child(map_test); |
| | return 0; |
| | } |
| |
|
| | loop_parent(map_test); |
| | pid = waitpid(pid, 0, 0); |
| | if (pid < 0) { |
| | perror(""waitpid""); |
| | ret = -1; |
| | } |
| | unmap: |
| | pthread_mutex_destroy(&map_test->lock); |
| | pthread_mutexattr_destroy(&attr); |
| | if (munmap(buf, 4096)) { |
| | perror(""munmap""); |
| | ret = -1; |
| | } |
| | end: |
| | return ret; |
| | } |
| | ",0 |
| | "#include <stdio.h> |
| | #include <stdlib.h> |
| | #include <pthread.h> |
| | #include <semaphore.h> |
| | #include <sched.h> |
| | #include <string.h> |
| |
|
| | typedef enum _color { |
| | ZERO = 0, |
| | BLUE = 1, |
| | RED = 2, |
| | YELLOW = 3, |
| | INVALID = 4, |
| | } color; |
| |
|
| | char *colors[] = { ""zero"", |
| | ""blue"", |
| | ""red"", |
| | ""yellow"", |
| | ""invalid"" |
| | }; |
| |
|
| | char *digits[] = { ""zero"", ""one"", ""two"", ""three"", ""four"", ""five"", |
| | ""six"", ""seven"", ""eight"", ""nine"" |
| | }; |
| |
|
| | sem_t at_most_two; |
| | sem_t mutex; |
| | sem_t sem_priv; |
| | sem_t sem_print; |
| |
|
| | pthread_mutex_t print_mutex; |
| |
|
| | int meetings_left = 0; |
| | int first_arrived = 0; |
| | int done = 0; |
| |
|
| | typedef struct _creature { |
| | color my_color; |
| | pthread_t id; |
| | int number_of_meetings; |
| | } chameos; |
| |
|
| | chameos A; |
| | chameos B; |
| |
|
| | static color |
| | compliment_color(color c1, color c2) { |
| | color result; |
| |
|
| | switch(c1) { |
| | case BLUE: |
| | switch(c2) { |
| | case BLUE: |
| | result = BLUE; |
| | break; |
| | case RED: |
| | result = YELLOW; |
| | break; |
| | case YELLOW: |
| | result = RED; |
| | break; |
| | default: |
| | printf(""error complementing colors: %d, %d\\n"", c1, c2); |
| | exit(1); |
| | } |
| | break; |
| | case RED: |
| | switch(c2) { |
| | case BLUE: |
| | result = YELLOW; |
| | break; |
| | case RED: |
| | result = RED; |
| | break; |
| | case YELLOW: |
| | result = BLUE; |
| | break; |
| | default: |
| | printf(""error complementing colors: %d, %d\\n"", c1, c2); |
| | exit(2); |
| | } |
| | break; |
| | case YELLOW: |
| | switch(c2) { |
| | case BLUE: |
| | result = RED; |
| | break; |
| | case RED: |
| | result = BLUE; |
| | break; |
| | case YELLOW: |
| | result = YELLOW; |
| | break; |
| | default: |
| | printf(""error complementing colors: %d, %d\\n"", c1, c2); |
| | exit(3); |
| | } |
| | break; |
| | default: |
| | printf(""error complementing colors: %d, %d\\n"", c1, c2); |
| | exit(4); |
| | } |
| | return result; |
| | } |
| |
|
| | static void |
| | spell_the_number(int prefix, int number) { |
| | char *string_number; |
| | int string_length; |
| | int i; |
| | int digit; |
| | int output_so_far = 0; |
| | char buff[1024]; |
| |
|
| | if(prefix != -1) { |
| | output_so_far = sprintf(buff, ""%d"", prefix); |
| | } |
| |
|
| | string_number = malloc(sizeof(char)*10); |
| | string_length = sprintf(string_number, ""%d"", number); |
| | for(i = 0; i < string_length; i++) { |
| | digit = string_number[i] - '0'; |
| | output_so_far += sprintf(buff+output_so_far, "" %s"", digits[digit]); |
| | } |
| | printf(""%s\\n"",buff); |
| |
|
| | } |
| |
|
| | static chameos * |
| | meeting(chameos c) { |
| | chameos *other_critter; |
| | other_critter = malloc(sizeof(chameos)); |
| |
|
| | sem_wait(&at_most_two); |
| | if(done == 1) { |
| | sem_post(&at_most_two); |
| | return 0; |
| | } |
| |
|
| | sem_wait(&mutex); |
| | if(done == 1) { |
| | sem_post(&mutex); |
| | sem_post(&at_most_two); |
| | return 0; |
| | } |
| |
|
| | if(first_arrived == 0) { |
| | first_arrived = 1; |
| |
|
| | A.my_color = c.my_color; |
| | A.id = c.id; |
| |
|
| | sem_post(&mutex); |
| | sem_wait(&sem_priv); |
| |
|
| | other_critter->my_color = B.my_color; |
| | other_critter->id = B.id; |
| |
|
| | meetings_left--; |
| | if(meetings_left == 0) { |
| | done = 1; |
| | } |
| |
|
| | sem_post(&mutex); |
| | sem_post(&at_most_two); sem_post(&at_most_two); |
| | } else { |
| | first_arrived = 0; |
| |
|
| | B.my_color = c.my_color; |
| | B.id = c.id; |
| |
|
| | other_critter->my_color = A.my_color; |
| | other_critter->id = A.id; |
| |
|
| | sem_post(&sem_priv); |
| | } |
| |
|
| | return other_critter; |
| | } |
| |
|
| | static void * |
| | creature(void *arg) { |
| | chameos critter; |
| | critter.my_color = (color)arg; |
| | critter.id = pthread_self(); |
| | critter.number_of_meetings = 0; |
| |
|
| | chameos *other_critter; |
| |
|
| | int met_others = 0; |
| | int met_self = 0; |
| | int *total_meetings = 0; |
| |
|
| | while(done != 1) { |
| | other_critter = meeting(critter); |
| |
|
| | if(other_critter == 0) { |
| | break; |
| | } |
| |
|
| | if(critter.id == other_critter->id) { |
| | met_self++; |
| | }else{ |
| | met_others++; |
| | } |
| |
|
| | critter.my_color = compliment_color(critter.my_color, other_critter->my_color); |
| | free(other_critter); |
| | } |
| |
|
| | sem_wait(&sem_print); |
| | |
| | spell_the_number(met_others + met_self, met_self); |
| | |
| |
|
| | total_meetings = malloc(sizeof(int)); |
| | |
| |
|
| | pthread_exit((void *)total_meetings); |
| | } |
| |
|
| | void |
| | print_colors(void) { |
| | int i, j; |
| | color c; |
| |
|
| | for(i = 1; i < INVALID; i++) { |
| | for(j = 1; j < INVALID; j++) { |
| | c = compliment_color(i,j); |
| | printf(""%s + %s -> %s\\n"",colors[i],colors[j], colors[c]); |
| | } |
| | } |
| | printf(""\\n""); |
| | } |
| |
|
| |
|
| |
|
| |
|
| |
|
| | void |
| | run_the_meetings(color *starting_colors, int n_colors, int total_meetings_to_run) { |
| | struct sched_param priority; |
| | priority.sched_priority = 1; |
| |
|
| | pthread_t pid_tab[10]; |
| | memset(pid_tab, 0, sizeof(pthread_t)*10); |
| |
|
| | int i; |
| | int total = 0; |
| | void *rslt = 0; |
| |
|
| | sem_init(&at_most_two, 0, 2); |
| | sem_init(&mutex, 0, 1); |
| | sem_init(&sem_priv, 0, 0); |
| | sem_init(&sem_print, 0, 0); |
| |
|
| | pthread_mutex_init(&print_mutex, 0); |
| |
|
| | meetings_left = total_meetings_to_run; |
| | first_arrived = 0; |
| | done = 0; |
| |
|
| | sched_setscheduler(0, SCHED_FIFO, &priority); |
| |
|
| | for(i = 0; i < n_colors; i++) { |
| | printf("" %s"", colors[starting_colors[i]]); |
| | pthread_create(&pid_tab[i], 0, &creature, (void *)starting_colors[i]); |
| | } |
| | printf(""\\n""); |
| | for(i = 0; i < n_colors; i++) { |
| | sem_post(&sem_print); |
| | } |
| |
|
| | for(i = 0; i < n_colors; i++) { |
| | pthread_join(pid_tab[i], &rslt); |
| | total += *(int *)rslt; |
| | free(rslt); |
| | } |
| | spell_the_number(-1, total); |
| | printf(""\\n""); |
| | } |
| |
|
| | int |
| | main(int argc, char **argv) { |
| | color first_generation[3] = { BLUE, RED, YELLOW }; |
| | color second_generation[10] = {BLUE, RED, YELLOW, RED, YELLOW, |
| | BLUE, RED, YELLOW, RED, BLUE}; |
| | int number_of_meetings_to_run = 600; |
| |
|
| | if(argc > 1) { |
| | number_of_meetings_to_run = strtol(argv[1], 0, 10); |
| | } |
| |
|
| | print_colors(); |
| | run_the_meetings(first_generation, 3, number_of_meetings_to_run); |
| | run_the_meetings(second_generation, 10, number_of_meetings_to_run); |
| |
|
| | return 0; |
| | } |
| | ",1 |
| | " |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | #include <stdio.h> |
| | #include <stdlib.h> |
| | #include <pthread.h> |
| | #include <unistd.h> |
| | #include <semaphore.h> |
| | #include <time.h> |
| |
|
| | #define THREAD_NUM 4 |
| | #define BUFFER_SIZE 256 |
| |
|
| | typedef struct Task{ |
| | int a, b; |
| | }Task; |
| |
|
| | Task taskQueue[BUFFER_SIZE]; |
| | int taskCount = 0; |
| |
|
| | pthread_mutex_t mutex; |
| |
|
| | pthread_cond_t condFull; |
| | pthread_cond_t condEmpty; |
| |
|
| | void executeTask(Task* task, int id){ |
| | int result = task->a + task->b; |
| | printf(""(Thread %d) Sum of %d and %d is %d\\n"", id, task->a, task->b, result); |
| | } |
| |
|
| | Task getTask(){ |
| | pthread_mutex_lock(&mutex); |
| | |
| | while (taskCount == 0){ |
| | pthread_cond_wait(&condEmpty, &mutex); |
| | } |
| | |
| | Task task = taskQueue[0]; |
| | int i; |
| | for (i = 0; i < taskCount - 1; i++){ |
| | taskQueue[i] = taskQueue[i+1]; |
| | } |
| | taskCount--; |
| | |
| | pthread_mutex_unlock(&mutex); |
| | pthread_cond_signal(&condFull); |
| | return task; |
| | } |
| |
|
| | void submitTask(Task task){ |
| | pthread_mutex_lock(&mutex); |
| |
|
| | while (taskCount == BUFFER_SIZE){ |
| | pthread_cond_wait(&condFull, &mutex); |
| | } |
| |
|
| | taskQueue[taskCount] = task; |
| | taskCount++; |
| |
|
| | pthread_mutex_unlock(&mutex); |
| | pthread_cond_signal(&condEmpty); |
| | } |
| |
|
| | void *startThread(void* args); |
| |
|
| | |
| | int main(int argc, char* argv[]) { |
| | pthread_mutex_init(&mutex, NULL); |
| | |
| | pthread_cond_init(&condEmpty, NULL); |
| | pthread_cond_init(&condFull, NULL); |
| |
|
| | pthread_t thread[THREAD_NUM]; |
| | long i; |
| | for (i = 0; i < THREAD_NUM; i++){ |
| | if (pthread_create(&thread[i], NULL, &startThread, (void*) i) != 0) { |
| | perror(""Failed to create the thread""); |
| | } |
| | } |
| | |
| | srand(time(NULL)); |
| | for (i = 0; i < 500; i++){ |
| | Task t = { |
| | .a = rand() % 100, |
| | .b = rand() % 100 |
| | }; |
| | submitTask(t); |
| | } |
| | |
| | for (i = 0; i < THREAD_NUM; i++){ |
| | if (pthread_join(thread[i], NULL) != 0) { |
| | perror(""Failed to join the thread""); |
| | } |
| | } |
| | |
| | pthread_mutex_destroy(&mutex); |
| | pthread_cond_destroy(&condEmpty); |
| | pthread_cond_destroy(&condFull); |
| | return 0; |
| | } |
| |
|
| | |
| | void *startThread(void* args) { |
| | long id = (long) args; |
| | while (1) { |
| | Task task = getTask(); |
| | if (task.a == -1 && task.b == -1) { |
| | printf(""(Thread %ld) Terminating\\n"", id); |
| | break; |
| | } |
| | executeTask(&task, id); |
| | sleep(rand() % 5); |
| | } |
| | return NULL; |
| | } |
| | ",0 |
| | " |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #include <pthread.h> |
| | #include <stdio.h> |
| | #include <unistd.h> |
| | |
| | pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER; |
| | |
| | pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; |
| | |
| | int done = 1; |
| | |
| | void* execute() |
| | { |
| | |
| | if (done == 1) { |
| | done = 2; |
| | printf(""Esperando por cond1\\n""); |
| | pthread_cond_wait(&cond1, &lock); |
| | } |
| | else { |
| | printf(""Sinalizando cond1\\n""); |
| | |
| | pthread_cond_signal(&cond1); |
| | return NULL; |
| | } |
| |
|
| | |
| | printf(""Fim da thread\\n""); |
| |
|
| | return NULL; |
| | } |
| | |
| | int main() |
| | { |
| | pthread_t tid1, tid2; |
| | |
| | pthread_create(&tid1, NULL, execute, NULL); |
| | |
| | sleep(5); |
| | |
| | pthread_create(&tid2, NULL, execute, NULL); |
| | |
| | pthread_join(tid2, NULL); |
| | |
| | return 0; |
| | } |
| | ",1 |
| | "#include <pthread.h> |
| | #include <unistd.h> |
| | #include <stdio.h> |
| | #include<math.h> |
| | int tickets = 20; |
| | pthread_mutex_t mutex; |
| |
|
| | void *mythread1(void) |
| | { |
| | while (1) |
| | { |
| | pthread_mutex_lock(&mutex); |
| | if (tickets > 0) |
| | { |
| | usleep(1000); |
| | printf(""ticketse1 sells ticket:%d\\n"", tickets--); |
| | pthread_mutex_unlock(&mutex); |
| | } |
| | else |
| | { |
| | pthread_mutex_unlock(&mutex); |
| | break; |
| | } |
| | sleep(1); |
| | } |
| | return (void *)0; |
| | } |
| | void *mythread2(void) |
| | { |
| | while (1) |
| | { |
| | pthread_mutex_lock(&mutex); |
| | if (tickets > 0) |
| | { |
| | usleep(1000); |
| | printf(""ticketse2 sells ticket:%d\\n"", tickets--); |
| | pthread_mutex_unlock(&mutex); |
| | } |
| | else |
| | { |
| | pthread_mutex_unlock(&mutex); |
| | break; |
| | } |
| | sleep(1); |
| | } |
| | return (void *)0; |
| | } |
| |
|
| | int main(int argc, const char *argv[]) |
| | { |
| | |
| | int ret = 0; |
| | pthread_t id1, id2; |
| |
|
| | ret = pthread_create(&id1, NULL, (void *)mythread1, NULL); |
| | if (ret) |
| | { |
| | printf(""Create pthread error!\\n""); |
| | return 1; |
| | } |
| |
|
| | ret = pthread_create(&id2, NULL, (void *)mythread2, NULL); |
| | if (ret) |
| | { |
| | printf(""Create pthread error!\\n""); |
| | return 1; |
| | } |
| |
|
| | pthread_join(id1, NULL); |
| | pthread_join(id2, NULL); |
| |
|
| | return 0; |
| | } |
| | ",0 |
| | " |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | #include <stdio.h> |
| | #include <stdlib.h> |
| | #include <pthread.h> |
| | #include <unistd.h> |
| | #include <string.h> |
| | #include <time.h> |
| |
|
| |
|
| | int publico = 0; |
| |
|
| | pthread_mutex_t mutex; |
| |
|
| | void incPublico(){ |
| | |
| | publico++; |
| | |
| | } |
| |
|
| | void *execute() { |
| | int i; |
| |
|
| | for (i = 1; i <= 100000; i++){ |
| | incPublico(); |
| | } |
| | return NULL; |
| | } |
| |
|
| |
|
| | |
| | int main(int argc, char* argv[]) { |
| | pthread_t t1, t2, t3, t4; |
| | |
| | pthread_mutex_init(&mutex, NULL); |
| |
|
| | |
| | pthread_create(&t1, NULL, execute, NULL); |
| | pthread_create(&t2, NULL, execute, NULL); |
| | pthread_create(&t3, NULL, execute, NULL); |
| | pthread_create(&t4, NULL, execute, NULL); |
| | |
| | |
| | pthread_join(t1, NULL); |
| | pthread_join(t2, NULL); |
| | pthread_join(t3, NULL); |
| | pthread_join(t4, NULL); |
| |
|
| | printf(""Público final: %d\\n"", publico); |
| | pthread_mutex_destroy(&mutex); |
| |
|
| | return 0; |
| | } |
| |
|
| |
|
| | ",1 |
| | "#include <stdio.h> |
| | #include <err.h> |
| | #include <stdlib.h> |
| | #include <string.h> |
| | #include <pthread.h> |
| |
|
| | struct employee { |
| | int number; |
| | int id; |
| | char first_name[32]; |
| | char last_name[32]; |
| | char department[32]; |
| | int root_number; |
| | }; |
| |
|
| | |
| | struct employee employees[] = { |
| | { 1, 12345678, ""astro"", ""Bluse"", ""Accounting"", 101 }, |
| | { 2, 87654321, ""Shrek"", ""Charl"", ""Programmer"", 102 }, |
| | }; |
| |
|
| | |
| | struct employee employee_of_the_day; |
| |
|
| | void copy_employee(struct employee *from, struct employee *to) |
| | { |
| | memcpy(to, from, sizeof(struct employee)); |
| | } |
| |
|
| | pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; |
| |
|
| | void *do_loop(void *data) |
| | { |
| | int num = *(int *)data; |
| |
|
| | while (1) { |
| | pthread_mutex_lock(&lock); |
| | copy_employee(&employees[num - 1], &employee_of_the_day); |
| | pthread_mutex_unlock(&lock); |
| | } |
| | } |
| |
|
| |
|
| | int main(int argc, const char *argv[]) |
| | { |
| | pthread_t th1, th2; |
| | int num1 = 1; |
| | int num2 = 2; |
| | int i; |
| |
|
| | copy_employee(&employees[0], &employee_of_the_day); |
| |
|
| | if (pthread_create(&th1, NULL, do_loop, &num1)) { |
| | errx(EXIT_FAILURE, ""pthread_create() error.\\n""); |
| | } |
| | if (pthread_create(&th2, NULL, do_loop, &num2)) { |
| | errx(EXIT_FAILURE, ""pthread_create() error.\\n""); |
| | } |
| |
|
| | while (1) { |
| | pthread_mutex_lock(&lock); |
| | struct employee *p = &employees[employee_of_the_day.number - 1]; |
| |
|
| | if (p->id != employee_of_the_day.id) { |
| | printf(""mismatching 'id', %d != %d (loop '%d')\\n"", |
| | employee_of_the_day.id, p->id, i); |
| | exit(EXIT_FAILURE); |
| | } |
| |
|
| | if (strcmp(p->first_name, employee_of_the_day.first_name)) { |
| | printf(""mismatching 'first_name', %s != %s (loop '%d')\\n"", |
| | employee_of_the_day.first_name, p->first_name, i); |
| | exit(EXIT_FAILURE); |
| | } |
| |
|
| | if (strcmp(p->last_name, employee_of_the_day.last_name)) { |
| | printf(""mismatching 'last_name', %s != %s (loop '%d')\\n"", |
| | employee_of_the_day.last_name, p->last_name, i); |
| | exit(EXIT_FAILURE); |
| | } |
| |
|
| | if (strcmp(p->department, employee_of_the_day.department)) { |
| | printf(""mismatching 'department', %s != %s (loop '%d')\\n"", |
| | employee_of_the_day.department, p->department, i); |
| | exit(EXIT_FAILURE); |
| | } |
| |
|
| | if (p->root_number != employee_of_the_day.root_number) { |
| | printf(""mismatching 'root_number', %d != %d (loop '%d')\\n"", |
| | employee_of_the_day.root_number, p->root_number, i); |
| | exit(EXIT_FAILURE); |
| | } |
| | |
| | printf(""lory, employees contents was always consistent\\n""); |
| | pthread_mutex_unlock(&lock); |
| | } |
| | |
| | exit(EXIT_SUCCESS); |
| | } |
| | ",0 |
| | "#include <stdio.h> |
| | #include <stdlib.h> |
| | #include <pthread.h> |
| | #include <math.h> |
| |
|
| | #define NUM_THREADS 2 |
| |
|
| | |
| | float hypotenuse; |
| | pthread_mutex_t mutexsum; |
| |
|
| |
|
| | void *square_side (void *); |
| |
|
| |
|
| | int main (int argc, char **argv) |
| | { |
| | int i; |
| | float sides[2]; |
| | pthread_t *thr_ids; |
| |
|
| | switch (argc) |
| | { |
| | case 3: |
| | |
| | sides[0] = atof (argv[1]); |
| | sides[1] = atof (argv[2]); |
| | if ((sides[0] < 1) || (sides[1] < 1)) |
| | { |
| | fprintf (stderr, ""Error: wrong values for triangle sides.\\n"" |
| | ""Usage:\\n"" |
| | "" %s <side_a> <side_b>\\n"" |
| | ""values of sizes should be > 0\\n"", |
| | argv[0]); |
| | exit (EXIT_FAILURE); |
| | } |
| | break; |
| |
|
| | default: |
| | fprintf (stderr, ""Error: wrong number of parameters.\\n"" |
| | ""Usage:\\n"" |
| | "" %s <side_a> <side_b>\\n"", |
| | argv[0]); |
| | exit (EXIT_FAILURE); |
| | } |
| | |
| | |
| | thr_ids = (pthread_t *) malloc (NUM_THREADS * sizeof (pthread_t)); |
| | |
| | |
| | if (thr_ids == NULL) |
| | { |
| | fprintf (stderr, ""File: %s, line %d: Can't allocate memory."", |
| | __FILE__, __LINE__); |
| | exit (EXIT_FAILURE); |
| | } |
| | |
| | printf (""\\nPythagoras' theorem | a^2 + b^2 = c^2 \\n""); |
| | hypotenuse = 0; |
| | |
| | |
| | pthread_mutex_init (&mutexsum, NULL); |
| | |
| | |
| | pthread_create (&thr_ids[0], NULL, square_side, &sides[0]); |
| | pthread_create (&thr_ids[1], NULL, square_side, &sides[1]); |
| | |
| | |
| | for (i = 0; i < NUM_THREADS; i++) |
| | { |
| | pthread_join (thr_ids[i], NULL); |
| | } |
| | |
| | printf (""Hypotenuse is %.2f\\n"", sqrt(hypotenuse)); |
| | |
| | |
| | pthread_mutex_destroy (&mutexsum); |
| | free (thr_ids); |
| | |
| | return EXIT_SUCCESS; |
| | } |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | void *square_side (void *arg) |
| | { |
| | float side; |
| |
|
| | |
| | side = *( ( float* )arg ); |
| | printf (""%.2f^2 = %.2f\\n"", side, side * side); |
| | |
| | |
| | pthread_mutex_lock (&mutexsum); |
| | hypotenuse += side * side; |
| | pthread_mutex_unlock (&mutexsum); |
| | |
| | pthread_exit (EXIT_SUCCESS); |
| | } |
| | ",1 |
| | "#include <stdio.h> |
| | #include <stdlib.h> |
| | #include <string.h> |
| | #include <pthread.h> |
| | #include <sys/time.h> |
| | #include <math.h> |
| |
|
| | #define GRID_SIZE 16384 |
| |
|
| | float** main_plate; |
| | float** main_prev_plate; |
| | char** main_locked_cells; |
| |
|
| | pthread_barrier_t barrier_first; |
| | pthread_barrier_t barrier_second; |
| | pthread_mutex_t critical_begin_end; |
| | pthread_mutex_t runnable; |
| |
|
| | typedef struct arg_plate { |
| | int nthreads; |
| | int begin; |
| | int end; |
| | } arg_plate_t; |
| |
|
| | double when() { |
| | struct timeval tp; |
| | gettimeofday(&tp, 0); |
| | return ((double)tp.tv_sec + (double)tp.tv_usec * 1e-6); |
| | } |
| |
|
| |
|
| | float** createPlate() { |
| | float** plate = (float**)malloc(16384 * sizeof(float*)); |
| | int k; |
| | for (k = 0; k < 16384; k++) { |
| | plate[k] = (float*)malloc(16384 * sizeof(float)); |
| | } |
| | return plate; |
| | } |
| | char** createCharPlate() { |
| | char** plate = (char**)malloc(16384 * sizeof(char*)); |
| | int k; |
| | for (k = 0; k < 16384; k++) { |
| | plate[k] = (char*)malloc(16384 * sizeof(char)); |
| | } |
| | return plate; |
| | } |
| | void copy(float** main, float** result) { |
| | int i,j; |
| | for (i = 0; i < 16384; i++) { |
| | for (j = 0; j < 16384; j++) { |
| |
|
| | result[i][j] = main[i][j]; |
| | } |
| | } |
| | } |
| |
|
| | void initPlate(float** plate, float** prev_plate) { |
| | int i, j; |
| | for (i = 0; i < 16384; i++) { |
| | for (j = 0; j < 16384; j++) { |
| | if (i == 0 || j == 0 || j == 16384 -1) { |
| | plate[i][j] = 0; |
| | prev_plate[i][j] = 0; |
| | main_locked_cells[i][j] = '1'; |
| | } |
| | else if (i == 16384 -1) { |
| | plate[i][j] = 100; |
| | prev_plate[i][j] = 100; |
| | main_locked_cells[i][j] = '1'; |
| |
|
| | } |
| | else if (i == 400 && j >= 0 && j <= 330) { |
| | plate[i][j] = 100; |
| | prev_plate[i][j] = 100; |
| | main_locked_cells[i][j] = '1'; |
| | } |
| | else if (i == 200 && j == 500) { |
| | plate[i][j] = 100; |
| | prev_plate[i][j] = 100; |
| | main_locked_cells[i][j] = '1'; |
| | } |
| | else { |
| | plate[i][j] = 50; |
| | prev_plate[i][j] = 50; |
| | main_locked_cells[i][j] = '0'; |
| | } |
| | } |
| | } |
| | for (i = 0; i < 16384; i++) { |
| | if ((i % 20) == 0) { |
| | for (j = 0; j < 16384; j++) { |
| | plate[i][j] = 100; |
| | prev_plate[i][j] = 100; |
| | main_locked_cells[i][j] = '1'; |
| | } |
| | } |
| | } |
| | for (j = 0; j < 16384; j++) { |
| | if ((j % 20) == 0) { |
| | for (i = 0; i < 16384; i++) { |
| | plate[i][j] = 0; |
| | prev_plate[i][j] = 0; |
| | main_locked_cells[i][j] = '1'; |
| | } |
| | } |
| | } |
| | } |
| |
|
| | void cleanupFloat(float** plate) { |
| | int i, j; |
| | for (i = 0; i < 16384; i++) { |
| | free(plate[i]); |
| | } |
| | free(plate); |
| | } |
| | void cleanupChar(char** plate) { |
| | int i, j; |
| | for (i = 0; i < 16384; i++) { |
| | free(plate[i]); |
| | } |
| | free(plate); |
| | } |
| |
|
| | void* update_plate(void* plate_arguments) { |
| | for(;;) { |
| | pthread_barrier_wait(&barrier_first); |
| |
|
| | arg_plate_t* plate_args = (arg_plate_t*)plate_arguments; |
| |
|
| | pthread_mutex_lock(&runnable); |
| | int begin = plate_args->begin; |
| | int end = plate_args->end; |
| | pthread_mutex_unlock(&runnable); |
| |
|
| |
|
| | int i, j; |
| | for (i = begin; i < end; i++) { |
| | for (j = 0; j < 16384; j++) { |
| | if (main_locked_cells[i][j] == '0') { |
| | main_plate[i][j] = (main_prev_plate[i+1][j] + main_prev_plate[i][j+1] + main_prev_plate[i-1][j] |
| | + main_prev_plate[i][j-1] + 4 * main_prev_plate[i][j]) * 0.125; |
| | } |
| | } |
| | } |
| |
|
| | pthread_barrier_wait(&barrier_second); |
| | } |
| | } |
| | char steady(float** current_plate) { |
| | int count = 0; |
| | int i, j; |
| | float main_diff = 0; |
| | for (i = 0; i < 16384; i++) { |
| | for (j = 0; j < 16384; j++) { |
| | if (main_locked_cells[i][j] == '0') { |
| | if (current_plate[i][j] > 50) |
| | count++; |
| |
|
| | float diff = fabs(current_plate[i][j] - (current_plate[i+1][j] + current_plate[i-1][j] |
| | + current_plate[i][j+1] + current_plate[i][j-1]) * 0.25); |
| | if (diff > main_diff) |
| | main_diff = diff; |
| | } |
| | } |
| | } |
| |
|
| | if (main_diff > 0.1) |
| | return (1); |
| | else |
| | return (0); |
| | } |
| |
|
| | void allocateWorkload(int nthreads, int* begin_end) { |
| | int step = 16384 / nthreads; |
| | int i; |
| | int begin = 0; |
| | for (i = 0; i < nthreads*2; i++) { |
| | begin_end[i] = begin; |
| |
|
| | begin = begin+step; |
| | i += 1; |
| |
|
| | begin_end[i] = begin; |
| | } |
| | } |
| |
|
| | int startUpdate(pthread_t* threads, int nthreads) { |
| | printf(""Updating plate to steady state\\n""); |
| |
|
| | int iterations = 0; |
| | int* begin_end = (int*)malloc((nthreads*2) * sizeof(int)); |
| | allocateWorkload(nthreads, begin_end); |
| |
|
| | int i; |
| | int j; |
| | pthread_t worker[nthreads]; |
| | arg_plate_t* plate_args; |
| | for (i = 0; i < nthreads; i++) { |
| | pthread_mutex_lock(&critical_begin_end); |
| |
|
| | plate_args = (arg_plate_t*)malloc(sizeof(arg_plate_t)); |
| | j = i * 2; |
| | plate_args->begin = begin_end[j]; |
| | plate_args->end = begin_end[j+1]; |
| |
|
| | pthread_create(&worker[i], 0, &update_plate, (void*)plate_args); |
| |
|
| |
|
| | pthread_mutex_unlock(&critical_begin_end); |
| | } |
| | do { |
| | iterations++; |
| | printf(""Iteration: %d\\n"", iterations); |
| |
|
| | pthread_barrier_wait(&barrier_first); |
| | pthread_barrier_wait(&barrier_second); |
| |
|
| | copy(main_plate, main_prev_plate); |
| |
|
| | } while(steady(main_plate)); |
| | return iterations; |
| | } |
| |
|
| |
|
| | int main(int argc, char* argv[]) { |
| |
|
| | double start = when(); |
| | printf(""Starting time: %f\\n"", start); |
| |
|
| |
|
| | int nthreads = atoi(argv[1]); |
| | pthread_t threads[nthreads]; |
| |
|
| |
|
| | main_plate = createPlate(); |
| | main_prev_plate = createPlate(); |
| | main_locked_cells = createCharPlate(); |
| |
|
| |
|
| | initPlate(main_plate, main_prev_plate); |
| |
|
| |
|
| | pthread_barrier_init(&barrier_first,0,nthreads+1); |
| | pthread_barrier_init(&barrier_second,0,nthreads+1); |
| | pthread_mutex_init(&critical_begin_end,0); |
| | pthread_mutex_init(&runnable,0); |
| |
|
| |
|
| | int iterations = startUpdate(threads, nthreads); |
| |
|
| |
|
| | double end = when(); |
| | printf(""\\nEnding time: %f\\n"", end); |
| | printf(""Total execution time: %f\\n"", end - start); |
| | printf(""Number of iterations: %d\\n\\n"", iterations); |
| |
|
| |
|
| | pthread_barrier_destroy(&barrier_first); |
| | pthread_barrier_destroy(&barrier_second); |
| | pthread_mutex_destroy(&critical_begin_end); |
| | pthread_mutex_destroy(&runnable); |
| |
|
| |
|
| | printf(""Cleanup\\n""); |
| | cleanupFloat(main_plate); |
| | cleanupFloat(main_prev_plate); |
| | cleanupChar(main_locked_cells); |
| |
|
| | return 0; |
| | } |
| | ",0 |
| | "#define _GNU_SOURCE |
| | #include <assert.h> |
| | #include <pthread.h> |
| | #include <stdio.h> |
| | #include <stdlib.h> |
| | #include <unistd.h> |
| |
|
| | pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; |
| |
|
| | void show_stack(pthread_attr_t *attr, pthread_t thread, char *prefix) { |
| | size_t stack_size, guard_size; |
| | void *stack_addr; |
| | int rc; |
| |
|
| | rc = pthread_attr_getguardsize(attr, &guard_size); |
| | assert(rc == 0); |
| |
|
| | rc = pthread_attr_getstack(attr, &stack_addr, &stack_size); |
| | assert(rc == 0); |
| |
|
| | printf(""Thread %s (id=%lu) stack:\\n"", prefix, thread); |
| | printf(""\\tstart address\\t= %p\\n"", stack_addr); |
| | printf(""\\tend address\\t= %p\\n"", stack_addr + stack_size); |
| | printf(""\\tstack size\\t= %.2f MB\\n"", stack_size / 1024.0 / 1024.0); |
| | printf(""\\tguard size\\t= %lu B\\n"", guard_size); |
| | } |
| |
|
| | void *entry_point(void *arg) { |
| | pthread_t thread = pthread_self(); |
| |
|
| | int rc; |
| | pthread_attr_t attr; |
| | rc = pthread_getattr_np(thread, &attr); |
| | assert(rc == 0); |
| |
|
| | |
| | show_stack(&attr, thread, (char *)arg); |
| | |
| |
|
| | return NULL; |
| | } |
| |
|
| | int main(int argc, char *argv[]) { |
| | pthread_t p1, p2; |
| | int rc; |
| |
|
| | rc = pthread_create(&p1, NULL, entry_point, ""1""); |
| | assert(rc == 0); |
| | rc = pthread_create(&p2, NULL, entry_point, ""2""); |
| | assert(rc == 0); |
| |
|
| | entry_point(""main""); |
| |
|
| | rc = pthread_join(p1, NULL); |
| | assert(rc == 0); |
| | rc = pthread_join(p2, NULL); |
| | assert(rc == 0); |
| |
|
| | return 0; |
| | } |
| | ",1 |
| | "#include <stdio.h> |
| | #include <stdlib.h> |
| | #include <string.h> |
| | #include <pthread.h> |
| |
|
| | enum { |
| | QUEUE_BUF_SIZE = 100000, |
| | }; |
| |
|
| | typedef struct vector_s { |
| | int size; |
| | int tail; |
| | void **buf; |
| | } vector_t; |
| |
|
| | typedef struct queue_cycl_s { |
| | int front; |
| | int tail; |
| | int max_size; |
| | void **cyclic_buf; |
| | } queue_cycl_t; |
| |
|
| | vector_t *vector_init(int size) { |
| | vector_t *tmp = (vector_t *)calloc(1, sizeof(vector_t)); |
| | if (tmp == NULL) { |
| | fprintf(stderr, ""vector_init error\\n""); |
| | exit(1); |
| | } |
| |
|
| | tmp->buf = (void **)calloc(size, sizeof(void *)); |
| | if (tmp->buf == NULL) { |
| | fprintf(stderr, ""vector_init error\\n""); |
| | free(tmp); |
| | exit(1); |
| | } |
| |
|
| | tmp->size = size; |
| | tmp->tail = 0; |
| |
|
| | return tmp; |
| | } |
| |
|
| | int vector_push_back(vector_t *v, void *data) { |
| | if (v->tail == v->size) |
| | return 0; |
| |
|
| | v->buf[v->tail++] = data; |
| |
|
| | return 1; |
| | } |
| |
|
| | void *vector_pop_back(vector_t *v) { |
| | return (v->tail == 0) ? NULL : v->buf[--(v->tail)]; |
| | } |
| |
|
| | void vector_delete(vector_t *v) { |
| | free(v->buf); |
| | free(v); |
| | } |
| |
|
| | queue_cycl_t *queue_init() { |
| | queue_cycl_t *tmp = (queue_cycl_t *)calloc(1, sizeof(queue_cycl_t)); |
| | if (tmp == NULL) { |
| | fprintf(stderr, ""queue_init error\\n""); |
| | exit(1); |
| | } |
| |
|
| | tmp->max_size = QUEUE_BUF_SIZE + 1; |
| | tmp->cyclic_buf = (void **)calloc(tmp->max_size, sizeof(void *)); |
| | if (tmp->cyclic_buf == NULL) { |
| | fprintf(stderr, ""queue_init error\\n""); |
| | free(tmp); |
| | exit(1); |
| | } |
| |
|
| | return tmp; |
| | } |
| |
|
| | int queue_size(queue_cycl_t *q) { |
| | return (q->tail >= q->front) ? (q->tail - q->front) : (q->max_size - q->front + q->tail); |
| | } |
| |
|
| | int queue_is_full(queue_cycl_t *q) { |
| | return ((q->tail + 1) % q->max_size == q->front); |
| | } |
| |
|
| | int queue_is_empty(queue_cycl_t *q) { |
| | return (q->front == q->tail); |
| | } |
| |
|
| | int queue_enqueue(queue_cycl_t *q, void *data) { |
| | if (queue_is_full(q)) |
| | return 0; |
| |
|
| | q->cyclic_buf[q->tail] = data; |
| | q->tail = (q->tail + 1) % q->max_size; |
| |
|
| | return 1; |
| | } |
| |
|
| | void *queue_dequeue(queue_cycl_t *q) { |
| | if (queue_is_empty(q)) |
| | return NULL; |
| | void *tmp = q->cyclic_buf[q->front]; |
| | q->cyclic_buf[q->front] = NULL; |
| | q->front = (q->front + 1) % q->max_size; |
| |
|
| | return tmp; |
| | } |
| |
|
| | vector_t *queue_dequeueall(queue_cycl_t *q) { |
| | int s = queue_size(q); |
| | vector_t *tmp = vector_init(s); |
| | void *data = NULL; |
| |
|
| | while ((data = queue_dequeue(q)) != NULL) { |
| | if (!vector_push_back(tmp, data)) { |
| | queue_enqueue(q, data); |
| | fprintf(stderr, ""queue_dequeueall error\\n""); |
| | exit(1); |
| | } |
| | } |
| |
|
| | return tmp; |
| | } |
| |
|
| | void queue_delete(queue_cycl_t *q) { |
| | free(q->cyclic_buf); |
| | free(q); |
| | } |
| |
|
| | typedef struct actor_s { |
| | pthread_t thread; |
| | pthread_mutex_t m; |
| | pthread_cond_t cond; |
| | queue_cycl_t *q; |
| | } actor_t; |
| |
|
| | void actor_send_to(actor_t *a, void *msg) { |
| | pthread_mutex_lock(&a->m); |
| | queue_enqueue(a->q, msg); |
| | pthread_cond_signal(&a->cond); |
| | pthread_mutex_unlock(&a->m); |
| | } |
| |
|
| | void *actor_runner(void *arg) { |
| | actor_t *iam = (actor_t *)arg; |
| | int buf[50] = {0}; |
| |
|
| | while (1) { |
| | pthread_mutex_lock(&iam->m); |
| | while (queue_is_empty(iam->q)) { |
| | pthread_cond_wait(&iam->cond, &iam->m); |
| | } |
| | vector_t *v = queue_dequeueall(iam->q); |
| | pthread_mutex_unlock(&iam->m); |
| |
|
| | int *data = NULL, exit_flag = 0; |
| | while ((data = vector_pop_back(v)) != NULL) { |
| | if (*data == -1) { |
| | exit_flag = 1; |
| | } else { |
| | buf[*data]++; |
| | } |
| | free(data); |
| | } |
| | vector_delete(v); |
| | if (exit_flag) break; |
| | } |
| |
|
| | for (int i = 0; i < 50; i++) { |
| | if (buf[i] != n_senders) { |
| | fprintf(stderr, ""ERROR!!!!!!!!\\n""); |
| | } |
| | } |
| |
|
| | return NULL; |
| | } |
| |
|
| | actor_t *actor_init() { |
| | actor_t *tmp = (actor_t *)calloc(1, sizeof(actor_t)); |
| | if (tmp == NULL) { |
| | fprintf(stderr, ""actor_init error\\n""); |
| | exit(1); |
| | } |
| |
|
| | pthread_mutex_init(&tmp->m, NULL); |
| | pthread_cond_init(&tmp->cond, NULL); |
| | tmp->q = queue_init(); |
| | pthread_create(&tmp->thread, NULL, actor_runner, tmp); |
| |
|
| | return tmp; |
| | } |
| |
|
| | void actor_finalize(actor_t *a) { |
| | pthread_join(a->thread, NULL); |
| | queue_delete(a->q); |
| | pthread_mutex_destroy(&a->m); |
| | pthread_cond_destroy(&a->cond); |
| | free(a); |
| | } |
| |
|
| | void *sender(void *arg) { |
| | actor_t *receiver = (actor_t *)arg; |
| | int *msg = NULL; |
| |
|
| | for (int i = 0; i < 50; i++) { |
| | msg = (int *)calloc(1, sizeof(int)); |
| | if (msg == NULL) { |
| | fprintf(stderr, ""Memory allocation failed\\n""); |
| | exit(1); |
| | } |
| | |
| | actor_send_to(receiver, msg); |
| | } |
| |
|
| | return NULL; |
| | } |
| |
|
| | int main(int argc, char **argv) { |
| | if (argc != 2) { |
| | fprintf(stderr, ""Usage: %s <number_of_senders>\\n"", argv[0]); |
| | exit(1); |
| | } |
| |
|
| | n_senders = atoi(argv[1]); |
| | pthread_t *senders_id = (pthread_t *)calloc(n_senders, sizeof(pthread_t)); |
| | if (senders_id == NULL) { |
| | fprintf(stderr, ""Memory allocation failed\\n""); |
| | exit(1); |
| | } |
| |
|
| | actor_t *actor = actor_init(); |
| | for (int i = 0; i < n_senders; i++) { |
| | pthread_create(&senders_id[i], NULL, sender, actor); |
| | } |
| |
|
| | for (int i = 0; i < n_senders; i++) { |
| | pthread_join(senders_id[i], NULL); |
| | } |
| |
|
| | int *msg_ext = (int *)calloc(1, sizeof(int)); |
| | |
| | actor_send_to(actor, msg_ext); |
| | actor_finalize(actor); |
| |
|
| | free(senders_id); |
| | return 0; |
| | } |
| |
|
| | ",0 |
| | "#include <stdio.h> |
| | #include <pthread.h> |
| | #include <assert.h> |
| |
|
| | static volatile int counter = 0; |
| | pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; |
| |
|
| | void *entry_point(void *arg) { |
| | printf(""Thread %s: begin\\n"", (char *)arg); |
| | |
| | for (int i = 0; i < 1e7; ++i) { |
| | counter += 1; |
| | } |
| | |
| | printf(""Thread %s: done\\n"", (char *)arg); |
| | return NULL; |
| | } |
| |
|
| | int main(int argc, char *argv[]) { |
| | pthread_t p1, p2; |
| | printf(""main: begin with counter = %d\\n"", counter); |
| | int rc; |
| | rc = pthread_create(&p1, NULL, entry_point, (void *)""A""); |
| | assert(rc == 0); |
| | rc = pthread_create(&p2, NULL, entry_point, (void *)""B""); |
| | assert(rc == 0); |
| |
|
| | |
| | rc = pthread_join(p1, NULL); |
| | assert(rc == 0); |
| | rc = pthread_join(p2, NULL); |
| | assert(rc == 0); |
| | printf(""main: done with counter = %d\\n"", counter); |
| | return 0; |
| | } |
| | ",1 |
| | "#include <stdio.h> |
| | #include <stdlib.h> |
| | #include <string.h> |
| | #include <pthread.h> |
| | #include <time.h> |
| | #include <stdbool.h> |
| |
|
| |
|
| | struct mode_extra_status |
| | { |
| | int output_pipe_fd; |
| | pthread_mutex_t mutex; |
| | union switch_data answer; |
| | long long rest_time[SWITCH_BUTTON_NUM + 1]; |
| | int score; |
| | pthread_t background_worker; |
| | bool terminated; |
| | }; |
| |
|
| | static void *background_worker_main (void *arg); |
| |
|
| | struct mode_extra_status *mode_extra_construct (int output_pipe_fd) |
| | { |
| | struct mode_extra_status *status; |
| | status = malloc (sizeof (*status)); |
| |
|
| | status->output_pipe_fd = output_pipe_fd; |
| | pthread_mutex_init (&status->mutex, 0); |
| | status->answer.val = 0; |
| | status->score = 0; |
| | status->terminated = 0; |
| |
|
| | pthread_create (&status->background_worker, 0, &background_worker_main, status); |
| |
|
| | return status; |
| | } |
| |
|
| | void mode_extra_destroy (struct mode_extra_status *status) |
| | { |
| | atomic_store_bool (&status->terminated, 1); |
| | pthread_join (status->background_worker, 0); |
| | pthread_mutex_destroy (&status->mutex); |
| | free (status); |
| | } |
| |
|
| | int mode_extra_switch (struct mode_extra_status *status, union switch_data data) |
| | { |
| | pthread_mutex_lock (&status->mutex); |
| |
|
| | int16_t correct = status->answer.val & data.val; |
| | int16_t incorrect = (status->answer.val ^ data.val) & data.val; |
| |
|
| | LOG (LOGGING_LEVEL_HIGH, ""[Main Process] correct = %03X, incorrect = %03X."", correct, incorrect); |
| |
|
| | status->answer.val ^= correct; |
| |
|
| | for (int i = 0; i < SWITCH_BUTTON_NUM; ++i) |
| | { |
| | status->score += (correct & 1); |
| | status->score -= (incorrect & 1); |
| | correct >>= 1; |
| | incorrect >>= 1; |
| | } |
| | if (status->score < 0) |
| | status->score = 0; |
| |
|
| | output_message_fnd_send (status->output_pipe_fd, status->score); |
| |
|
| | pthread_mutex_unlock (&status->mutex); |
| |
|
| | return 0; |
| | } |
| |
|
| | static void *background_worker_main (void *arg) |
| | { |
| | struct mode_extra_status *status = arg; |
| |
|
| | long long prev_nano_time = get_nano_time (); |
| | long long prev_creation_time = 0; |
| |
|
| | while (!atomic_load_bool (&status->terminated)) |
| | { |
| | pthread_mutex_lock (&status->mutex); |
| |
|
| | long long cur_nano_time = get_nano_time (); |
| | long long time_gap = cur_nano_time - prev_nano_time; |
| | prev_nano_time = cur_nano_time; |
| | # 110 ""mode_extra.c"" |
| | if (status->answer.bit_fields.s1) { status->rest_time[ 1 ] -= time_gap; if (status->rest_time[ 1 ] < 0) { status->answer.bit_fields.s1 = 0; if (status->score > 0) --status->score; } }; |
| | if (status->answer.bit_fields.s2) { status->rest_time[ 2 ] -= time_gap; if (status->rest_time[ 2 ] < 0) { status->answer.bit_fields.s2 = 0; if (status->score > 0) --status->score; } }; |
| | if (status->answer.bit_fields.s3) { status->rest_time[ 3 ] -= time_gap; if (status->rest_time[ 3 ] < 0) { status->answer.bit_fields.s3 = 0; if (status->score > 0) --status->score; } }; |
| | if (status->answer.bit_fields.s4) { status->rest_time[ 4 ] -= time_gap; if (status->rest_time[ 4 ] < 0) { status->answer.bit_fields.s4 = 0; if (status->score > 0) --status->score; } }; |
| | if (status->answer.bit_fields.s5) { status->rest_time[ 5 ] -= time_gap; if (status->rest_time[ 5 ] < 0) { status->answer.bit_fields.s5 = 0; if (status->score > 0) --status->score; } }; |
| | if (status->answer.bit_fields.s6) { status->rest_time[ 6 ] -= time_gap; if (status->rest_time[ 6 ] < 0) { status->answer.bit_fields.s6 = 0; if (status->score > 0) --status->score; } }; |
| | if (status->answer.bit_fields.s7) { status->rest_time[ 7 ] -= time_gap; if (status->rest_time[ 7 ] < 0) { status->answer.bit_fields.s7 = 0; if (status->score > 0) --status->score; } }; |
| | if (status->answer.bit_fields.s8) { status->rest_time[ 8 ] -= time_gap; if (status->rest_time[ 8 ] < 0) { status->answer.bit_fields.s8 = 0; if (status->score > 0) --status->score; } }; |
| | if (status->answer.bit_fields.s9) { status->rest_time[ 9 ] -= time_gap; if (status->rest_time[ 9 ] < 0) { status->answer.bit_fields.s9 = 0; if (status->score > 0) --status->score; } }; |
| |
|
| |
|
| |
|
| | long long delay_base = 300LL * 1000LL * 1000LL; |
| | long long micro_delay = (rand () % 10 + 1) * delay_base; |
| | double delay_coef = 5 / ((status->score + 10) / 20.0); |
| |
|
| | if (cur_nano_time - prev_creation_time > delay_coef * delay_base) |
| | { |
| | int no = 1 + rand () % SWITCH_BUTTON_NUM; |
| | status->rest_time[no] = micro_delay; |
| | status->answer.val |= (1 << (no-1)); |
| | prev_creation_time = cur_nano_time; |
| | } |
| |
|
| | output_message_fnd_send (status->output_pipe_fd, status->score); |
| |
|
| | struct dot_matrix_data dot_data = { { { 0, }, } }; |
| | int16_t bit_mask = 1; |
| | const int x_jump = 2, x_size = 3; |
| | const int y_jump = 3, y_size = 4; |
| | for (int i = 0; i < 3; ++i) |
| | { |
| | for (int j = 0; j < 3; ++j) |
| | { |
| | if (status->answer.val & bit_mask) |
| | { |
| | int base_y = i * y_jump; |
| | int base_x = j * x_jump; |
| | for (int y = base_y; y < base_y + y_size; ++y) |
| | for (int x = base_x; x < base_x + x_size; ++x) |
| | dot_data.data[y][x] = 1; |
| | } |
| | bit_mask <<= 1; |
| | } |
| | } |
| | output_message_dot_matrix_send (status->output_pipe_fd, &dot_data); |
| |
|
| | pthread_mutex_unlock (&status->mutex); |
| |
|
| | usleep ((10*1000)); |
| | } |
| |
|
| | return 0; |
| | } |
| | ",0 |
| | "#include <stdio.h> |
| | #include <stdlib.h> |
| | #include <string.h> |
| | #include <pthread.h> |
| | #include <assert.h> |
| | #include <sys/time.h> |
| |
|
| | #define NUM_BUCKETS 5 |
| | #define NUM_KEYS 100000 |
| | int num_threads = 1; |
| | int keys[NUM_KEYS]; |
| | pthread_mutex_t lock[NUM_BUCKETS]; |
| |
|
| | typedef struct _bucket_entry { |
| | int key; |
| | int val; |
| | struct _bucket_entry *next; |
| | } bucket_entry; |
| |
|
| | bucket_entry *table[NUM_BUCKETS]; |
| |
|
| | void panic(char *msg) { |
| | printf(""%s\\n"", msg); |
| | exit(1); |
| | } |
| |
|
| | double now() { |
| | struct timeval tv; |
| | gettimeofday(&tv, 0); |
| | return tv.tv_sec + tv.tv_usec / 1000000.0; |
| | } |
| |
|
| | |
| | void insert(int key, int val) { |
| | int i = key % NUM_BUCKETS; |
| | |
| | bucket_entry *e = (bucket_entry *) malloc(sizeof(bucket_entry)); |
| | if (!e) panic(""No memory to allocate bucket!""); |
| | pthread_mutex_lock(&lock[i]); |
| | e->next = table[i]; |
| | e->key = key; |
| | e->val = val; |
| | table[i] = e; |
| | |
| | pthread_mutex_unlock(&lock[i]); |
| | } |
| |
|
| | |
| | |
| | bucket_entry * retrieve(int key) { |
| | bucket_entry *b; |
| | for (b = table[key % NUM_BUCKETS]; b != NULL; b = b->next) { |
| | if (b->key == key) return b; |
| | } |
| | return NULL; |
| | } |
| |
|
| | void * put_phase(void *arg) { |
| | long tid = (long) arg; |
| | int key = 0; |
| |
|
| | |
| | |
| | for (key = tid ; key < NUM_KEYS; key += num_threads) { |
| | insert(keys[key], tid); |
| | } |
| |
|
| | pthread_exit(NULL); |
| | } |
| |
|
| | void * get_phase(void *arg) { |
| | long tid = (long) arg; |
| | int key = 0; |
| | long lost = 0; |
| |
|
| | for (key = tid ; key < NUM_KEYS; key += num_threads) { |
| | if (retrieve(keys[key]) == NULL) lost++; |
| | } |
| | printf(""[thread %ld] %ld keys lost!\\n"", tid, lost); |
| |
|
| | pthread_exit((void *)lost); |
| | } |
| |
|
| | int main(int argc, char **argv) { |
| | long i; |
| | pthread_t *threads; |
| | double start, end; |
| |
|
| | if (argc != 2) { |
| | panic(""usage: ./parallel_hashtable <num_threads>""); |
| | } |
| | if ((num_threads = atoi(argv[1])) <= 0) { |
| | panic(""must enter a valid number of threads to run""); |
| | } |
| |
|
| | srandom(time(NULL)); |
| | for (i = 0; i < NUM_KEYS; i++) |
| | keys[i] = random(); |
| |
|
| | |
| | for (i = 0; i < NUM_BUCKETS; i ++) |
| | pthread_mutex_init(&lock[i], NULL); |
| | threads = (pthread_t *) malloc(sizeof(pthread_t)*num_threads); |
| | if (!threads) { |
| | panic(""out of memory allocating thread handles""); |
| | } |
| |
|
| | |
| | start = now(); |
| | for (i = 0; i < num_threads; i++) { |
| | pthread_create(&threads[i], NULL, put_phase, (void *)i); |
| | } |
| | |
| | |
| | for (i = 0; i < num_threads; i++) { |
| | pthread_join(threads[i], NULL); |
| | } |
| | end = now(); |
| | |
| | printf(""[main] Inserted %d keys in %f seconds\\n"", NUM_KEYS, end - start); |
| | |
| | |
| | memset(threads, 0, sizeof(pthread_t)*num_threads); |
| |
|
| | |
| | start = now(); |
| | for (i = 0; i < num_threads; i++) { |
| | pthread_create(&threads[i], NULL, get_phase, (void *)i); |
| | } |
| |
|
| | |
| | long total_lost = 0; |
| | long *lost_keys = (long *) malloc(sizeof(long) * num_threads); |
| | for (i = 0; i < num_threads; i++) { |
| | pthread_join(threads[i], (void **)&lost_keys[i]); |
| | total_lost += lost_keys[i]; |
| | } |
| | end = now(); |
| |
|
| | printf(""[main] Retrieved %ld/%d keys in %f seconds\\n"", NUM_KEYS - total_lost, NUM_KEYS, end - start); |
| |
|
| | return 0; |
| | } |
| |
|
| |
|
| | ",1 |
| | "#include <pthread.h> |
| | #include <iostream> |
| | #include <unistd.h> |
| | using namespace std; |
| |
|
| | #define NUM_THREADS 3 |
| | #define TCOUNT 10 |
| | #define COUNT_LIMIT 12 |
| |
|
| | int count1 = 0; |
| | pthread_mutex_t count_mutex; |
| | pthread_cond_t count_threshold_cv; |
| |
|
| | void *inc_count(void *t) { |
| | long my_id = (long)t; |
| |
|
| | for (int i = 0; i < TCOUNT; i++) { |
| | pthread_mutex_lock(&count_mutex); |
| | count1++; |
| |
|
| | if (count1 >= COUNT_LIMIT) { |
| | cout << ""inc_count(): thread "" << my_id << "", count = "" << count1 << "" Threshold reached. ""; |
| | pthread_cond_broadcast(&count_threshold_cv); |
| | cout << ""Just sent signal.\\n""; |
| | } |
| | cout << ""inc_count(): thread "" << my_id << "", count = "" << count1 << "", unlocking mutex\\n""; |
| | pthread_mutex_unlock(&count_mutex); |
| | usleep(100); |
| | } |
| | pthread_exit(NULL); |
| | } |
| |
|
| | void *watch_count(void *t) { |
| | long my_id = (long)t; |
| |
|
| | cout << ""Starting watch_count(): thread "" << my_id << ""\\n""; |
| | pthread_mutex_lock(&count_mutex); |
| | while (count1 < COUNT_LIMIT) { |
| | cout << ""watch_count(): thread "" << my_id << "" Count= "" << count1 << "". Going into wait...\\n""; |
| | pthread_cond_wait(&count_threshold_cv, &count_mutex); |
| | cout << ""watch_count(): thread "" << my_id << "" Condition signal received. Count= "" << count1 << ""\\n""; |
| | } |
| |
|
| | |
| | cout << ""watch_count(): thread "" << my_id << "" Updating the value of count...\\n""; |
| | count1 += 125; |
| | cout << ""watch_count(): thread "" << my_id << "" count now = "" << count1 << "".\\n""; |
| | |
| | cout << ""watch_count(): thread "" << my_id << "" Unlocking mutex.\\n""; |
| | pthread_mutex_unlock(&count_mutex); |
| | pthread_exit(NULL); |
| | } |
| |
|
| | int main(int argc, char *argv[]) { |
| | long t1 = 1, t2 = 2, t3 = 3; |
| | pthread_t threads[3]; |
| | pthread_attr_t attr; |
| |
|
| | pthread_mutex_init(&count_mutex, NULL); |
| | pthread_cond_init(&count_threshold_cv, NULL); |
| |
|
| | pthread_attr_init(&attr); |
| | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
| | pthread_create(&threads[0], &attr, watch_count, (void *)t1); |
| | pthread_create(&threads[1], &attr, inc_count, (void *)t2); |
| | pthread_create(&threads[2], &attr, inc_count, (void *)t3); |
| |
|
| | for (int i = 0; i < NUM_THREADS; i++) { |
| | pthread_join(threads[i], NULL); |
| | } |
| | cout << ""Main(): Waited and joined with "" << NUM_THREADS << "" threads. Final value of count = "" << count1 << "". Done.\\n""; |
| |
|
| | pthread_attr_destroy(&attr); |
| | pthread_mutex_destroy(&count_mutex); |
| | pthread_cond_destroy(&count_threshold_cv); |
| | pthread_exit(NULL); |
| | } |
| |
|
| | ",0 |
| | "#include <pthread.h> |
| | #include <iostream> |
| | #include <cstdlib> |
| | using namespace std; |
| |
|
| | #define THREADNUM 4 |
| | #define VECLEN 1000000 |
| |
|
| | struct DOT { |
| | int *a; |
| | int *b; |
| | long long int sum; |
| | long int veclen; |
| | }; |
| |
|
| | DOT data; |
| | pthread_t callThd[THREADNUM]; |
| | pthread_mutex_t mutexsum; |
| |
|
| | void *dotprod(void *arg) |
| | { |
| | long offset = reinterpret_cast<long>(arg); |
| | int start, end, len; |
| | long long int threadsum = 0; |
| | int *x, *y; |
| | |
| | len = data.veclen; |
| | start = offset * len; |
| | end = start + len; |
| | x = data.a; |
| | y = data.b; |
| |
|
| | |
| | for (int i = start; i < end; i++) { |
| | threadsum += static_cast<long long int>(x[i]) * y[i]; |
| | } |
| |
|
| | |
| | |
| | data.sum += threadsum; |
| | cout << ""Thread "" << offset << "" did "" << start << "" to "" << end |
| | << "": ThreadSum = "" << threadsum << "", global sum = "" << data.sum << ""\\n""; |
| | |
| |
|
| | pthread_exit(nullptr); |
| | } |
| |
|
| | int main(int argc, char *argv[]) |
| | { |
| | int i; |
| | int *a, *b; |
| | void *status; |
| | pthread_attr_t attr; |
| |
|
| | |
| | a = (int*) malloc(THREADNUM * VECLEN * sizeof(int)); |
| | b = (int*) malloc(THREADNUM * VECLEN * sizeof(int)); |
| |
|
| | if (a == nullptr || b == nullptr) { |
| | cerr << ""Error allocating memory for vectors.\\n""; |
| | exit(1); |
| | } |
| |
|
| | for (i = 0; i < VECLEN * THREADNUM; i++) { |
| | a[i] = rand() % 100; |
| | b[i] = rand() % 100; |
| | } |
| |
|
| | data.veclen = VECLEN; |
| | data.a = a; |
| | data.b = b; |
| | data.sum = 0; |
| |
|
| | pthread_mutex_init(&mutexsum, nullptr); |
| | pthread_attr_init(&attr); |
| | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
| |
|
| | for (i = 0; i < THREADNUM; i++) { |
| | pthread_create(&callThd[i], &attr, dotprod, reinterpret_cast<void*>(i)); |
| | } |
| |
|
| | pthread_attr_destroy(&attr); |
| |
|
| | |
| | for (i = 0; i < THREADNUM; i++) { |
| | pthread_join(callThd[i], &status); |
| | } |
| |
|
| | cout << ""Sum = "" << data.sum << "" \\n""; |
| |
|
| | free(a); |
| | free(b); |
| | pthread_mutex_destroy(&mutexsum); |
| | pthread_exit(nullptr); |
| | } |
| |
|
| | ",1 |
| | "#include ""helper.h"" |
| |
|
| | |
| | |
| | |
| |
|
| | void pclock(char *msg, clockid_t cid) { |
| | struct timespec ts; |
| | printf(""%s"", msg); |
| | if (clock_gettime(cid, &ts) == -1) { |
| | perror(""clock_gettime""); |
| | return; |
| | } |
| | printf(""%4ld.%03ld\\n"", ts.tv_sec, ts.tv_nsec / 1000000); |
| | } |
| |
|
| | void errp(char *s, int code) { |
| | fprintf(stderr, ""Error: %s -- %s\\n"", s, strerror(code)); |
| | } |
| |
|
| | void thr_sleep(time_t sec, long nsec) { |
| | struct timeval now; |
| | struct timezone tz; |
| | struct timespec ts; |
| | int retcode; |
| |
|
| | pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; |
| | pthread_cond_t cond = PTHREAD_COND_INITIALIZER; |
| |
|
| | retcode = pthread_mutex_lock(&m); |
| | if(retcode) { |
| | fprintf(stderr, ""Error: mutex_lock -- %s\\n"", strerror(retcode)); |
| | return; |
| | } |
| |
|
| | gettimeofday(&now, &tz); |
| | ts.tv_sec = now.tv_sec + sec + (nsec / 1000000000L); |
| | ts.tv_nsec = (now.tv_usec * 1000) + (nsec % 1000000000L); |
| | if(ts.tv_nsec > 1000000000L) { |
| | (ts.tv_sec)++; |
| | (ts.tv_nsec) -= 1000000000L; |
| | } |
| |
|
| | retcode = pthread_cond_timedwait(&cond, &m, &ts); |
| | if(retcode != ETIMEDOUT) { |
| | if(retcode == 0) { |
| | fprintf(stderr, ""pthread_cond_timedwait returned early.\\n""); |
| | } else { |
| | fprintf(stderr, ""pthread_cond_timedwait error: %s\\n"", strerror(retcode)); |
| | return; |
| | } |
| | } |
| |
|
| | retcode = pthread_mutex_unlock(&m); |
| | if(retcode) { |
| | fprintf(stderr, ""Error: mutex_unlock -- %s\\n"", strerror(retcode)); |
| | return; |
| | } |
| |
|
| | pthread_cond_destroy(&cond); |
| | pthread_mutex_destroy(&m); |
| | } |
| |
|
| | void mulock(int ul, pthread_mutex_t *m) { |
| | int retcode = 0; |
| | char myErrStr[100]; |
| |
|
| | if (ul) { |
| | strcpy(myErrStr, ""mutex_unlock""); |
| | retcode = pthread_mutex_unlock(m); |
| | } else { |
| | strcpy(myErrStr, ""mutex_lock""); |
| | retcode = pthread_mutex_lock(m); |
| | } |
| | |
| | if (retcode) { |
| | fprintf(stderr, ""%s, %s\\n"", myErrStr, strerror(retcode)); |
| | } |
| | } |
| |
|
| | ",0 |
| |
|