0

ミニシェルを作成していて、ジョブ制御で問題が発生しました。
メイン関数でデータを取得できず、run_command の下部でもデータを取得できません。
メインの情報を保存して正常に取得する方法を知りたいです。

typedef struct job{
    int num;
    pid_t pid[SIZE];
    char* name[SIZE];
    int occupied;
}
job jobs; <- the global variable

int run_command(........){
.    .......
.    .......
.    result = fork(); // generate child process
.    if ( result == 0 ){ //child process
.    .    if ( 2-pipe function is detected){ // 2 pipe
.    .    .    .......
.    .    .    .......
.    .    .    pid01 = fork(); // fork the first child ( for the first "cat" in case of "cat | cat | cat")
.    .    .    if (pid 01 == 0){
.    .    .        .....
.    .    .    }
.    .    .    else{
.    .    .    .    pid02 = fork(); // fork the second child
.    .    .    .    if (pid02 == 0){
.    .    .    .        .....
.    .    .    .    }
.    .    .    .    else{
.    .    .    .    .    pid03 = fork(); // fork the third child
.    .    .    .    .    if (pid03 == 0){
.    .    .    .    .        ....
.    .    .    .    .    }
.    .    .    .    .    else{
.    .    .    .    .    .     ....
.    .    .    .    .    .     waitpid(pid01,&status1, WUNTRACED);
.    .    .    .    .    .     waitpid(pid02,&status2, WUNTRACED);
.    .    .    .    .    .     waitpid(pid03,&status3, WUNTRACED);
.    .    .    .    .    .     if (WIFSTOPPED(status1)){
.    .    .    .    .    .         jobs.occupied = 1;
.    .    .    .    .    .         jobs.num = 3;
.    .    .    .    .    .         for () {} (a for loop to store the job, i.e. " cat | cat | cat ")
.    .    .    .    .    .         jobs.pid[0] = pid01;
.    .    .    .    .    .     }
.    .    .    .    .    .     if (WIFSTOPPED(status2)){
.    .    .    .    .    .         jobs.pid[1] = pid02;
.    .    .    .    .    .     }
.    .    .    .    .    .     if (WIFSTOPPED(status3)){
.    .    .    .    .    .         jobs.pid[2] = pid03;
.    .    .    .    .    .     }
.    .    .    .    .    }
.    .    .    .    }
.    .    .    }
.    .    }
.    .    exit(-1);
.    }
.    waitpid( result, &status, WUNTRACED);
.    if (WIDSTOPPED(status)){
.    ....
.    }
.    ( cannot retrieve jobs here already, like the variable "jobs" has been initialized again)
.    return 0;
}

int main(){
.....
.....
.....
run_command(........);
(jobs hasn't been modified after I have press ctrl-z)
return 0;
}
4

1 に答える 1

0

変数は、フォークによって作成された親プロセスと子プロセスの間で共有されません。プロセス間で通信する唯一の方法は次のとおりです。

  • Unix ドメイン ソケットを使用してデータを交換する
  • フォークの前にパイプを作成し、それを使用してデータの書き込み/読み取りを行います
  • プロセスの終了ステータスの使用 (データ サイズの制限)
  • シグナルの使用 (非常に制限されたデータ サイズ、実際には SIGUSR1 と SIGUSR2 のみが安全に使用できます)
于 2013-10-14T12:54:44.260 に答える