C++ Kill child processes, but not parent, and uptime through exec function -


i don't know if current way killing child processes correct. want when child three's timer (temp) hit's 0 kills children, parent prints message exits. also, tried using sysinfo uptime in child 2, sysinfo struct doesn't have uptime member in environment running program in. so, how use exec function uptime /usr/bin/uptime. also, don't tell me split out of main function because going after figure out how complete functionality.

the code:

#include <string> #include <ctime> #include <iostream> #include <sys/sysinfo.h> #include <cstdlib> #include <unistd.h> #include <sched.h> #include <assert.h> #include <stdio.h> #include <stdlib.h> #include <signal.h> #include <errno.h>  using namespace std;  pid_t parent_pid;  void sigquit_handler (int sig) { assert(sig == sigquit); pid_t self = getpid(); if (parent_pid != self) _exit(0); }  int main (string input) { if (input.empty()) {     input = "10"; } signal(sigquit, sigquit_handler); parent_pid = getpid(); int number = atoi(input.c_str()); pid_t pid; int i; file* fp; double uptime;  for(i = 0; < 3; i++) {     pid = fork();     if(pid < 0) {         printf("error");         exit(1);     }     else if (pid == 0) {         if (i == 0) { //child 1             sleep(1);             time_t t = time(0);   // time             struct tm * = localtime( & t );             cout << (now->tm_year + 1900) << '-'                     << (now->tm_mon + 1) << '-'                     <<  now->tm_mday                     << endl;         }         else if (i == 1) { //child 2             sleep(5);             struct sysinfo info;             cout << info.uptime << endl;         }         else { //child 3             int temp = number;             int minute = temp / 60;             int second = temp - (minute * 60);             sleep(1);             cout << minute << ":" << second << endl;             temp--;             if (temp == 0) {                 kill(-parent_pid, sigquit);             }         }     }     else  {             cout << "program complete";     } } } 

also, tried using sysinfo uptime in child 2, sysinfo struct doesn't have uptime member in environment running program in. so, how use exec function uptime /usr/bin/uptime

you can set alarm in process , receive sigalrm when time has run out.


i want when child three's timer (temp) hit's 0 kills children, parent prints message exits.

you may make parent process group leader first call setpgrp, can send signal process in group processes in same group (the parent , children) kill(0, sighup). parent process must install signal handler sighup able survive receiving sighup or whatever other signal may use purpose.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -