Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取运行时间和管道的C++执行命令_C++_Exec_Pipe_Uptime - Fatal编程技术网

获取运行时间和管道的C++执行命令

获取运行时间和管道的C++执行命令,c++,exec,pipe,uptime,C++,Exec,Pipe,Uptime,如何使用exec命令从/usr/bin/uptime每隔5秒显示一次正常运行时间?我如何让我的3个孩子通过管道向家长写信,然后在家长中向标准输出写信 到目前为止,我的项目有: #include <string> #include <ctime> #include <iostream> #include <sys/sysinfo.h> #include <cstdlib> #include <unistd.h> #includ

如何使用exec命令从/usr/bin/uptime每隔5秒显示一次正常运行时间?我如何让我的3个孩子通过管道向家长写信,然后在家长中向标准输出写信

到目前为止,我的项目有:

#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";
    }
}
 }

你试过代码了吗?显示到目前为止您所获得的。我已尝试让exec使用sysinfo,但它在我所在的环境中不起作用。因为正常运行时间不是其中的成员