- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我不知道我目前杀死子进程的方式是否正确。我希望当 child 三的计时器(临时)命中为零时它会杀死所有 child ,然后 parent 打印一条消息而不是退出。另外,我尝试使用 sysinfo 来获取 child 2 的正常运行时间,但是 sysinfo 结构在我运行程序的环境中没有作为成员的正常运行时间。那么,我如何使用 exec 函数从/获取正常运行时间usr/bin/正常运行时间。另外,不要告诉我将所有内容都从主要功能中拆分出来,因为我会在弄清楚如何完成该功能后才这样做。
代码:
#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
你可以设置一个 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.
您可能希望先通过调用 setpgrp
使父进程成为组长,这样您就可以使用 kill(0, SIGHUP)
从组中的任何进程向同一组(父进程及其子进程)中的所有进程发送信号。父进程必须为 SIGHUP
安装一个信号处理程序,以便能够在接收到 SIGHUP
或您可能希望用于此目的的任何其他信号时继续存在。
关于C++通过exec函数杀死子进程,但不杀死父进程和正常运行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24456693/
考虑以下示例代码: void main() { List array = []; for (int i = 0; i newList = array.where( (value) => v
我在java中实现了textrank,但它看起来很慢。有谁知道它的预期性能吗? 如果预计速度不会很慢,则可能是以下任一问题: 1) 似乎没有办法在 JGraphT 时间内创建一条边并同时为其添加权重,
我正在尝试提交解决方案(使用一些具有编译时间限制的在线编译器)来对数组进行排序 - 这是我的代码片段 - class TSORT { public static void main(Strin
只是一个困惑......部分C++示例代码如下 我只是重新编辑了整个帖子。抱歉造成任何混淆 int i, j; i = 0; // c1 j = 0; // c2 while (i
我正在使用 OpenMP 运行一些并行工作,但发现加速无法很好地扩展。然后我发现随着线程数量的增加,完成相同工作量的时间也会增加。这是示例: void fillingVec(vector& vec)
我目前正在分析 maximum subarray problem 用于强力算法和分而治之算法(递归)。 使用蛮力算法,最坏情况下的运行时间为 O(n^2)。使用递归算法,最坏情况下的运行时间为 O(n
如果我有两个数组都有 10000 个项目,现在我想将它们合并到一个数组中,所以我用 concat 来完成: array1=array1.concat(array2); 但是有人知道运行时间是多少吗?
{ clock_t t1, t2; double time1 = 0; t1 = clock(); bubbleSort(data, n); t2 = cloc
我在某处读到有人可以在运行时访问配置值,但不能在设计时访问。在这种情况下,运行时和设计时有什么区别? 最佳答案 设计时间是有人用愉快的“看起来不错!”在我们的 Word 文档和 UML 图表上签字的时
我正在比较我们一个项目的两个分支的性能,一个比另一个慢得多。我注意到其中一个的 GC 运行计数更高(见下图)。 更有趣的是,运行时间要长很多倍,远远超过额外运行所能解释的时间。什么可以解释运行次数增加
我想以可读的方式格式化 log4j 经过的运行时间,%r 参数: [00:36:25.844] 和 [01 13:35:25.844] [时:分:ss.SSS] ... [dd 时:分:ss.SSS]
如何计算此代码的大 O 运行时效率?我的直觉告诉我它是 O(n^3),但我不确定,因为我不确定循环是独立的还是相关的。 for (i=1; i<=n; i++) for (j=1; j<=n;
确定这些不同循环的大 O 运行时间? for i = 1 to n { ... for j = 1 to 2*i { ... k = j; while (k>=0)
运行时间、复杂性、编译时间和执行时间有什么区别? 运行时间与时间复杂度有冲突,执行时间和执行时间有什么区别? 最佳答案 您真正需要的是如何将大O时间复杂度转换为运行时。这不像一开始看起来那么容易。 因
斯内普的《Unfriendly Algorithms for Wizards》教科书声称合并的运行时间排序是 O(n^4)。这种说法是否正确? 解决方案:是的。这个说法在技术上是正确的,因为 O(n^
我有一系列问题需要反馈和答案。我会评论我的想法,这不是家庭作业而是准备为了我的考试。 我的主要问题是确定不同情况下循环的迭代。试图弄清楚这一点会如何? 评估运行时间。 Q2。 for(int i =
我试图找到一个合理的机会来测量我的 java 程序/程序部分的速度,例如测试两种方法中哪一种执行得更快。注释掉这两种方法中的一种,确定运行时间,最后比较时间。我不想在我的程序中使用任何代码,例如: p
好吧,这些都是非常简单的方法,而且有几个,所以当它们都是同一件事时,我不想只创建多个问题。 BigO 是我的弱点。我只是想不通他们是如何得出这些答案的。无论如何,您是否可以让我深入了解您对分析其中一些
如何计算此给定代码的 Theta 运行时间: void f(int n) { for (int i=3; i
使用 leiningen 创建 uberjar 并使用 java -jar foo-uberjar.jar 运行该 jar 后程序运行良好,最后一行代码执行得相当快,但程序在关闭前挂了大约一分钟。这是
我是一名优秀的程序员,十分优秀!