Advertisement
999ms

Untitled

Dec 12th, 2021
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <signal.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <sys/wait.h>
  6. #include <sys/file.h>
  7. #include <fcntl.h>
  8.  
  9. using namespace std;
  10.  
  11. /*
  12. 9. Добавить использование слабой блокировки во второй. Что изменится?
  13. */
  14.  
  15. bool useFlock = true;
  16. auto currentLock = LOCK_EX; // LOCK_SH;
  17.  
  18. int main(int argc, char* argv[]) {
  19.  
  20. {
  21. // fill the file with symbols '*'
  22. int fd = creat(argv[1], 0777);
  23. string s(1024 * 16, '*');
  24. write(fd, s.data(), s.size());
  25. close(fd);
  26. }
  27.  
  28. auto filename = argv[1];
  29. int pid = fork();
  30.  
  31. if (pid == 0) {
  32. int fd = open(filename, O_RDWR);
  33. if (fd < 0) {
  34. cout << "Child process: file creation failed" << endl;
  35. exit(2);
  36. }
  37.  
  38. int fcl = -1;
  39. struct flock l;
  40.  
  41. if (useFlock) {
  42. fcl = flock(fd, currentLock);
  43. } else {
  44. l.l_type = F_RDLCK;
  45. l.l_whence = SEEK_SET;
  46. l.l_start = 0;
  47. l.l_len = 16 * 1024; // until end of the file
  48.  
  49. int fcl = fcntl(fd, F_SETLKW, &l);
  50. if (fcl < 0) {
  51. cout << "Child process: fcntl set lock on file failed" << endl;
  52. perror("Here:");
  53. exit(2);
  54. } else {
  55. cout << "Child process: lock set successfully" << endl;
  56. }
  57. }
  58.  
  59. for (int i = 0; i < 15; i++) {
  60. string str(1023, char('a' + i));
  61. str += "\n";
  62. int len = write(fd, str.data(), str.size());
  63. if (len < 0) {
  64. cout << "Child process: operation writting to file failed" << endl;
  65. kill(getppid(), SIGKILL);
  66. exit(2);
  67. }
  68. cout << "Child " << i << endl;
  69. }
  70.  
  71. if (useFlock) {
  72. fcl = flock(fd, LOCK_UN);
  73. } else {
  74. l.l_type = F_UNLCK;
  75. fcl = fcntl(fd, F_SETLKW, &l);
  76. }
  77. close(fd);
  78. exit(0);
  79.  
  80. } else if (pid > 0) {
  81.  
  82. int fd = open(filename, O_RDONLY);
  83. if (fd < 0) {
  84. cout << "Parent process: file openning failed" << endl;
  85. exit(3);
  86. }
  87.  
  88. int fcl = -1;
  89. struct flock l;
  90.  
  91. if (useFlock) {
  92. fcl = flock(fd, currentLock);
  93. } else {
  94. l.l_type = F_RDLCK;
  95. l.l_whence = SEEK_SET;
  96. l.l_start = 0;
  97. l.l_len = 16 * 1024; // until end of the file
  98.  
  99. int fcl = fcntl(fd, F_SETLKW, &l);
  100. if (fcl < 0) {
  101. cout << "Parent process: fcntl set lock on file failed" << endl;
  102. perror("Here:");
  103. exit(2);
  104. } else {
  105. cout << "Parent process: lock set successfully" << endl;
  106. }
  107. }
  108.  
  109. char buffer[512];
  110. int index = 0;
  111. while (true) {
  112. int len = read(fd, buffer, sizeof(buffer));
  113. if (len < 0) {
  114. cout << "Parent process: read error!" << endl;
  115. kill(pid, SIGKILL);
  116. exit(3);
  117. } else if (len == 0) {
  118. break;
  119. }
  120. cout << "Parent read: " << string(buffer, buffer + len) << endl;
  121. }
  122.  
  123. if (useFlock) {
  124. fcl = flock(fd, LOCK_UN);
  125. } else {
  126. l.l_type = F_UNLCK;
  127. fcl = fcntl(fd, F_SETLKW, &l);
  128. }
  129.  
  130. close(fd);
  131.  
  132. int status;
  133. wait(&status);
  134.  
  135. exit(0);
  136. } else {
  137. cout << "Fork failed" << endl;
  138. exit(1);
  139. }
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement