如何暂停一个过程并继续该程序?

时间:2016-04-29 21:46:48

标签: c unix process signals parent-child

我想知道是否有办法暂停进程但是继续该程序。

我需要创建这样的流程:
enter image description here

但当我点击P4它结束,死亡然后P2创建P5。然后P2 Dies和P1创建P3,同样的事情发生在那里。

我需要在流程开始死之前创建整个'树' 不能使用waitwaitpid()因为它只针对它的儿子。

有没有办法暂停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);
}

1 个答案:

答案 0 :(得分:1)

流程彼此独立运行,因此您只需:

  1. 让您的子进程保持活动足够长的时间,以便创建所有进程,例如,使用sleep()就足够了。以及

  2. 在您创建所有子流程之前,不要wait()开始#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; }

  3. 以下是一个例子:

    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()进行半心半意的尝试,否则只是为了让它们保持足够长的时间。通过对class queryFactory(object): def __init__(self, request, sql,params): self.request=request self.sql=sql self.params=params def run_it(self): def getQuery(): return dbpool.runQuery(self.sql, self.params) def onResult(data): self.request.write("done") self.request.finish() d = getQuery() d.addCallback(onResult) return NOT_DONE_YET 的战略调用,您几乎可以保证执行和终止顺序,或者使用某种形式的进程间通信来保证它。作为一般规则,您不应该关心此订单,只关心您的实际工作是否按顺序完成。

    但是,它符合在所有进程开始死亡之前保持所有进程存活的标准。