Advertisement
Guest User

Untitled

a guest
May 3rd, 2022
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. #include<unistd.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<sys/types.h>
  5. #include<sys/wait.h>
  6.  
  7. #define NUM_THREADS 10
  8. #define BLOCK 200
  9.  
  10. struct thread_arg{
  11. char *filename;
  12. int start;
  13. int end;
  14. };
  15.  
  16. int x = 0;
  17.  
  18. void female_30_50(struct thread_arg *);
  19.  
  20. int main (int argc, char *argv[]){
  21.  
  22. int err_code, i=0, p_id, p[2], counter=0;
  23. char token[1000];
  24.  
  25. struct thread_arg myArg[NUM_THREADS];
  26. pipe(p);
  27.  
  28. while (1) {
  29. printf("In main: creating thread %d\n", i);
  30. myArg[i].filename = argv[1];
  31. myArg[i].start = BLOCK*i+1;
  32. myArg[i].end = BLOCK*(i+1);
  33. p_id = fork();
  34.  
  35. if (p_id < 0){
  36. printf("Cannot create a process.\n");
  37. exit(-1);
  38. }
  39.  
  40. if(p_id==0){
  41. close(p[0]);
  42. dup2(p[1],1);
  43. female_30_50(myArg+i);
  44. return 0;
  45. }
  46. i++;
  47. if(i>=NUM_THREADS) break;
  48.  
  49. }
  50.  
  51. close(p[1]); /* close the fd of the rear end of the pipe */
  52. dup2(p[0],0);
  53.  
  54. while(scanf("%s", token) != EOF){
  55. printf("Parent says %s\n", token); /* add the suffix string */
  56.  
  57. fflush(stdout);
  58. }
  59. wait(NULL);
  60. return 0;
  61.  
  62. }
  63.  
  64. void female_30_50(struct thread_arg *td){
  65.  
  66. long int DATA[4];
  67. char comment[256];
  68. int i, n, m, hid, flag;
  69. n=0;
  70. int start, end,lines =0;
  71.  
  72. FILE *rFile, *wFile;
  73. rFile=fopen(td->filename, "r");
  74. wFile=fopen("output.txt", "w");
  75.  
  76. start = td->start;
  77. end = td->end;
  78.  
  79. fgets(comment, 256, rFile);
  80.  
  81. while(!feof(rFile)){
  82. lines++;
  83. fscanf(rFile, "%ld %ld %ld %ld", DATA, DATA+1, DATA+2, DATA+3);
  84. if(lines >= start && lines <= end){
  85. if(DATA[3] == 2 && DATA[2]>=30 && DATA[2]<=50){
  86. n++;
  87. fprintf(wFile, "%ld", DATA[1]);
  88.  
  89. }
  90. }
  91. }
  92.  
  93. x = x + n;
  94.  
  95. printf("%d\n", n);
  96. fclose(rFile);
  97. fclose(wFile);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement