Hey guys,
I'm trying to start a process which executes the uniq command to every string in a string array. this already works good. But I have to wait for execlp in parent, because the main program exits before uniq command finishes. How can I achieve this?
Code:
static void uniq_process(void) { pid_t pid;
int fd[2];
if (pipe(fd) < 0) {
bail_out("Error creating pipe");
}
pid = fork();
/* child process */
if (pid == 0) {
close(fd[1]);
close(STDIN_FILENO);
dup(fd[0]);
(void) execlp("uniq", "uniq", "-d", (char*) 0 );
bail_out("execlp failed");
}
/* parent process */
else if (pid > 0) {
FILE *file = fdopen(fd[1] , "w");
close(fd[0]);
for (int i = 0; i < array_size; i++) {
(void) fprintf(file, "%s", output_array[i]);
}
(void) fclose(file);
} else {
bail_out("Fork error");
}
}
Thanks for your help and time.