Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <zconf.h>
  4. #include <sys/file.h>
  5. #include <sys/stat.h>
  6. #include <string.h>
  7.  
  8. #define piperead 0
  9. #define pipewrite 1
  10.  
  11. int pipe_fd1[2];
  12. int pipe_fd2[2];
  13.  
  14. int getFileDimension(const char* fileName){
  15. struct stat st;
  16. if(stat(fileName, &st) != 0){
  17. perror("File doesn't exist\n");
  18. }
  19. return st.st_size;
  20. }
  21.  
  22. char* getNCharFromEndFile(char* fileInput, int n){
  23. int fd = open(fileInput, O_RDONLY);
  24. if(fd==-1)perror("open");
  25.  
  26. int resL=lseek(fd,-n,SEEK_END);
  27. if(resL==-1)perror("lseek");
  28.  
  29. char *buffer = malloc(sizeof(char));
  30. int res = read(fd, buffer, 1);
  31. if(res==-1)perror("read");
  32. return buffer;
  33. }
  34. char* getNCharFromFile(char* fileInput, int n){
  35. int fd = open(fileInput, O_RDONLY);
  36. if(fd==-1)perror("open");
  37.  
  38. int resL=lseek(fd,n,SEEK_SET);
  39. if(resL==-1)perror("lseek");
  40.  
  41. char *buffer = malloc(sizeof(char));
  42. int res = read(fd, buffer, 1);
  43. if(res==-1)perror("read");
  44. return buffer;
  45. }
  46.  
  47. void padre(char* fileInput);
  48. void figlio1(char* fileInput);
  49. void figlio2(char *string);
  50.  
  51. int main(int argc, char* argv[]) {
  52. printf("%s\n", argv[1]);
  53. printf("%s\n", argv[2]);
  54.  
  55. if(argc!=3)
  56. exit(999);
  57.  
  58. pid_t sons[2]; //array di pid per i processi
  59.  
  60. if (pipe(pipe_fd1)==-1){
  61. fprintf(stderr, "Pipe Failed" );
  62. return 1;
  63. }
  64. if (pipe(pipe_fd2)==-1){
  65. fprintf(stderr, "Pipe Failed" );
  66. return 1;
  67. }
  68.  
  69. switch(sons[0]=fork()){ //Figlio 1
  70. case -1: perror("Errore fork 1"); break;
  71. case 0:{
  72. figlio1(argv[1]);
  73. } break;
  74. default:
  75. switch(sons[1]=fork()){ //Figlio 2
  76. case -1: perror("Errore fork 2"); break;
  77. case 0:{
  78. figlio2(argv[2]);
  79. } break;
  80. default:
  81. padre(argv[1]);
  82. exit(0);
  83. break;
  84. }
  85. break;
  86. }
  87.  
  88. waitpid(sons[0],NULL,0);
  89. waitpid(sons[1],NULL,0);
  90.  
  91. return 0;
  92. }
  93.  
  94. void figlio2(char *fileOutput) {
  95. char* readFromFiglio1 = malloc(sizeof(char));
  96.  
  97. int fd = open(fileOutput, O_WRONLY | O_CREAT | O_APPEND);
  98.  
  99. while((read(pipe_fd2[piperead],readFromFiglio1,1))>0){
  100. printf("Figlio 2 deve scrivere: %s\n",readFromFiglio1);
  101. write(fd,readFromFiglio1,1);
  102. }
  103. close(fd);
  104. close(pipe_fd2[piperead]);
  105. }
  106. void figlio1(char* fileInput) {
  107. close(pipe_fd1[pipewrite]);
  108. close(pipe_fd2[piperead]);
  109.  
  110. int index = 0;
  111. char* readFromPadre = malloc(sizeof(char));
  112. while((read(pipe_fd1[piperead],readFromPadre,1))>0){
  113. char* readFromFiglio1 = getNCharFromFile(fileInput,index);
  114. printf("Padre: %s, Figlio: %s, Index; %d\n",readFromPadre, readFromFiglio1,index);
  115.  
  116. if(strcmp(readFromPadre,readFromFiglio1)==0)
  117. write(pipe_fd2[pipewrite], readFromPadre, 1);
  118. index++;
  119. }
  120.  
  121. close(pipe_fd2[pipewrite]);
  122. }
  123.  
  124. void padre(char* fileInput) {
  125. close(pipe_fd1[piperead]);
  126. close(pipe_fd2[pipewrite]);
  127.  
  128. int lenght = getFileDimension(fileInput);
  129. for(int i=1; i<lenght+1;i++){
  130. char* c = getNCharFromEndFile(fileInput,i);
  131. write(pipe_fd1[pipewrite], c, 1);
  132. }
  133. close(pipe_fd1[pipewrite]);
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement