简体   繁体   English

C ++通过exec函数杀死子进程(而不是父进程)和正常运行时间

[英]C++ Kill child processes, but not parent, and uptime through exec function

I don't know if the current way I am killing the child processes is correct. 我不知道我目前杀死子进程的方式是否正确。 I want when child three's timer (temp) hit's zero that it kills all the children, then the parent prints a message than exits. 我希望当孩子三的计时器(温度)达到零时,它杀死所有孩子,然后父母打印一条消息,然后退出。 Also, I tried using sysinfo to get the uptime in child 2, but the sysinfo struct doesn't have uptime as a member in the environment I am running the program in. So, how do I use a exec function to get uptime from /usr/bin/uptime. 另外,我尝试使用sysinfo获取子项2的正常运行时间,但是sysinfo结构在运行程序的环境中没有正常运行时间。因此,如何使用exec函数从/获得正常运行时间USR /斌/正常运行时间。 Also, don't tell me to split everything up out of the main function because I am going to do that after I figure out how to complete the 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; i < 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);   // get time now
            struct tm * now = 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 is Complete";
    }
}
}

Also, I tried using sysinfo to get the uptime in child 2, but the sysinfo struct doesn't have uptime as a member in the environment I am running the program in. So, how do I use a exec function to get uptime from /usr/bin/uptime 另外,我尝试使用sysinfo获取子项2的正常运行时间,但是sysinfo结构在运行程序的环境中没有正常运行时间。因此,如何使用exec函数从/中获取正常运行时间? USR /斌/运行时间

You can just set an alarm in a process and receive SIGALRM when the time has run out. 您可以在过程中设置alarm ,并在时间用完时接收SIGALRM


I want when child three's timer (temp) hit's zero that it kills all the children, then the parent prints a message than exits. 我希望当孩子三的计时器(温度)达到零时,它杀死所有孩子,然后父母打印一条消息,然后退出。

You may like to make the parent process a group leader first with a call to setpgrp , so that you can send a signal from any process in the group to all processes in the same group (the parent and its children) with kill(0, SIGHUP) . 您可能希望先通过调用setpgrp来使父进程成为组长,以便可以使用kill(0, SIGHUP)从组中任何进程向同一个组中的所有进程(父级及其子级)发送信号kill(0, SIGHUP) The parent process must install a signal handler for SIGHUP to be able to survive receiving SIGHUP or whatever other signal you may like to use for this purpose. 父进程必须为SIGHUP安装信号处理程序,以便能够继续接收SIGHUP或您可能希望用于此目的的任何其他信号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM