2017-03-04 10 views
-1

Unixクラスの場合、fork(),pipe(),exec()を使用して、|パイプリダイレクション機能を再作成する必要があります。私はそうすることができますが、今度はループ全体でプログラム全体を実装して、ユーザーがいったんやり直せば、もう一度やり直すことができます。プログラムの最初に戻るにはどうすればいいですか?問題は、exec()システムコールが現在実行中の実行可能ファイルを完全に置き換えることです。自分のプログラムでmain()に戻るにはどうすればいいですか?exec()システムコールを使用した後でmain()にループバックするにはどうすればよいですか?

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


using std::cout; 
using std::cin; 
using std::endl; 
using std::string; 

int main (int argc, char* argv[]) { 

    char command1[127]; 
    char command2[127]; 

    char* token1[5]; 
    char* token2[5]; 

    int pipefd[2], 
     i = 0; 

    pid_t rs, fs; 

    // Get two commands from users. 

    cout << "Enter command 1 along with any arguments: "; 
    cin.getline(command1, 127); 

    string comString1 = command1; 
    if (comString1 == "quit") 
     exit(1); 

    cout << "Enter command 2 along with any arguments: "; 
    cin.getline(command2, 127); 

    string comString2 = command2; 
    if (comString2 == "quit") 
     exit(1); 

    // Parse both commands into tokens. 

    token1[i] = strtok(command1, " "); 
    while (token1[i] != NULL) { 
     i++; 
     token1[i] = strtok(NULL, " "); 
    } 

    i = 0; 

    token2[i] = strtok(command2, " "); 
    while (token2[i] != NULL) { 
     i++; 
     token2[i] = strtok(NULL, " "); 
    } 

    // Create pipe. 

    rs = pipe(pipefd); 

    if (rs < 0) { 
     perror("pipe"); 
     exit(1); 
    } 

    // First fork. There are now two processes. 

    rs = fork(); 

    if (rs < 0) { 
     perror("fork"); 
     exit(1); 
    } 

    if (rs == 0) { // Child 
     close(pipefd[1]); 

     close(0); 

     dup(pipefd[0]); 

     close(pipefd[0]); 

     // Second fork. There are now three processes. 

     fs = fork(); 

     // Trouble begins here I reckon. I forked once and forked again in a child. That may be an issue here. 

     if (fs == 0) { Child 
      rs = execlp(token2[0], token2[0], token2[1], token2[2], token2[3], token2[4], NULL); 
      if (rs < 0) { 
       perror("execlp in child"); 
       exit(1); 
      } 
     } 
     else { // Parent 
      // This execl() functions runs, but runs over and over. Am I infinitely forking processes and thus calling execl() infinitely? 
      execl("/home/hopper/z1806979/csci330/Assign4/Assign4", "Assign4", NULL); 
     } 
    } 
    else { // Parent 

     close(pipefd[0]); 

     close(1); 

     dup(pipefd[1]); 

     close(pipefd[1]); 

     rs = execlp(token1[0], token1[0], token1[1], token1[2], token1[3], token1[4], NULL); 

     if (rs < 0) { 
      perror("execlp in parent"); 
      exit(1); 
     } 
    } 

    return 0; 
} 
+0

1)迷惑メールをしないでください!これはCには分からない。2)学ぶ[尋ねる]。 – Olaf

+0

ありがとうございます。私はCフラグを削除しました。 –

+1

1)それは_タグ2です。いいえ、私はすでにそれを削除しました。 3)質問の改善に集中する。 – Olaf

答えて

0

exec指定したファイルに含まれるコードでexecを呼び出して、現在のプロセスの現在のコードを回復するシステムコールです。私はあなた自身が常にコードを復旧していると思っていますし、入力コマンドを使って "シェル"を復旧していると思います。

また、リカバリ後に前のコードに戻ることはできません。リカバリ、リカバリ。

関連する問題