Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <ctype.h>
- int main(int argc, char **argv)
- {
- //cmp - functie ca strcmp
- char n,temp[500],c;
- int fd,arr[500],sum=0,k=0,rez,TOTAL_NO=0,j=0;
- if(argc!=2)
- {printf("Usage %s arg1, arg2",argv[0]);
- exit(1);}
- if((fd=open(argv[1],O_RDONLY))<0)
- {
- printf("Error at opening");
- exit(2);}
- while((n=read(fd,&c,1))>0)
- {
- if(isdigit(c))
- {
- temp[j]=c;
- j++;
- }
- else
- {
- arr[TOTAL_NO]=atoi(temp);
- TOTAL_NO++;
- memset(temp,0,j);
- j=0;
- }
- }
- if(n<0)
- {
- printf("Error reading from file");
- exit(3);}
- pid_t pid1,pid2;
- if((pid1==fork())<0)
- printf("error pid1\n");
- else if(pid1==0)
- {
- for(j=0;j<TOTAL_NO;j++)
- {
- sum=sum+arr[j];
- }
- printf("Avg of file numbers is: %d\n",sum/TOTAL_NO);
- }
- else if((pid2=fork())<0)
- printf("error pid2\n");
- else if(pid2==0)
- {
- qsort(arr,TOTAL_NO,sizeof(int),comp);
- for(i=0;i<TOTAL_NO;i++)
- printf("Elem %d is %d\n",i,arr[i]);
- }
- wait(&status);
- close(fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement