c - How to use exec() system call correctly after a fork? -


i'm having issues program want run_uptime function display output of uptime function every 5 seconds stdout. however, don't know i'm doing wrong in code, i've looked through various other questions related exec() , fork() still can't seem find solution. 1 other thing how can rewrite fork call without switch statement, that's more of aesthetic reason still wanna know how it. i'd appreciate help. please explain why though, because i'm newbie.

problem statement, program should do:

you shall create program following:

o upon startup, read total runtime (in seconds) user wants program run for. default value of 10 seconds used if user not include time when launching program (i.e., rohan> a3 30 causes program run 30 seconds, rohan> a3 run program 10 seconds).

o program shall create 3 child processes, busy-wait until child processes complete , exit.

o first child process shall implement clock prints hour, minute, , second once every second (in human readable localtime)

o second child process shall run uptime (/usr/bin/uptime) program once every 5 seconds

o third child process shall implement countdown timer prints minutes , seconds remaining, once every second, until 00:00 reached.

o third child process, upon reaching 00:00, shall notify (signal, or pipe, or ..., etc, choice of ipc mechanism) first , second child process’ (i.e. it's sibling processes) telling them terminate , terminate itself.

o once child processes have terminated, parent shall print friendly message exit.

here code, compiles need different output.

/* include files */ #include <stdio.h> #include <time.h> /* local time functions */ #include <unistd.h> /* fork */ #include <string.h> #include <stdlib.h> #include <sys/wait.h> /* wait function */ #include <sys/types.h> /* pid_t */  /* remember 0 means program finished correctly, unix convention */  /* definitions */ #define time_quantum 5 //default time program run!  /* functions */ void system_clock(void); //function display human readable time. void time_to_kill(int timequota); //prints out time in countdown fashion, till program ends. void run_uptime(void);  /* variables */ int procpipe1[2]; //allocate pipe set 1 int procpipe2[2]; //allocate pipe set 2 char bufkill[12]; //buffer hold kill signal, tell other processes end.       int main(int argc, const char * argv[]) {     int time=time_quantum;     if(argc==2)     {         time=atoi(argv[1]); //looking see if user wants custom running time.     }     //creating pipes before creating child processes, communicate.    pipe(procpipe1);    pipe(procpipe2);     printf("pipes opened, creating child process!\n");      //create child processes , run associated functions in them.     //creating the 1st child process!      switch (fork())     {         case -1:             printf("error: couldn't create 1st child!\n");             exit(1);          case 0: //if no error in creating child process create process!             printf("\nchild 1 process: time_to_kill executing...\n");             system_clock();             exit(1);          default:             break;     }      //creating 2nd child process!     switch (fork())     {         case -1:             printf("error: couldn't create 3rd child!\n");         case 0: //if no error in creating child process create process!             printf("\nchild 2 process: time_to_kill executing...\n");             run_uptime();             exit(1);          default:             break;     }      //creating 3rd child process!     switch (fork())     {         case -1:             printf("error: couldn't create 2nd child!\n");             exit(1);         case 0: //if no error in creating child process create process!             printf("\nchild 3 process: time_to_kill executing...\n");             time_to_kill(time);              exit(1);          default:             break;     }      printf("parent closing pipes\n");      //need close pipes after using them, close communications between processes.     //pipe close error checking      close(procpipe1[0]);     close(procpipe1[1]);     close(procpipe2[0]);     close(procpipe1[1]);      printf("parent waiting children completion...\n");      //wait child processes end before ending main, overall process      //that spawned child processes.      wait(null);     wait(null);     wait(null);      printf("time print out friendly message!\n");     printf("parent process finishing, bye!\n");     exit(exit_success);    }//end main  //this 1st child process void system_clock(void)  {     /* variable of system_clock()*/     //temp buffer termination string     char str1[12];     time_t now;     struct tm *lcltime;      strcpy(str1, "time die"); //store string str1 local variable      close(procpipe1[0]);     close(procpipe1[1]);     close(procpipe2[1]);      //show current time     for(;;)     {         = time ( null );         lcltime = localtime ( &now );          //read message child 2 process         int status = read(procpipe2[0], bufkill, 12);          if ((strcmp(str1, bufkill)) != 0)         {             //printf("read status: %d, buffer= %s\n", status, bufkill);             printf ("the time %d:%d:%d\n", lcltime->tm_hour, lcltime->tm_min, lcltime->tm_sec );             //show ever second             sleep(1);         }           //if end signal came in         //if(ret == 0)         if ((strcmp(str1, bufkill)) == 0)         {             //close reading end             close(procpipe2[0]);              //return parent             printf("child 1 process: system_clock ending...\n");             exit(1);         }     }   }//end system_clock  //this 2nd child process void run_uptime()  {     // variable of run_uptime     //temp buffer termination string     char str1[12];      strcpy(str1, "time die"); //store string str1 local variable      close(procpipe1[0]);     close(procpipe1[1]);     close(procpipe2[1]);          for(;;)     {         //read message child 2 process         int status = read(procpipe2[0], bufkill, 12);          if ((strcmp(str1, bufkill)) != 0)         {             //printf("read status: %d, buffer= %s\n", status, bufkill);             execl("/usr/bin/uptime","uptime",null);             //show ever 5 seconds             sleep(1);         }           //if end signal came in         if ((strcmp(str1, bufkill)) == 0)         {             //close reading end of pipe.             close(procpipe2[0]);              //return parent             printf("child 2 process: run_uptime ending...\n");             exit(1);         }     }   }//end run_uptime  //this 3rd child process void time_to_kill(int timequota) //this process signals other processes stop. {     /* need change signal */     //string signal end process      strcpy(bufkill, "time die");      //pipes aren't being used close them.     close(procpipe1[0]);     close(procpipe1[1]);     close(procpipe2[0]);      for( ;timequota>0; timequota--)     {         printf("count down: 00:0%d\n", timequota);         //send signal child 1 process , child 2 process not end         write(procpipe2[1], "not die yet",12); //write pipe2 statement doesn't signal stopping process.         //count down every second         sleep(1);     }      //send signal child 1 process , child 2 process terminate itself.     write(procpipe2[1], bufkill, sizeof(bufkill)); //write pipe2 kill statement stored in bufkill      //close pipe 2 writing end     close(procpipe2[1]);       //back main     printf("count down: 0\n");     printf("child 3 process: time_to_kill ending...\n");     exit(1); }//end time_to_kill 


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -