2016-04-29 20 views
-2

私はかなり大きな問題があります。だから私の割り当てのために私は名前付きパイプ(FIFO)とFork()を使う必要があります。私は3つのプログラムlab6(主/親/印刷の回答です)を持っています。それから私は生きがいを持っています(それは子供です)。そして私はナビゲーションも持っています。 2人の子供は親に書く必要があり、親はそれを読んでそれを印刷します。答えがどうあるべきか説明するのは本当に難しいです。その複雑ですが、私は数学の権利を得ました。私は、このファイルを別々のFIFOファイルに移動しようとすると問題に遭遇しました。私はセグメンテーションフォールトエラーが発生しています。だから私の質問は私が間違って何ですか?このコードをFIFOに変換しようとしています。 p.s.時間やシードジェネレータなどのことについて心配しないでください。私はちょうどその部分を計算し、親に送信するナビゲーションが必要と同じ寿命になるが、私はFIFOが動作するように見えることはできません。名前付きパイプ、Fork()を使用

メインファイル -

#include<unistd.h> 
#include<stdio.h> 
#include<string.h> 
#include<stdlib.h> 
#include<sys/stat.h> 
    #include<linux/stat.h> 

#define FIFO_FILE "MYFIFO"   //default is current directory 


void getTime(); 
void writeToMain(char *, int); 
void lifeSupport(); 
    void navigation(); 
int generate(int); 
    void countdown(int); 

//Global Variables 
    const int MAX_LINE = 80; 
    int ret, myPipes[2]; 
    char *message; 

int time_; 
    char myClock[25]; 

//Pipe variables 
    int totalTime = 0; 
    char toSend[120]; 



int main(void){ 

    FILE *fpoint; 
    char readbuffer[80]; 
    int again = 1; 


    mknod(FIFO_FILE, S_IFIFO | 0666, 0); 

    while(again){ 

    fpoint = fopen(FIFO_FILE, "r"); 
    fgets(readbuffer, 80, fpoint); 
    printf("recevived string: %s\n, readbuffer"); 
    fclose(fpoint); 
    if(strcmp(readbuffer, "stop") == 0) again = 0; 

    return(0); 

      } 


       if(ret == 0){ 
      if(fork() == 0){ 

        while (repeat){ 

          totalTime = 0; 
          getTime(); 
          lifeSupport(); 
          navigation(); 

          getTime(); 
          sprintf(toSend, "[ %s ] \tSleep \t\t[ %d second(s) ]\n", myClock, 30 - totalTime); 
          write(myPipes[PipeStdOut], toSend, strlen(toSend) + 1); 

          sleep(30 - totalTime); 
        } 
      } 

      else{ 

        while(again){ 
        read(myPipes[PipeStdIn], buffer, MAX_LINE); 
        again = strcmp(buffer, "Stop"); 


        if (again == 0) ; 
          else 
           printf("%s \n", buffer); 
        } 

      } 
     }//exit main 

子(ライフサポート) -

 #include<stdio.h> 
    #include<stdlib.h> 
    #include<string.h> 
    #include<unistd.h> 
    #include<sys/stat.h> 
    #include<linux/stat.h> 
    #define FIFO_FILE "MYFIFO" 



    void getTime(); 
    void writeToMain(char *, int); 
    void lifeSupport(); 
    void navigation(); 
    int generate(int); 
    void countdown(int); 

    void lifeSupport(){ 
      getTime(); 
      message = "Beginning life support systems \t"; 
      sprintf(toSend, "[ %s ] %s ", myClock, message); 
      write(myPipes[PipeStdOut], toSend, strlen(toSend) + 1); 
      sleep(1); 
      totalTime ++; 

      getTime(); 
      time_ = generate(5); 
      message = "Adjusting breathing gas levels"; 
      sprintf(toSend, "[ %s ] %s \t[ %d Second(s) ]", myClock, message, time_); 
      write(myPipes[PipeStdOut], toSend, strlen(toSend) + 1); 
      sleep(time_); 
     totalTime += time_; 

      getTime(); 
      time_ = generate(3); 
      message = "Adjusting environment"; 
      sprintf(toSend, "[ %s ] %s \t\t[ %d Second(s) ]", myClock, message, time_); 
      write(myPipes[PipeStdOut], toSend, strlen(toSend) + 1); 
      sleep(time_); 
      totalTime += time_; 

      getTime(); 
      message = "Done. Life support adjusted. \t[ 1 Second(s) ]"; 
      sprintf(toSend, "[ %s ] %s ", myClock, message); 
      write(myPipes[PipeStdOut], toSend, strlen(toSend) + 1); 
      sleep(1); 
      totalTime ++; 

      getTime(); 
      message = "\t\tExecution time"; 
      sprintf(toSend, "%s [ %d Second(s) ]\n", message, (totalTime - 1)); 
    write(myPipes[PipeStdOut], toSend, strlen(toSend) + 1); 
    sleep(1); 
    totalTime ++; 


      FILE *fpoint; 
      int again =1; 
      char strIn[80] = "Use message from command line"; 

      if(argc !=2){ 
     printf("USAGE: NamedPipeClient[string]\n"); 
        exit(1); 
        } 

      while(again == 1){ 

      if((fpoint = fopen (FIFO_FILE, "w")) == NULL){ 
        perror("fopen"); 
        exit(1); 

      if((fpoint = fopen(FIFO_FILE, "w")) == NULL){ 
        perror("fopen"); 
        exit(1); 
          } 
        } 
      } 

と最後の子(ナビゲーション、両方の子供が親に書き込むことを覚えておいてください。) -

 #include<stdio.h> 
    #include<stdlib.h> 
    #include<string.h> 
    #include<unistd.h> 
    #include<sys/stat.h> 
    #include<linux/stat.h> 
    #define FIFO_FILE "MYFIFO" 


    void getTime(); 
    void writeToMain(char *, int); 
    void lifeSupport(); 
    void navigation(); 
    int generate(int); 
    void countdown(int); 

    void navigation(){ 
    getTime(); 
    message = "Beginning navigation systems"; 
    sprintf(toSend, "[ %s ] %s \t[ 1 Second(s) ]", myClock, message); 
    write(myPipes[PipeStdOut], toSend, strlen(toSend) + 1); 
    sleep(1); 
    totalTime ++; 

    getTime(); 
    time_ = generate(6); 
    message = "Adjusting navigation systems"; 
    sprintf(toSend, "[ %s ] %s \t[ %d Second(s) ]", myClock, message, time_); 
    write(myPipes[PipeStdOut], toSend, strlen(toSend) + 1); 
    sleep(time_); 
    totalTime += time_; 
      getTime(); 
    message = "Done. Navigation systems adjusted \t[ 1 Second(s) ]"; 
    sprintf(toSend, "[ %s ] %s ", myClock, message); 
    write(myPipes[PipeStdOut], toSend, strlen(toSend) + 1); 
    sleep(1); 
    totalTime ++; 

    message = "\t\tExecution time"; 
    sprintf(toSend, "%s [ %d Second(s) ]", message, (totalTime - 2)); 
    write(myPipes[PipeStdOut], toSend, strlen(toSend) + 1); 
    sleep(1); 
    totalTime += 2; 

    getTime(); 
    message = "\tTotal time"; 
    sprintf(toSend, "[ %s ] %s \t[ %d Second(s) ]", myClock, message, totalTime); 
    write(myPipes[PipeStdOut], toSend, strlen(toSend) + 1); 
    sleep(1); 



      while(again == 1){ 

      if((fpoint = fopen (FIFO_FILE, "w")) == NULL){ 
        perror("fopen"); 
        exit(1); 
      } 

      if((fpoint = fopen(FIFO_FILE, "w")) == NULL){ 
       perror("fopen"); 
        exit(1); 
      } 
    } 
      fputs(strIn,fpoint); 
      fclose(fpoint); 

    return(0); 
    } 

私はこれがたくさんあることを知っており、どんな助けも大いに訴えています。これがあまりにも大きければ、私はルールを尊重し、それ以外の質問を削除するでしょう、それはまともなチャレンジになると思います。 lol ありがとうございます!

+0

'fopen()'と 'fclose()'の戻り値をチェックします。なぜあなたは他の 'f'関数のためにそれをしませんでしたか? – tofro

+0

どういう意味ですか? @tofro – rtayl0a

+0

1. FIFOからの主な読み込みで 'fgets()'の戻り値がチェックされません。 2. 'lifeSupport'のFIFOに書き込まれたことは何もありません(なぜそれを含めるのか分からないのですが、FIFOはオープンされ、何も行われません)、3. FIFOへの 'fputs()'の戻り値のチェック'navigation' - あなたがそれをチェックしないと、*何が間違っているのか知りたいのですか? – tofro

答えて

0

可能な限り問題を難読化しようとしたようです。あなたのプログラム例はない最小限である - それはあなたがそれを開くと(あなたが実際に何かを得たことを確認せずに)、それから読んで、FIFOを作成

int main (void){ 

    mknod(FIFO_FILE, S_IFIFO | 0666, 0); 

    while(again){ 

     fpoint = fopen(FIFO_FILE, "r"); 
     fgets(readbuffer, 80, fpoint); 
     printf("recevived string: %s\n, readbuffer"); 
     fclose(fpoint); 

     // All the fork() stuff follows here 
     .... 
    } 

に(メイン)沸きます。あなたが実際にやっている子プロセスが開始されていなくても、そこに何かを置くことを期待しているのは誰ですか?

戻り値fgets()をチェックしていないため、初期化されていないバッファ(readbuffer)で作業しています。あなただけの他のプロセスを想定することはできませんすでに何かするときに書かれているだろう - あなたは外部の何か魔法のFIFOへの書き込みがあった

でも場合、あなた必要は、特にマルチプロセス環境でfgets()の戻り値をチェックしますあなたが読む。

関連する問題