2016-12-16 7 views
2

私は非常に単純なプログラムをforkして別の問題を呼び出すようにしました。私がしたいことがあれば、バグが発生し、forループの倍の時間が発生します。 main.cppに子を生成し、exec - 未定義の振る舞い

`#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 = '\0' 
    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 
[email protected]$ 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 

だから私はここに誤解です:ここでは、コードのですか?

+0

C++を学んでいるならば、このコードの半分はUBか標準準拠ではありません。Cを忘れていれば、Cを忘れてしまいます。チューターの要件でC++の_features_を使用しない場合は、代替案を検討し、それを念頭に置くことを検討する。例えば、 – Swift

+0

。 stdlib.hを含むUBですが、あなたは既に右のヘッダcstdlibを含んでいます。 string.hを含めても間違っています。 _global_ scopeで 'namespace std'を使用すると、このスニペットではありませんが、可能なUBにつながる可能性があります。ローカルスコープで使用したり、' std :: cin'を使用するなど必要な名前を使用することができます。もはやSTLはありません。STLは歴史的な理由から利用できる別のライブラリです.iostreamを使用している場合、既にC++テンプレートライブラリ(STLではなく)を使用しています。 C++コードでmalloc \ free \ reallocを使用しないで、newとdeleteを使用してください。 – Swift

答えて

1

ここで誤解されているのは、最新のC++コードを書く一般的な原則です。これらのひどい見た目のCスタイルの動的メモリ割り当てを使用する理由はまったくありません。ここで行われたすべてのことは、コンテナを使用してより洗練された作業を行うことができ、結果コードは少なくとも3倍小さくなります。

ああ、execvのパラメータ配列は、NULLポインタで終了する必要があります。そうでないので、結果として未定義の動作になります。ほとんどの場合、execvシステムコールは、このガベージパラメータのために失敗します。おそらく、マニュアルページの閲覧に応じてEFAULTとなる可能性があります。

したがって、execv()は実際には子プロセスで返されます。示されたコードは戻り値をチェックすることができないので、子プロセスはランダムに子プロセスでexecv()から返されて実行を継続し、子プロセスでmain()に戻り、自身のメリーゴーラウンドの外側のforループしたがって、出力が重複します。

+0

STLは許可されていないので、私は動的execを使いたいので、execlを使うことができないので、このようにしてしまったのです。 –