Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include "debug.h"
- #include "memory.h"
- #define PING 0
- #define PONG 1
- #define MAX_ITERATIONS 10
- void *ping(void *arg);
- void *pong(void *arg);
- typedef struct
- {
- int next;
- pthread_mutex_t mutex;
- pthread_cond_t cond;
- }thread_params_t;
- int main(int argc, char *argv[]){
- /* Disable warnings */
- (void)argc; (void)argv;
- pthread_t t1, t2;
- thread_params_t thread_params;
- //Inicializa o mutex
- if ((errno = pthread_mutex_init(&thread_params.mutex, NULL)) != 0)
- ERROR(12, "pthread_mutex_init() failed");
- // Var.Condição: inicializa variável de condição
- if ((errno = pthread_cond_init(&thread_params.cond, NULL)) != 0)
- ERROR(14, "pthread_cond_init() failed!");
- //Criação das threads + passagem de parâmetro
- if ((errno = pthread_create(&t1, NULL, ping, &thread_params) != 0))
- ERROR(10, "Erro no pthread_create()!");
- if ((errno = pthread_create(&t2, NULL, pong, &thread_params) != 0))
- ERROR(10, "Erro no pthread_create()!");
- //Espera que as threads terminem
- if ((errno = pthread_join(t1, NULL)) != 0)
- ERROR(11, "Erro no pthread_join()!\n");
- if ((errno = pthread_join(t2, NULL)) != 0)
- ERROR(11, "Erro no pthread_join()!\n");
- // Var.Condição: destroi a variável de condição
- if ((errno = pthread_cond_destroy(&thread_params.cond)) != 0)
- ERROR(15,"pthread_cond_destroy failed!");
- //Liberta o mutex
- if ((errno = pthread_mutex_destroy(&thread_params.mutex)) != 0)
- ERROR(13, "pthread_mutex_destroy() failed");
- return 0;
- }
- void *ping(void *arg)
- {
- // cast para o tipo de dados enviado pela 'main thread'
- thread_params_t *params = (thread_params_t *) arg;
- for (int i = 0; i < MAX_ITERATIONS; ++i)
- {
- // Mutex: entra na secção crítica
- if ((errno = pthread_mutex_lock(¶ms->mutex)) != 0){
- WARNING("pthread_mutex_lock() failed");
- return NULL;
- }
- // Var.Condição: bloqueia a thread usando a variável de condição (em conjunto com o mutex)
- while (params->next != PING){
- if ((errno = pthread_cond_wait(¶ms->cond, ¶ms->mutex)!=0)){
- WARNING("pthread_cond_wait() failed");
- return NULL;
- }
- }
- printf("PING...\n");
- params->next = PONG;
- // Var.Condição: notifica a primeira thread à espera na variável de condição
- if ((errno = pthread_cond_signal(¶ms->cond)) != 0){
- WARNING("pthread_cond_signal() failed");
- return NULL;
- }
- // Mutex: sai da secção crítica
- if ((errno = pthread_mutex_unlock(¶ms->mutex)) != 0){
- WARNING("pthread_mutex_unlock() failed");
- return NULL;
- }
- }
- return NULL;
- }
- void *pong(void *arg)
- {
- // cast para o tipo de dados enviado pela 'main thread'
- thread_params_t *params = (thread_params_t *) arg;
- for (int i = 0; i < MAX_ITERATIONS; ++i)
- {
- // Mutex: entra na secção crítica
- if ((errno = pthread_mutex_lock(¶ms->mutex)) != 0){
- WARNING("pthread_mutex_lock() failed");
- return NULL;
- }
- // Var.Condição: bloqueia a thread usando a variável de condição (em conjunto com o mutex)
- while (params->next != PONG){
- if ((errno = pthread_cond_wait(¶ms->cond, ¶ms->mutex)!=0)){
- WARNING("pthread_cond_wait() failed");
- return NULL;
- }
- }
- printf("\t...PONG!\n");
- params->next = PING;
- // Var.Condição: notifica a primeira thread à espera na variável de condição
- if ((errno = pthread_cond_signal(¶ms->cond)) != 0){
- WARNING("pthread_cond_signal() failed");
- return NULL;
- }
- // Mutex: sai da secção crítica
- if ((errno = pthread_mutex_unlock(¶ms->mutex)) != 0){
- WARNING("pthread_mutex_unlock() failed");
- return NULL;
- }
- }
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement