- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想知道是否有办法暂停进程但继续执行程序。
但是当我点击 P4
时,它结束,消失,然后 P2 创建 P5。然后 P2 死了,P1 创建了 P3,同样的事情也发生在那里。
我需要在进程开始死亡之前创建整个“树”。
不能使用 wait
或 waitpid()
因为它只针对它的儿子。
有没有办法暂停 P4 并从它的父亲继续?
如果我不能,我怎么能实现我的目标?
我当前的代码:它按以下顺序创建:
P1 -> P2 -> P4 -> P4 DIES -> P5 -> P5 DIES -> P2 DIES -> P3 -> P6 -> P6 DIES -> P7 -> P7 DIES -> P3 DIES -> P1 DIES
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <time.h>
int main(){
clock_t t;
double time_taken;
t = clock();
int status;
pid_t idProcesso, pai;
printf("P1 created: %d\n", getpid());
idProcesso = fork();
switch(idProcesso)
{
case -1: exit(1);
case 0: //P2
printf("P2 created: %d - son of P1: %d\n", getpid(), getppid());
idProcesso = fork();
switch(idProcesso)
{
case -1: exit(1);
case 0: //P4
printf("P4 created: %d - son of P2: %d\n", getpid(), getppid());
sleep(1);
break;
default://P2
idProcesso = fork();
switch(idProcesso)
{
case -1: exit(1);
case 0://P5
printf("P5 created: %d - son of P2: %d\n", getpid(), getppid());
break;
default://P2
sleep(1);
break;
}
break;
}
break;
default:
idProcesso = fork();
switch(idProcesso)
{
case -1: exit(1);
case 0://P3
printf("P3 created: %d - son of P1: %d\n", getpid(), getppid());
idProcesso = fork();
switch(idProcesso)
{
case -1: exit(1);
case 0://P6
printf("P6 created: %d - son of P3: %d\n", getpid(), getppid());
sleep(1);
break;
default://P3
idProcesso = fork();
switch(idProcesso)
{
case -1: exit(1);
case 0://P7
printf("P7 created: %d - son of P3: %d\n", getpid(), getppid());
break;
default://P3
break;
}
sleep(1);
break;
}
break;
default:
sleep(4);
break;
}
break;
}
printf("Process id: %d terminated\n", getpid());
exit(0);
}
最佳答案
进程彼此独立运行,因此您只需要:
让您的子进程保持事件状态足够长的时间,以便创建所有进程,这很容易使用 sleep()
来完成,例如;和
在创建所有子进程之前,不要为任何子进程启动 wait()
ing。
这是一个例子:
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void)
{
pid_t pids[8] = {0};
printf("P1 created (%lu).\n", (unsigned long) getpid());
for ( size_t i = 1; i < 3; ++i ) {
pids[i] = fork();
if ( pids[i] == -1 ) {
perror("fork() error");
exit(EXIT_FAILURE);
}
else if ( pids[i] == 0 ) {
printf("P%zu created (%lu).\n", i + 1, (unsigned long) getpid());
for ( size_t j = 1 + i * 2; j < 3 + i * 2; ++j ) {
pids[j] = fork();
if ( pids[j] == -1 ) {
perror("fork() error");
exit(EXIT_FAILURE);
}
else if ( pids[j] == 0 ) {
printf("P%zu created (%lu).\n", j + 1,
(unsigned long) getpid());
sleep(8 - j);
printf("P%zu exiting.\n", j + 1);
exit(EXIT_SUCCESS);
}
}
for ( size_t j = 2 + i * 2; j >= 1 + i * 2; --j ) {
if ( waitpid(pids[j], NULL, 0) == -1 ) {
perror("waitpid() error");
exit(EXIT_FAILURE);
}
printf("Waited for P%zu (%lu).\n", j + 1,
(unsigned long) pids[j]);
}
printf("P%zu exiting.\n", i + 1);
exit(EXIT_SUCCESS);
}
}
for ( size_t i = 2; i > 0; --i ) {
if ( waitpid(pids[i], NULL, 0) == -1 ) {
perror("waitpid() error");
exit(EXIT_FAILURE);
}
printf("Waited for P%zu (%lu).\n", i + 1, (unsigned long) pids[i]);
}
printf("P1 exiting.\n");
return 0;
}
输出:
paul@horus:~/src/sandbox$ ./procs
P1 created (27206).
P2 created (27207).
P3 created (27208).
P4 created (27209).
P5 created (27210).
P6 created (27211).
P7 created (27212).
P7 exiting.
Waited for P7 (27212).
P6 exiting.
Waited for P6 (27211).
P3 exiting.
Waited for P3 (27208).
P5 exiting.
Waited for P5 (27210).
P4 exiting.
Waited for P4 (27209).
P2 exiting.
Waited for P2 (27207).
P1 exiting.
paul@horus:~/src/sandbox$
请注意,进程运行的顺序本质上是不可预测的,因此每次运行它时,您可能会得到与上述略有不同的结果。我没有尝试让它们按顺序创建或退出,除了在四叶进程中对 sleep()
进行半心半意的尝试,否则它们只是为了让它们都存活足够长的时间.您几乎可以通过对 sleep()
的战略调用来保证执行和终止顺序,或者使用某种形式的进程间通信来保证它。作为一般规则,您不应该关心这个顺序,只关心您的实际工作是否按顺序完成。
但是,它满足了在任何进程开始死亡之前保持所有进程存活的标准。
关于c - 如何暂停进程并继续程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36947926/
我正在我的应用程序后台下载视频。如果用户在下载过程中重启了应用/设备,有什么方法可以在他们下次启动应用时从他们中断的地方继续下载? 最佳答案 这主要取决于文件服务器的配置(HTTP、FTP 等)。 现
我正在试验 WPF 动画,但有点卡住了。这是我需要做的: 鼠标悬停: 淡入(2 秒内从 0% 到 100% 不透明度) MouseOut: 暂停 2 秒 淡出(2 秒内从 100% 到 0% 不透明度
我的问题是这个线程的延续: Ant: copy the same fileset to multiple places 我是映射器的新手。有人(carej?)可以分享一个使用映射器来做到这一点的例子吗
继续previous question我希望能够显示一些事件指示器即使主线程被阻塞。(基于this article)。 基于所附代码的问题: 使用 Synchronize(PaintTargetWin
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
我有一个场景,其中有一个线程在等待和执行任务之间循环。但是,我想中断线程的等待(如果愿意,可以跳过其余的等待)并继续执行任务。 有人知道如何做到这一点吗? 最佳答案 我认为你需要的是实现 wait()
这是我的代码架构: while (..) { for (...; ...;...) for(...;...;...) if ( )
import java.util.Scanner; public class InteractiveRectangle { public static void main(String[] args)
如何将 continue 放入具有函数的列表理解中? 下面的示例代码... import pandas as pd l = list(pd.Series([1,3,5,0,6,8])) def inv
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this qu
我正在用 python 开发一个程序,遇到了一个我不知道如何解决的问题。我的意图是使用 with 语句,避免使用 try/except。 到目前为止,我的想法是能够使用 continue 语句,就像在
我对下一段代码的执行感到困惑: label: for (int i = 0; i < 100; i++) { if (i % 2 == 0) c
这很好用: #include int main(){ volatile int abort_counter = 0; volatile int i = 0; while (i
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
如果不满足某些条件,我会尝试跳到循环的下一次迭代。问题是循环仍在继续。 我哪里出错了? 根据第一条评论更新了代码示例。 foreach ($this->routes as $route =>
如果不满足某些条件,我会尝试跳到循环的下一次迭代。问题是循环仍在继续。 我哪里出错了? 根据第一条评论更新了代码示例。 foreach ($this->routes as $route =>
Android项目中的一个需求:通过线程读取文件内容,并且可以控制线程的开始、暂停、继续,来控制读文件。在此记录下。 直接在主线程中,通过wait、notify、notifyAll去控制读文件的线
link text 我得到了引用计数的概念 所以当我执行“del astrd”时,引用计数降为零并且 astrd 被 gc 收集? 这是示例代码。这些代码是我在昨天的问题之后开发的:link text
我想首先检查我的 Range 是否有 #NA 错误,然后在退出宏之前显示包含错误的单元格地址。这是我到目前为止所做的。 现在,如果出现错误,我想显示 MsgBox警告用户错误并停止程序的其余部分执行,
while( (c = fgetc(stdin)) != EOF ){ count++; if (count == lineLen - 1){ moreChars =
我是一名优秀的程序员,十分优秀!