Guest User

Untitled

a guest
Dec 10th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. read.c:21:8: error: conflicting types for ‘write’
  2. void * write(void *temp) {
  3. ^
  4. In file included from read.c:11:0:
  5. /usr/include/unistd.h:369:16: note: previous declaration of ‘write’ was here
  6. extern ssize_t write (int __fd, const void *__buf, size_t __n) __wur;
  7. ^
  8. read.c: In function ‘write’:
  9. read.c:25:4: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
  10. ret=pthread_rwlock_wrlock(&rwlock);
  11. ^
  12. read.c: In function ‘write_2’:
  13. read.c:45:4: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
  14. ret=pthread_rwlock_wrlock(&rwlock);
  15. ^
  16. read.c: At top level:
  17. read.c:106:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
  18. main() {
  19.  
  20. #include <stdio.h>
  21. #include <pthread.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24.  
  25. /*
  26. From the output we can see that the two writes were executed one after the other. But in case of reads, even though read_1 had not unlocked the rwlock, read_2 was allowed into the critical section and read the file. That shows us that multiple readers are allowed but only one writer is allowed into the critical section.
  27. */
  28. pthread_rwlock_t rwlock; // allows multiple readers to access the resource, but only one reader at any given time.
  29.  
  30.  
  31.  
  32.  
  33. void * write(void *temp) {
  34. char *ret;
  35. FILE *file1;
  36. char *str;
  37. ret=pthread_rwlock_wrlock(&rwlock);
  38. printf("nFile locked, please enter the message n");
  39. str=(char *)malloc(10*sizeof(char));
  40. file1=fopen("temp","w");
  41. scanf("%s",str);
  42. fprintf(file1,"%s",str);
  43. fclose(file1);
  44. pthread_rwlock_unlock(&rwlock);
  45. printf("nUnlocked the file you can read it now n");
  46. return ret;
  47. }
  48.  
  49.  
  50.  
  51.  
  52. void * write_2(void *temp) {
  53. char *ret;
  54. FILE *file1;
  55. char *str;
  56. sleep(3);
  57. ret=pthread_rwlock_wrlock(&rwlock);
  58. printf("nFile locked, please enter the message n");
  59. str=(char *)malloc(10*sizeof(char));
  60. file1=fopen("temp","a");
  61. scanf("%s",str);
  62. fprintf(file1,"%s",str);
  63. fclose(file1);
  64. pthread_rwlock_unlock(&rwlock);
  65. printf("nUnlocked the file you can read it now n");
  66. return ret;
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73. void * read_1(void *temp) {
  74. char *ret;
  75. FILE *file1;
  76. char *str;
  77. sleep(5);
  78. pthread_rwlock_rdlock(&rwlock);
  79. printf("n1 Opening file for readingn");
  80. file1=fopen("temp","r");
  81. str=(char *)malloc(10*sizeof(char));
  82. fscanf(file1,"%s",str);
  83. printf("nMessage from file is %s n",str);
  84. sleep(3);
  85.  
  86. fclose(file1);
  87. printf("nUnlocking rwlockn");
  88. pthread_rwlock_unlock(&rwlock);
  89. return ret;
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96. void * read_2(void *temp) {
  97. char *ret;
  98. FILE *file1;
  99. char *str;
  100. sleep(6);
  101. pthread_rwlock_rdlock(&rwlock);
  102. printf("n2 Opening file for readingn");
  103. file1=fopen("temp","r");
  104. str=(char *)malloc(10*sizeof(char));
  105. fscanf(file1,"%s",str);
  106. printf("nMessage from file is %s n",str);
  107.  
  108. fclose(file1);
  109.  
  110. pthread_rwlock_rdlock(&rwlock);
  111.  
  112. return ret;
  113. }
  114.  
  115.  
  116.  
  117.  
  118. main() {
  119.  
  120. pthread_t thread_id,thread_id1,thread_id3,thread_id4;
  121. pthread_attr_t attr;
  122. int ret;
  123. void *res;
  124. pthread_rwlock_init(&rwlock,NULL);
  125. ret=pthread_create(&thread_id,NULL,&write,NULL);
  126. ret=pthread_create(&thread_id1,NULL,&read_1,NULL);
  127.  
  128. ret=pthread_create(&thread_id3,NULL,&read_2,NULL);
  129.  
  130. ret=pthread_create(&thread_id4,NULL,&write_2,NULL);
  131. printf("n Created thread");
  132. pthread_join(thread_id,&res);
  133. pthread_join(thread_id1,&res);
  134. pthread_join(thread_id3,&res);
  135.  
  136. pthread_join(thread_id4,&res);
  137. pthread_rwlock_destroy(&rwlock);
  138. }
Add Comment
Please, Sign In to add comment