Guest User

Untitled

a guest
Dec 12th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. // for c++ related functions
  2. #include<iostream>
  3. #include<fstream>
  4. #include<string>
  5.  
  6. // for c related functions
  7. #include<cstdio>
  8. #include<cstdlib>
  9. #include<unistd.h>
  10.  
  11. // for system calls
  12. #include<sys/types.h>
  13. #include<sys/ipc.h>
  14. // for shared memory
  15. #include<sys/shm.h>
  16.  
  17. using namespace std;
  18.  
  19. // store the key for accessing the shared memory
  20. key_t shm_key = ftok("practical",65);
  21.  
  22. // the structure for shared memory
  23. struct shared{
  24. bool the_end = false;
  25. int count = 0;
  26. string str[6];
  27. };
  28.  
  29. int main(){
  30.  
  31. // create the shared memory
  32. int shmid = shmget(shm_key,sizeof(shared),0666|IPC_CREAT);
  33. if(shmid == -1){
  34. perror("Error in creating shared memory");
  35. return 1;
  36. }
  37.  
  38. // attach server to the shared memory
  39. struct shared *buffer = (shared*)shmat(shmid,NULL,0);
  40. if(buffer == (void *) -1){
  41. perror("Unable to attach to shared memory");
  42. return 1;
  43. }
  44.  
  45. // open the file for reading the input
  46. ifstream fin("input.txt");
  47. // check for errors in opening the file
  48. if(!fin){
  49. cerr<<"Error in opening the file"<<endl;
  50. exit(1);
  51. }
  52.  
  53. // to store the read line
  54. string inptxt;
  55. cout<<"Reading contents from filen"<<endl;
  56.  
  57. while(fin){
  58. // read a line
  59. getline(fin,inptxt);
  60.  
  61. // exit the loop if string is empty
  62. if(inptxt == "") break;
  63.  
  64. // store the line in the buffer
  65. buffer->str[buffer->count++] = inptxt;
  66. cout<<"This was written : "<<buffer->str[buffer->count-1]<<endl;
  67.  
  68. sleep(3);
  69. }
  70. cout<<"Closing the file"<<endl;
  71. fin.close();
  72.  
  73. // marks the end of writing into shared memory
  74. buffer->the_end = true;
  75.  
  76. if(shmdt(buffer) == -1){
  77. perror("error in deatching from shared memory");
  78. return 1;
  79. }
  80.  
  81. if(shmctl(shmid,IPC_RMID,0) == -1){
  82. perror("error in destroying the shared memory");
  83. return 1;
  84. }
  85.  
  86. cout<<"Writing Completed"<<endl;
  87. return 0;
  88. }
  89.  
  90. // for c++ related functions
  91. #include<iostream>
  92. #include<fstream>
  93. #include<string>
  94.  
  95. // for c related functions
  96. #include<cstdio>
  97. #include<cstdlib>
  98. #include<unistd.h>
  99.  
  100. // for system calls
  101. #include<sys/types.h>
  102. #include<sys/ipc.h>
  103. // for shared memory
  104. #include<sys/shm.h>
  105.  
  106. using namespace std;
  107.  
  108. // store the key for accessing the shared memory
  109. key_t shm_key = ftok("practical",65);
  110.  
  111. // the structure for shared memory
  112. struct shared{
  113. bool the_end = false;
  114. int count = 0;
  115. string str[6];
  116. };
  117.  
  118. int main(){
  119.  
  120. // create the shared memory
  121. int shmid = shmget(shm_key,sizeof(shared),0666|IPC_CREAT);
  122. if(shmid == -1){
  123. perror("Error in creating shared memory");
  124. return 1;
  125. }
  126.  
  127. // attach server to the shared memory
  128. struct shared *buffer = (shared*)shmat(shmid,NULL,0);
  129. if(buffer == (void *) -1){
  130. perror("Unable to attach to shared memory");
  131. return 1;
  132. }
  133.  
  134. cout<<"Reading contents from shared memoryn"<<endl;
  135.  
  136. int initial = 0;
  137. while(!buffer->the_end){
  138. if(buffer->count > initial){
  139. cout<<"initial : "<<initial<<" buffer count : "<<buffer->count<<"n";
  140. // the following line gives segmentation fault
  141. // cout<<buffer->str[initial]<<endl;
  142. initial++;
  143. }
  144. }
  145.  
  146. if(shmdt(buffer) == -1){
  147. perror("error in deatching from shared memory");
  148. return 1;
  149. }
  150.  
  151. cout<<"Reading Completed"<<endl;
  152. return 0;
  153. }
Add Comment
Please, Sign In to add comment