linux - creating two child processes in C and execute them one after the other -
i have 2 programs program_a , program_b, called c code. program_b has executed after program_a finishes work.
i tried use fork() , execl() accomplish goal, seems program_b wont executed @ all.. can 1 give me correct structure on in order should create child processes?
you need wait
first child finish, fork
, execl
next child.
#include <stdio.h> #include <stdlib.h> #include <unistd.h> void die(char *msg) { fprintf(stderr, "error: %s\n", msg); exit(exit_failure); } int main(void) { pid_t pid = fork(); if (pid < 0) die("fork a"); if (pid == 0) { // child execl("./program_a", "program_a", (char*)0); die("exec a"); } else { // parent wait(null); pid = fork(); if (pid < 0) die("fork b"); if (pid == 0) { // child execl("./program_b", "program_b", (char*)0); die("exec b"); } else { // parent wait(null); printf("done.\n"); } } return 0; }
Comments
Post a Comment