Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @file main.c
- * @brief Description
- * @date 2018-1-1
- * @author name of author
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <pthread.h>
- #include "debug.h"
- #include "memory.h"
- #include "args.h"
- #define MODE_SLICE 0
- #define MODE_FIAMBRE 1
- #define MODE_QUEIJO_1 2
- #define MODE_FINALIZADOR 3
- #define MODE_TOSTAR 6
- #define MODE_QUEIJO_2 4
- #define MODE_MESA 10
- #define ERR_THREADS 5
- void *task(void *arg);
- struct gengetopt_args_info args;
- typedef struct
- {
- int mode; // slice, fiambre, etc..
- pthread_mutex_t mutex;
- pthread_cond_t cond;
- }shared_t;
- void *slice(void *);
- void *fiambre(void *);
- void *queijo(void *);
- void *finalizador(void *);
- int main(int argc, char *argv[]){
- /* Disable warnings */
- (void)argc; (void)argv;
- if(cmdline_parser(argc, argv, &args))
- ERROR(1, "Erro: execução de cmdline_parser\n");
- shared_t shared;
- shared.mode = MODE_SLICE;
- if ((errno = pthread_mutex_init(&shared.mutex, NULL)) != 0) {
- ERROR(ERR_THREADS, "pthread_mutex_init() failed");
- }
- if ((errno = pthread_cond_init(&shared.cond, NULL)) != 0) {
- ERROR(ERR_THREADS, "pthread_cond_init() failed");
- }
- pthread_t tid1,tid2,tid3,tid4;
- if ((errno = pthread_create(&tid1, NULL, slice, &shared)) != 0) {
- ERROR(ERR_THREADS, "pthread_create() failed");
- }
- if ((errno = pthread_create(&tid2, NULL, fiambre, &shared)) != 0) {
- ERROR(ERR_THREADS, "pthread_create() failed");
- }
- if ((errno = pthread_create(&tid3, NULL, queijo, &shared)) != 0) {
- ERROR(ERR_THREADS, "pthread_create() failed");
- }
- if ((errno = pthread_create(&tid4, NULL, finalizador, &shared)) != 0) {
- ERROR(ERR_THREADS, "pthread_create() failed");
- }
- if ((errno = pthread_join(tid1, NULL)) != 0) {
- ERROR(ERR_THREADS, "pthread_join() failed");
- }
- if ((errno = pthread_join(tid2, NULL)) != 0) {
- ERROR(ERR_THREADS, "pthread_join() failed");
- }
- if ((errno = pthread_join(tid3, NULL)) != 0) {
- ERROR(ERR_THREADS, "pthread_join() failed");
- }
- if ((errno = pthread_join(tid4, NULL)) != 0) {
- ERROR(ERR_THREADS, "pthread_join() failed");
- }
- if ((errno = pthread_mutex_destroy(&shared.mutex)) != 0) {
- ERROR(ERR_THREADS, "pthread_mutex_destroy() failed");
- }
- if ((errno = pthread_cond_destroy(&shared.cond)) != 0) {
- ERROR(ERR_THREADS, "pthread_cond_destroy() failed");
- }
- cmdline_parser_free(&args);
- return 0;
- }
- void *slice(void *arg)
- {
- shared_t * param = arg;
- if ((errno = pthread_mutex_lock(&(param->mutex))) != 0)
- {
- WARNING("pthread_mutex_lock() failed\n");
- return NULL;
- }
- if(param->mode!=MODE_SLICE)
- {
- if ((errno = pthread_cond_wait(&(param->cond), &(param->mutex))) != 0)
- {
- WARNING("pthread_cond_wait() failed");
- return NULL;
- }
- }
- printf("Bread Sliced!!\n");
- param->mode = MODE_QUEIJO_1;
- if ((errno = pthread_cond_signal(&(param->cond))) != 0)
- {
- WARNING("pthread_cond_signal() failed");
- return NULL;
- }
- if ((errno = pthread_mutex_unlock(&(param->mutex))) != 0)
- {
- WARNING("pthread_mutex_unlock() failed");
- return NULL;
- }
- return NULL;
- }
- void *fiambre(void *arg)
- {
- shared_t * param = arg;
- if ((errno = pthread_mutex_lock(&(param->mutex))) != 0)
- {
- WARNING("pthread_mutex_lock() failed\n");
- return NULL;
- }
- while(param->mode!=MODE_FIAMBRE )
- {
- if ((errno = pthread_cond_wait(&(param->cond), &(param->mutex))) != 0)
- {
- WARNING("pthread_cond_wait() failed");
- return NULL;
- }
- printf("Putting ham slice!!\n");
- param->mode = MODE_QUEIJO_2;
- if ((errno = pthread_cond_broadcast(&(param->cond))) != 0)
- {
- WARNING("pthread_cond_signal() failed");
- return NULL;
- }
- if ((errno = pthread_mutex_unlock(&(param->mutex))) != 0)
- {
- WARNING("pthread_mutex_unlock() failed");
- return NULL;
- }
- }
- return NULL;
- }
- void *queijo(void *arg)
- {
- shared_t * param = arg;
- if ((errno = pthread_mutex_lock(&(param->mutex))) != 0)
- {
- WARNING("pthread_mutex_lock() failed\n");
- return NULL;
- }
- if (param->mode==MODE_QUEIJO_1)
- {
- /* code */
- while(param->mode!=MODE_QUEIJO_1){
- if((errno = pthread_cond_wait(&(param->cond), &(param->mutex))) != 0)
- {
- WARNING("pthread_cond_wait() failed");
- return NULL;
- }
- printf("Putting cheese slice #1\n");
- param->mode=MODE_FIAMBRE;
- pthread_cond_broadcast(&(param->cond));
- if ((errno = pthread_mutex_unlock(&(param->mutex))) != 0)
- {
- WARNING("pthread_mutex_unlock() failed");
- return NULL;
- }
- }
- }else if(param->mode==MODE_QUEIJO_2){
- while(param->mode!=MODE_QUEIJO_2){
- if((errno = pthread_cond_wait(&(param->cond), &(param->mutex))) != 0)
- {
- WARNING("pthread_cond_wait() failed");
- return NULL;
- }
- printf("Putting cheese slice #2\n");
- param->mode=MODE_FINALIZADOR;
- pthread_cond_broadcast(&(param->cond));
- if ((errno = pthread_mutex_unlock(&(param->mutex))) != 0)
- {
- WARNING("pthread_mutex_unlock() failed");
- return NULL;
- }
- }
- }
- return NULL;
- }
- void *finalizador(void *arg)
- {
- shared_t * param = arg;
- if ((errno = pthread_mutex_lock(&(param->mutex))) != 0)
- {
- WARNING("pthread_mutex_lock() failed\n");
- return NULL;
- }
- if(param->mode!=MODE_FINALIZADOR || param->mode!=MODE_TOSTAR )
- {
- if ((errno = pthread_cond_wait(&(param->cond), &(param->mutex))) != 0)
- {
- WARNING("pthread_cond_wait() failed");
- return NULL;
- }
- }
- else if (param->mode==MODE_TOSTAR)
- {
- printf("Toasting!!\n");
- param->mode=MODE_FIAMBRE;
- }
- else if (param->mode==MODE_FINALIZADOR)
- {
- printf("A tosta está pronta\n");
- param->mode=MODE_SLICE;
- sleep(3);
- }
- if ((errno = pthread_cond_signal(&(param->cond))) != 0)
- {
- WARNING("pthread_cond_signal() failed");
- return NULL;
- }
- if ((errno = pthread_mutex_unlock(&(param->mutex))) != 0)
- {
- WARNING("pthread_mutex_unlock() failed");
- return NULL;
- }
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement