产卵儿童和执行 - 未定义的行为

Spawning children and exec - undefined behavior

我做了一个非常简单的程序,调用 forking 并调用另一个 problem.While 它做了我想做的事,出现了一个错误,并且 cout 出现了两倍于 for 循环的次数。这是代码: main.cpp

`#include <iostream>
#include <cstdlib>

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/ipc.h>

using namespace std;

char *Strduplicate(const char *source) {
    char *dest = (char *) malloc(strlen(source) + 1); // +1 = '[=10=]'
    if (dest == NULL)
        return NULL;
    strcpy(dest, source);
    return dest;
}

string Get_cwd(string word) {
    char cwd[256];
    getcwd(cwd, sizeof(cwd));
    strcat(cwd,"/");
    strcat(cwd,word.c_str());
    string returnVal(cwd);
    return returnVal;
}

void Call_exec(const char *name,int value) {
    char *exec_array[3];
    exec_array[0] = Strduplicate(name);
    exec_array[1] = (char *)malloc(2);
    exec_array[1] = (char *)"-m";
    asprintf(&(exec_array[2]), "%d", value);
    for (int i = 0 ; i < 3; i++)
        cout << exec_array[i] << " ";
    cout << endl;
    execv(exec_array[0],exec_array);
}

int main(int argc ,char **argv) {
    srand(time(NULL));
    /* Getting arguments */
    //........
    /* Spawning children */
    for (int i = 0 ; i < 3 ; i++ ) {
        int value = rand()%100 + 1;
        pid_t waiterpid = fork();
        if (waiterpid < 0)
            cout << "ERROR FORK" << endl;
        else if (!waiterpid) {
            string program_name = Get_cwd("child");
            Call_exec(program_name.c_str(),value);
        }
    }
    return EXIT_SUCCESS;
}
`

另一个进程是child.cpp

    #include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

using namespace std;

int main(int argc ,char **argv) {
    cout << "Child #" << getpid() << " has started" << endl;
    int value;
    /* Getting arguments */
    if (argc != 3) {
        cerr << "ERROR : Wrong arguments" << endl;
        exit(EXIT_FAILURE);
    }
    else {
        if (strncmp(argv[1],"-m",2) == 0)
            value = atoi(argv[2]);
    }
    cout << "Child has " << value << endl;
    return EXIT_SUCCESS;
}

输出是

mypath/child -m 31 
mypath/child -m 23 
mypath/child -m 48 
mypath/child -m 23 
mypath/child -m 48 
alex@alex$ Child #13063 has started
Child #13062 has started
Child has 48
Child has 23
mypath/child -m 48 
Child #13064 has started
Child has 48

所以我误解了什么?

这里误解的是编写现代 C++ 代码的一般原则。没有任何理由使用这些看起来很糟糕的 C 风格动态内存分配。使用容器,这里所做的一切都可以做得更干净,生成的代码至少要小三倍。

哦,而且execv的参数数组必须以NULL指针结束。它不是,所以这会导致未定义的行为。由于这个垃圾参数,execv 系统调用很可能会失败 - 根据我对其手册页的细读,很可能带有 EFAULT

因此,execv() 实际上是 return 在子进程中。由于显示的代码无法检查其 return 值:子进程将随机地在 returning 从 execv()、returning 到 [=16] 时继续执行=] 在子进程中,并继续其外部 for 循环的旋转木马,因此导致重复输出。