Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <ctype.h>
  9.  
  10. int main(int argc, char **argv)
  11. {
  12.  
  13. //cmp - functie ca strcmp
  14.  
  15.  
  16. char n,temp[500],c;
  17. int fd,arr[500],sum=0,k=0,rez,TOTAL_NO=0,j=0;
  18.  
  19. if(argc!=2)
  20. {printf("Usage %s arg1, arg2",argv[0]);
  21. exit(1);}
  22.  
  23. if((fd=open(argv[1],O_RDONLY))<0)
  24. {
  25. printf("Error at opening");
  26. exit(2);}
  27.  
  28. while((n=read(fd,&c,1))>0)
  29. {
  30.  
  31. if(isdigit(c))
  32. {
  33. temp[j]=c;
  34. j++;
  35. }
  36. else
  37. {
  38. arr[TOTAL_NO]=atoi(temp);
  39. TOTAL_NO++;
  40. memset(temp,0,j);
  41. j=0;
  42. }
  43. }
  44.  
  45. if(n<0)
  46. {
  47. printf("Error reading from file");
  48. exit(3);}
  49.  
  50.  
  51. pid_t pid1,pid2;
  52.  
  53. if((pid1==fork())<0)
  54. printf("error pid1\n");
  55. else if(pid1==0)
  56. {
  57. for(j=0;j<TOTAL_NO;j++)
  58. {
  59. sum=sum+arr[j];
  60. }
  61. printf("Avg of file numbers is: %d\n",sum/TOTAL_NO);
  62. }
  63. else if((pid2=fork())<0)
  64. printf("error pid2\n");
  65. else if(pid2==0)
  66. {
  67. qsort(arr,TOTAL_NO,sizeof(int),comp);
  68. for(i=0;i<TOTAL_NO;i++)
  69. printf("Elem %d is %d\n",i,arr[i]);
  70. }
  71.  
  72. wait(&status);
  73.  
  74. close(fd);
  75.  
  76.  
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement