fork download
  1. # include <stdio.h>
  2. # include <pthread.h>
  3. # define BufferSize 10
  4.  
  5. void *Producer();
  6. void *Consumer();
  7.  
  8. int BufferIndex=0;
  9. char *BUFFER;
  10.  
  11. pthread_cond_t Buffer_Not_Full=PTHREAD_COND_INITIALIZER;
  12. pthread_cond_t Buffer_Not_Empty=PTHREAD_COND_INITIALIZER;
  13. pthread_mutex_t mVar=PTHREAD_MUTEX_INITIALIZER;
  14.  
  15. int main()
  16. {
  17. pthread_t ptid,ctid;
  18.  
  19. BUFFER=(char *) malloc(sizeof(char) * BufferSize);
  20.  
  21. pthread_create(&ptid,NULL,Producer,NULL);
  22. pthread_create(&ctid,NULL,Consumer,NULL);
  23.  
  24. pthread_join(ptid,NULL);
  25. pthread_join(ctid,NULL);
  26.  
  27.  
  28. return 0;
  29. }
  30.  
  31. void *Producer()
  32. {
  33. for(;;)
  34. {
  35. pthread_mutex_lock(&mVar);
  36. if(BufferIndex==BufferSize)
  37. {
  38. pthread_cond_wait(&Buffer_Not_Full,&mVar);
  39. }
  40. BUFFER[BufferIndex++]='@';
  41. printf("Produce : %d \n",BufferIndex);
  42. pthread_mutex_unlock(&mVar);
  43. pthread_cond_signal(&Buffer_Not_Empty);
  44. }
  45.  
  46. }
  47.  
  48. void *Consumer()
  49. {
  50. for(;;)
  51. {
  52. pthread_mutex_lock(&mVar);
  53. if(BufferIndex==-1)
  54. {
  55. pthread_cond_wait(&Buffer_Not_Empty,&mVar);
  56. }
  57. printf("Consume : %d \n",BufferIndex--);
  58. pthread_mutex_unlock(&mVar);
  59. pthread_cond_signal(&Buffer_Not_Full);
  60. }
  61. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:19:53: error: 'malloc' was not declared in this scope
     BUFFER=(char *) malloc(sizeof(char) * BufferSize);            
                                                     ^
prog.cpp:21:44: error: invalid conversion from 'void* (*)()' to 'void* (*)(void*)' [-fpermissive]
     pthread_create(&ptid,NULL,Producer,NULL);
                                            ^
In file included from prog.cpp:2:0:
/usr/include/pthread.h:244:12: note: initializing argument 3 of 'int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)'
 extern int pthread_create (pthread_t *__restrict __newthread,
            ^
prog.cpp:22:44: error: invalid conversion from 'void* (*)()' to 'void* (*)(void*)' [-fpermissive]
     pthread_create(&ctid,NULL,Consumer,NULL);
                                            ^
In file included from prog.cpp:2:0:
/usr/include/pthread.h:244:12: note: initializing argument 3 of 'int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)'
 extern int pthread_create (pthread_t *__restrict __newthread,
            ^
stdout
Standard output is empty