Advertisement
iuliaa

Untitled

Nov 13th, 2020
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <signal.h>
  7. #include <sys/wait.h>
  8.  
  9. pid_t pid, prevChild;
  10. int waiter, k = 0;
  11.  
  12. void handleUSR1(int signum) {
  13. if(signum == SIGUSR1) {
  14. printf("<%d> am primit <semnal>\n", getpid());
  15. exit(1);
  16. }
  17. }
  18.  
  19. void handleUSR2(int signum) {
  20. if(signum == SIGUSR2)
  21. waiter = 0;
  22. }
  23.  
  24. void handleChildEnd(int signum) {
  25. int status;
  26. wait(&status);
  27. printf("Copilul <%d> s-a terminat\n", prevChild);
  28. waiter = 0;
  29. }
  30.  
  31. int main(int argc, char** argv) {
  32. if(argc < 4) {
  33. printf("Numarul de argumente nu corespunde cerintei");
  34. exit(-1);
  35. }
  36.  
  37. if(strlen(argv[2]) != strlen(argv[3])) {
  38. printf("String-urile nu au dimensiuni egale");
  39. exit(-2);
  40. }
  41.  
  42. struct stat* stats = (struct stat*)malloc(sizeof(struct stat));
  43.  
  44. if(!stats) {
  45. printf("Memoria pentru structura stat nu a fost apelata corespunzator");
  46. exit(-3);
  47. }
  48.  
  49. int statFunction = stat(argv[1], stats);
  50.  
  51. if(!S_ISREG(stats->st_mode)) {
  52. printf("Argumentul 1 nu este un fisier obsinuit");
  53. exit(-4);
  54. }
  55. free(stats);
  56. char* filePath = (char*)calloc(30, sizeof(char));
  57.  
  58. if(!filePath) {
  59. printf("Memoria pentru filePath nu a fost alocata corespunzator");
  60. exit(-6);
  61. }
  62.  
  63. strcpy(filePath, argv[1]);
  64. char newPath[30], name[30];
  65. strcpy(newPath, "");
  66. strcpy(name, argv[1]);
  67. name[strlen(name) - 4] = '\0';
  68.  
  69. for(int i = 0; i < strlen(argv[2]); ++i) {
  70. waiter = 1;
  71. sprintf(newPath, "%s%d.txt", name, i);
  72. pid = fork();
  73.  
  74. if(pid < 0) {
  75. perror("Fork: ");
  76. exit(-5);
  77. }
  78.  
  79. if(!pid) {
  80. int fd1 = open(filePath, O_RDONLY);
  81. int fd2 = open(newPath, O_WRONLY | O_CREAT | O_TRUNC, 0777);
  82. struct sigaction sigUSR1;
  83. memset(&sigUSR1, 0x00, sizeof(struct sigaction));
  84. sigUSR1.sa_handler = handleUSR1;
  85. int signal = sigaction(SIGUSR1, &sigUSR1, NULL);
  86.  
  87. if(signal < 0) {
  88. perror("Signal: ");
  89. exit(-9);
  90. }
  91.  
  92. if(!fd1 || !fd2) {
  93. perror("Open: ");
  94. exit(-7);
  95. }
  96.  
  97. int reader, writer;
  98. char* buffer = (char*)calloc(10, sizeof(char));
  99.  
  100. if(!buffer) {
  101. printf("Memoria pentru buffer nu a fost alocata corespunzator");
  102. exit(-8);
  103. }
  104.  
  105. while((reader = read(fd1, (void*)buffer, 10)) > 0) {
  106. for(int j = 0; j < reader; ++j)
  107. if(buffer[j] == argv[2][i])
  108. buffer[j] = argv[3][i];
  109. writer = write(fd2, buffer, reader);
  110. }
  111. sleep(1);
  112. close(fd1);
  113. close(fd2);
  114. free(buffer);
  115.  
  116. if(i != 0)
  117. kill(prevChild, SIGUSR1);
  118.  
  119. if(i == 0)
  120. kill(getppid(), SIGUSR2);
  121. if(i == strlen(argv[2]) - 1)
  122. exit(0);
  123. }
  124. strcpy(filePath, newPath);
  125. struct sigaction sigChild;
  126. memset(&sigChild, 0x00, sizeof(struct sigaction));
  127. sigChild.sa_handler = handleChildEnd;
  128. int signal1 = sigaction(SIGCHLD, &sigChild, NULL);
  129.  
  130. struct sigaction sigUSR2;
  131. memset(&sigUSR2, 0x00, sizeof(struct sigaction));
  132. sigUSR2.sa_handler = handleUSR2;
  133. int signal2 = sigaction(SIGUSR2, &sigUSR2, NULL);
  134.  
  135. if(signal1 < 0 || signal2 < 0) {
  136. perror("Signal: ");
  137. exit(-10);
  138. }
  139.  
  140. while(waiter);
  141. prevChild = pid;
  142. sleep(1);
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement