Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Thread and Mutex Code
#include <pthread.h> #include <signal.h> #include <time.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <semaphore.h> #include <mqueue.h> #include <unistd.h> #define QUEUE_NAME "/queue_b" #define QUEUE_MAX_SIZE 128 #define TIMER_INTERVAL 2 //sec #define TIMER_RUN_TIME 3 //For Process End. #define THREAD_WAIT_TICK 1 //us #define DEBUG_MESSAGE 1 //----Call Debug Function--------------------------- void err_abort(int status, char* message) { fprintf(stderr, "%s\n", message); exit(status); } void errno_abort(char* message) { perror(message); exit(EXIT_FAILURE); } //-------------------------------------------------------- //----Thread_A Init and Functions------------------------- pthread_mutex_t mutex_A = PTHREAD_MUTEX_INITIALIZER; void * thread_A_main(void * args) { #if (defined(DEBUG_MESSAGE) && DEBUG_MESSAGE) printf("thread_A in\n"); #endif while(1) { if(pthread_mutex_trylock(&mutex_A) == 0) // if unlock, lock it { printf("HELLO\n"); } usleep(THREAD_WAIT_TICK); } pthread_exit(0); } //-------------------------------------------------------- //----Thread_B Init and Functions------------------------- void * thread_B_main(void * args) { mqd_t mq; ssize_t bytes_read; struct mq_attr attr; char buffer[QUEUE_MAX_SIZE + 1]; /* initialize the queue attributes */ attr.mq_flags = 0; attr.mq_maxmsg = 10; attr.mq_msgsize = QUEUE_MAX_SIZE; attr.mq_curmsgs = 0; /* create the message queue */ mq = mq_open(QUEUE_NAME, O_CREAT | O_RDONLY | O_NONBLOCK , 0644, &attr); #if (defined(DEBUG_MESSAGE) && DEBUG_MESSAGE) printf("thread_B in\n"); #endif while(1) { memset(buffer, 0x00, sizeof(buffer)); bytes_read = mq_receive(mq, buffer, QUEUE_MAX_SIZE, NULL); if(bytes_read >= 0) { #if (defined(DEBUG_MESSAGE) && DEBUG_MESSAGE) printf("Thread_B: Received message: %s\n", buffer); #endif if (pthread_mutex_unlock(&mutex_A) != 0) err_abort(0, "Error Unlock A mutex"); } usleep(THREAD_WAIT_TICK); } /* cleanup */ mq_close(mq); mq_unlink(QUEUE_NAME); pthread_exit(0); } //-------------------------------------------------------- //----Timer_C Init and Functions-------------------------- pthread_mutex_t timer_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t timer_cond = PTHREAD_COND_INITIALIZER; int time_counter = 0; void timer_C(union sigval C) { mqd_t mq; char buffer[QUEUE_MAX_SIZE]; if (pthread_mutex_lock(&timer_mutex) != 0) err_abort(0, "Error Lock Time mutex"); //----------------Time_C Program-------------------- mq = mq_open(QUEUE_NAME, O_WRONLY); time_counter++; #if (defined(DEBUG_MESSAGE) && DEBUG_MESSAGE) printf("Timer_C in %d \n",time_counter); #endif snprintf(buffer, sizeof(buffer), "MESSAGE %d", time_counter); if(mq_send(mq, buffer, strlen(buffer), 0) != 0) err_abort(0, "Error mq enque"); mq_close(mq); //-------------------------------------------------- if (time_counter >= TIMER_RUN_TIME) { if (pthread_cond_signal(&timer_cond) != 0) //if counter reached, let the process end. err_abort(0, "Error Time Signal condition"); } if (pthread_mutex_unlock(&timer_mutex) != 0) err_abort(0, "Error Unlock Time mutex"); } void create_timer() { timer_t timer_id; struct itimerspec ts; struct sigevent se; /* * Set the sigevent structure to cause the signal to be * delivered by creating a new thread. */ se.sigev_notify = SIGEV_THREAD; se.sigev_value.sival_ptr = &timer_id; se.sigev_notify_function = timer_C; se.sigev_notify_attributes = NULL; if (timer_create(CLOCK_REALTIME, &se, &timer_id) == -1) errno_abort("Error Create timer"); ts.it_value.tv_sec = TIMER_INTERVAL; ts.it_interval.tv_sec = TIMER_INTERVAL; if (timer_settime(timer_id, 0, &ts, 0) == -1) errno_abort("Error Set timer"); } int main() { pthread_t thread_a, thread_b; create_timer(); pthread_create(&thread_a, NULL, &thread_A_main, NULL); pthread_create(&thread_b, NULL, &thread_B_main, NULL); //---------Waitting Timer_C End for End the Process------------- /* * To design for End of Timer, * beaceuse of some online compiler print after process end. */ if (pthread_mutex_lock(&timer_mutex) != 0) err_abort(0, "Lock mutex"); if (pthread_cond_wait(&timer_cond, &timer_mutex) != 0) err_abort(0, "Wait on Timer condition"); if(pthread_cancel(thread_a) != 0) err_abort(0, "Error Cancel Thread A"); if(pthread_cancel(thread_b) != 0) err_abort(0, "Error Cancel Thread B"); #if (defined(DEBUG_MESSAGE) && DEBUG_MESSAGE) printf("End of Process!"); #endif mq_unlink(QUEUE_NAME); return 0; if (pthread_mutex_unlock(&timer_mutex) != 0) err_abort(0, "Error Unlock Timer mutex"); //-------------------------------------------------------------- pthread_join(thread_a, NULL); // wait thread end. pthread_join(thread_b, NULL); // wait thread end. mq_unlink(QUEUE_NAME); return 0; }
run
|
edit
|
history
|
help
0
Spring 2017 Lab 4 v1
lab pro v.05
pllkkj
A_141124Burbuja
Little union method
Quiz4
LECTURA DE CADENAS CON FGETS
abc.c
Simple Calculator v1.0
a3 commented