C Sorting of String arrays with exec() and fork (); -
could please explain me how for() , exec() works calling system calls sort? i've been reading around, still quite confused. data stored in .txt file looks like:
00-11-d9-20-aa-4e 951 cc-3a-61-df-4b-61 259 84-1b-5e-a8-bf-7f 82 74-e2-f5-17-96-89 829 84-1b-5e-a8-bf-7c 56 the space in center tab delimited. i'm trying sort out values based on second field decreasing exec() , sort(). no idea how to. i've called fork() complete sorting in separate process. then, returning parent print out results console.
int pid; file *data; char line[linesize]; data = fopen("results.txt", "r"); switch(pid = fork()){ case -1: perror("fork()"); exit(1); break; case 0: break; default: sleep(3); while(fgets(line, sizeof line, data) != null){ printf("%s", line); } printf("arrived @ parent"); break; } fflush(stdout); exit(exit_success);
i've finished it,you can have try.
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<fcntl.h> #define linesize 1024 int main() { int pid; file *data; int fd; char line[linesize]; data = fopen("results.txt","r"); switch(pid=fork()) { case -1: perror("fork()"); exit(1); break; case 0: fd = open("results.txt",o_wronly); dup2(fd,1); //output redirection stdout results.txt execl("/usr/bin/sort",".","a.txt\0",null); break; default: sleep(3); while(fgets(line,sizeof(line),data) != null) { printf("%s",line); } printf("arrived @ parent"); break; } return 0; }
Comments
Post a Comment