我有以下代码:

int main(int argc, char **argv)
{
    char    *program;
    char     stringa[1000] = "";
    int num = 123;
    char snum[10];

    if (argc != 2) {
        printf("Usage: mon fileName\n where fileName is an executable file.\n");
        exit(-1);
    } else {
        program = argv[1];
        sprintf(stringa, "./%s", program);

        pid_t pid = fork();
        if (pid < 0 ) {
            perror("fork failed.");
            exit(1); }

        else if (pid == 0) {
            char* args[] = {stringa, NULL};
            execv(args[0], args);
        }
        else {

            char procmon_str[] = "./procmon";
            num = pid;
            sprintf(snum, "%d",num);
            pid_t pid2 = fork();
            if (pid2 == 0) {
                char* args2[] = {procmon_str, snum, NULL};
                execv(args2[0], args2); }
            else {

                printf("PID of child is %s", snum);
                int parent_pid = getpid();
                printf("PID of parent is %d", parent_pid);}

        }}
    wait(NULL);

    return 0;
}

该程序的名称是myProgram。我在shell中提供的参数是:
./myProgram calc
calc是我要使用myProgram启动的另一个程序。
然后myProgram执行calc,获取其PID并将该PID传递给另一个名为procmon的程序,该程序可以对其进行处理。这就是为什么我需要 fork 两次。
但是,当我运行上面的代码时,我得到:
procmon: cannot open /proc/6225/stat, the monitored process is not running anymore

我怎样才能解决这个问题?

calc是做什么的?
它进入for循环,递增int变量,然后进入休眠状态3秒钟,然后重复10次。因此,它应该运行约30秒。

procmon是做什么的? procmon仅接收进程的PID作为参数,并显示相应的/proc/PID/stat文件。当您自己运行它时,它可以完美运行。

最佳答案

您有比赛条件。您无法保证第一个fork()实际上甚至会在calc子进程完成执行并退出之前返回到父进程。您需要同步流程的执行。

关于在何处阻止和发出信号的ETA建议

if (pid == 0)
{
    // block here waiting for the go-ahead from parent
    char* args[] = { stringa, NULL };
    execv( args[ 0 ], args );
}

......

else
{
    // signal calc child here
    printf( "PID of child is %s", snum );
    int parent_pid = getpid();
    printf( "PID of parent is %d", parent_pid );
}

学习者如何通过进程间通信来阻止和发信号,这是发问者的一项练习。

09-09 23:12