fork(1) download
  1. Jantar dos Filósofos – Implentação em C
  2.  
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<semaphore.h>
  6. #include<pthread.h>
  7. #define N 5
  8. #define PENSAR 0
  9. #define FOME 1
  10. #define COMER 2
  11. #define ESQUERDA (nfilosofo+4)%N //agarrar garfo
  12. //da esquerda
  13. #define DIREITA (nfilosofo+1)%N //agarrar garfo
  14. //da direita
  15. void *filosofo(void *num);
  16. void agarraGarfo(int);
  17. void deixaGarfo(int);
  18. void testar(int);
  19.  
  20. sem_t mutex;
  21. sem_t S[N]; //inicializacao do semáforo
  22. int estado[N];
  23. int nfilosofo[N]={0,1,2,3,4};
  24.  
  25. void *filosofo(void *num)
  26. {
  27. while(1)
  28. {
  29. int *i = num;
  30. sleep(1);
  31. agarraGarfo(*i);
  32. sleep(1);
  33. deixaGarfo(*i);
  34. }
  35. }
  36.  
  37. void agarraGarfo(int nfilosofo)
  38. {
  39. sem_wait(&mutex);
  40. estado[nfilosofo] = FOME;
  41. printf("Filosofo %d tem fome.\n", nfilosofo+1);
  42. //+1 para imprimir filosofo 1 e nao filosofo 0
  43. testar(nfilosofo);
  44. sem_post(&mutex);
  45. sem_wait(&S[nfilosofo]);
  46. sleep(1);
  47. }
  48.  
  49. void deixaGarfo(int nfilosofo)
  50. {
  51. sem_wait(&mutex);
  52. estado[nfilosofo]=PENSAR;
  53. printf("Filosofo %d deixou os garfos %d e %d.\n", nfilosofo+1, ESQUERDA+1, nfilosofo+1);
  54. printf("Filosofo %d esta a pensar.\n", nfilosofo+1);
  55. testar(ESQUERDA);
  56. testar(DIREITA);
  57. sem_post(&mutex);
  58. }
  59.  
  60. void testar(int nfilosofo)
  61. {
  62. if(estado[nfilosofo]==FOME && estado[ESQUERDA]
  63. !=COMER && estado[DIREITA]!=COMER)
  64. {
  65. estado[nfilosofo]=COMER;
  66. sleep(2);
  67. printf("Filosofo %d agarrou os garfos %d e %d.\n", nfilosofo+1, ESQUERDA+1, nfilosofo+1);
  68. printf("Filosofo %d esta a comer.\n", nfilosofo+1);
  69. sem_post(&S[nfilosofo]);
  70. }
  71. }
  72.  
  73. int main() {
  74. int i;
  75. pthread_t thread_id[N]; //identificadores das
  76. //threads
  77. sem_init(&mutex,0,1);
  78. for(i=0;i<N;i++)
  79. sem_init(&S[i],0,0);
  80. for(i=0;i<N;i++)
  81. {
  82. pthread_create(&thread_id[i], NULL, filosofo, &nfilosofo[i]);
  83. //criar as threads
  84. printf("Filosofo %d esta a pensar.\n",i+1);
  85. }
  86. for(i=0;i<N;i++)
  87. pthread_join(thread_id[i],NULL); //para
  88. //fazer a junção das threads
  89. return(0);
  90. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:1:1: error: unknown type name ‘Jantar’
 Jantar dos Filósofos – Implentação em C
 ^~~~~~
prog.c:1:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘Fil’
 Jantar dos Filósofos – Implentação em C
            ^~~
prog.c:1:15: error: stray ‘\303’ in program
 Jantar dos Filósofos – Implentação em C
               ^
prog.c:1:16: error: stray ‘\263’ in program
 Jantar dos Filósofos – Implentação em C
                ^
prog.c:1:12: error: unknown type name ‘Fil’
 Jantar dos Filósofos – Implentação em C
            ^~~
prog.c:1:23: error: stray ‘\342’ in program
 Jantar dos Filósofos – Implentação em C
                       ^
prog.c:1:24: error: stray ‘\200’ in program
 Jantar dos Filósofos – Implentação em C
                        ^
prog.c:1:25: error: stray ‘\223’ in program
 Jantar dos Filósofos – Implentação em C
                         ^
prog.c:1:35: error: stray ‘\303’ in program
 Jantar dos Filósofos – Implentação em C
                                   ^
prog.c:1:36: error: stray ‘\247’ in program
 Jantar dos Filósofos – Implentação em C
                                    ^
prog.c:1:37: error: stray ‘\303’ in program
 Jantar dos Filósofos – Implentação em C
                                     ^
prog.c:1:38: error: stray ‘\243’ in program
 Jantar dos Filósofos – Implentação em C
                                      ^
In file included from /usr/include/stdio.h:74:0,
                 from prog.c:3:
/usr/include/libio.h:302:3: error: unknown type name ‘size_t’
   size_t __pad5;
   ^~~~~~
/usr/include/libio.h:305:67: error: ‘size_t’ undeclared here (not in a function)
   char _unused2[15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t)];
                                                                   ^~~~~~
/usr/include/libio.h:333:62: error: expected declaration specifiers or ‘...’ before ‘size_t’
 typedef __ssize_t __io_read_fn (void *__cookie, char *__buf, size_t __nbytes);
                                                              ^~~~~~
/usr/include/libio.h:342:6: error: expected declaration specifiers or ‘...’ before ‘size_t’
      size_t __n);
      ^~~~~~
/usr/include/libio.h:464:19: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘_IO_sgetn’
 extern _IO_size_t _IO_sgetn (_IO_FILE *, void *, _IO_size_t);
                   ^~~~~~~~~
In file included from prog.c:3:0:
/usr/include/stdio.h:339:20: error: expected declaration specifiers or ‘...’ before ‘size_t’
       int __modes, size_t __n) __THROW;
                    ^~~~~~
/usr/include/stdio.h:388:44: error: expected declaration specifiers or ‘...’ before ‘size_t’
 extern int snprintf (char *__restrict __s, size_t __maxlen,
                                            ^~~~~~
/usr/include/stdio.h:392:45: error: expected declaration specifiers or ‘...’ before ‘size_t’
 extern int vsnprintf (char *__restrict __s, size_t __maxlen,
                                             ^~~~~~
/usr/include/stdio.h:711:15: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fread’
 extern size_t fread (void *__restrict __ptr, size_t __size,
               ^~~~~
/usr/include/stdio.h:717:15: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fwrite’
 extern size_t fwrite (const void *__restrict __ptr, size_t __size,
               ^~~~~~
In file included from prog.c:4:0:
/usr/include/stdlib.h:100:15: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__ctype_get_mb_cur_max’
 extern size_t __ctype_get_mb_cur_max (void) __THROW __wur;
               ^~~~~~~~~~~~~~~~~~~~~~
/usr/include/stdlib.h:427:22: error: expected declaration specifiers or ‘...’ before ‘size_t’
 extern void *malloc (size_t __size) __THROW __attribute_malloc__ __wur;
                      ^~~~~~
/usr/include/stdlib.h:429:22: error: expected declaration specifiers or ‘...’ before ‘size_t’
 extern void *calloc (size_t __nmemb, size_t __size)
                      ^~~~~~
/usr/include/stdlib.h:429:38: error: expected declaration specifiers or ‘...’ before ‘size_t’
 extern void *calloc (size_t __nmemb, size_t __size)
                                      ^~~~~~
/usr/include/stdlib.h:441:36: error: expected declaration specifiers or ‘...’ before ‘size_t’
 extern void *realloc (void *__ptr, size_t __size)
                                    ^~~~~~
/usr/include/stdlib.h:470:29: error: expected declaration specifiers or ‘...’ before ‘size_t’
 extern void *aligned_alloc (size_t __alignment, size_t __size)
                             ^~~~~~
/usr/include/stdlib.h:470:49: error: expected declaration specifiers or ‘...’ before ‘size_t’
 extern void *aligned_alloc (size_t __alignment, size_t __size)
                                                 ^~~~~~
/usr/include/stdlib.h:716:9: error: expected declaration specifiers or ‘...’ before ‘size_t’
         size_t __nmemb, size_t __size, __compar_fn_t __compar)
         ^~~~~~
/usr/include/stdlib.h:716:25: error: expected declaration specifiers or ‘...’ before ‘size_t’
         size_t __nmemb, size_t __size, __compar_fn_t __compar)
                         ^~~~~~
In file included from /usr/include/stdlib.h:720:0,
                 from prog.c:4:
/usr/include/x86_64-linux-gnu/bits/stdlib-bsearch.h:20:49: error: expected declaration specifiers or ‘...’ before ‘size_t’
 bsearch (const void *__key, const void *__base, size_t __nmemb, size_t __size,
                                                 ^~~~~~
/usr/include/x86_64-linux-gnu/bits/stdlib-bsearch.h:20:65: error: expected declaration specifiers or ‘...’ before ‘size_t’
 bsearch (const void *__key, const void *__base, size_t __nmemb, size_t __size,
                                                                 ^~~~~~
In file included from prog.c:4:0:
/usr/include/stdlib.h:725:34: error: expected declaration specifiers or ‘...’ before ‘size_t’
 extern void qsort (void *__base, size_t __nmemb, size_t __size,
                                  ^~~~~~
/usr/include/stdlib.h:725:50: error: expected declaration specifiers or ‘...’ before ‘size_t’
 extern void qsort (void *__base, size_t __nmemb, size_t __size,
                                                  ^~~~~~
/usr/include/stdlib.h:823:36: error: expected declaration specifiers or ‘...’ before ‘size_t’
 extern int mblen (const char *__s, size_t __n) __THROW;
                                    ^~~~~~
/usr/include/stdlib.h:827:34: error: expected declaration specifiers or ‘...’ before ‘size_t’
      const char *__restrict __s, size_t __n) __THROW;
                                  ^~~~~~
/usr/include/stdlib.h:834:15: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘mbstowcs’
 extern size_t mbstowcs (wchar_t *__restrict  __pwcs,
               ^~~~~~~~
/usr/include/stdlib.h:837:15: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘wcstombs’
 extern size_t wcstombs (char *__restrict __s,
               ^~~~~~~~
In file included from /usr/include/sched.h:34:0,
                 from /usr/include/pthread.h:23,
                 from prog.c:6:
/usr/include/time.h:205:15: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘strftime’
 extern size_t strftime (char *__restrict __s, size_t __maxsize,
               ^~~~~~~~
In file included from /usr/include/sched.h:43:0,
                 from /usr/include/pthread.h:23,
                 from prog.c:6:
/usr/include/x86_64-linux-gnu/bits/sched.h:204:30: error: expected declaration specifiers or ‘...’ before ‘size_t’
 extern int __sched_cpucount (size_t __setsize, const cpu_set_t *__setp)
                              ^~~~~~
/usr/include/x86_64-linux-gnu/bits/sched.h:206:37: error: expected declaration specifiers or ‘...’ before ‘size_t’
 extern cpu_set_t *__sched_cpualloc (size_t __count) __THROW __wur;
                                     ^~~~~~
In file included from prog.c:6:0:
/usr/include/pthread.h:306:11: error: expected declaration specifiers or ‘...’ before ‘size_t’
           size_t *__guardsize)
           ^~~~~~
/usr/include/pthread.h:311:11: error: expected declaration specifiers or ‘...’ before ‘size_t’
           size_t __guardsize)
           ^~~~~~
/usr/include/pthread.h:369:19: error: expected declaration specifiers or ‘...’ before ‘size_t’
           __attr, size_t *__restrict __stacksize)
                   ^~~~~~
/usr/include/pthread.h:376:11: error: expected declaration specifiers or ‘...’ before ‘size_t’
           size_t __stacksize)
           ^~~~~~
prog.c: In function ‘filosofo’:
prog.c:30:7: warning: implicit declaration of function ‘sleep’ [-Wimplicit-function-declaration]
       sleep(1);
       ^~~~~
stdout
Standard output is empty