C 使用Fork()命名管道

C 使用Fork()命名管道,c,fork,gnu,named-pipes,fifo,C,Fork,Gnu,Named Pipes,Fifo,我有一个相当大的问题。因此,对于我的任务,我必须使用命名管道(FIFO)和Fork()。我有三个程序,lab6(这是我的主要/父母/打印答案),然后我有我的生命支持(这是一个孩子),然后我有我的导航(这也是一个孩子)。这两个孩子必须向家长写信,家长将阅读并打印出来。很难解释答案应该是什么。这很复杂,但我的数学正确,我只是在尝试将此文件移动到单独的FIFO文件时遇到问题。我得到了分割错误。所以我的问题是我做错了什么??我正在尝试将此代码转换为FIFO。p、 不要担心任何时间、种子发生器或诸如此类的

我有一个相当大的问题。因此,对于我的任务,我必须使用命名管道(FIFO)和Fork()。我有三个程序,lab6(这是我的主要/父母/打印答案),然后我有我的生命支持(这是一个孩子),然后我有我的导航(这也是一个孩子)。这两个孩子必须向家长写信,家长将阅读并打印出来。很难解释答案应该是什么。这很复杂,但我的数学正确,我只是在尝试将此文件移动到单独的FIFO文件时遇到问题。我得到了分割错误。所以我的问题是我做错了什么??我正在尝试将此代码转换为FIFO。p、 不要担心任何时间、种子发生器或诸如此类的事情。我只需要导航来计算它的部分并将其发送给家长,生命支持也是如此,但我似乎无法让FIFO工作

主文件-

  #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
#包括
#包括
#包括
#包括
#包括
#包括
#定义FIFO_文件“MYFIFO”//默认为当前目录
void getTime();
void writeDomain(字符*,整数);
void lifeSupport();
无效导航();
int生成(int);
无效倒计时(int);
//全局变量
const int MAX_LINE=80;
国际贸易,我的管道[2];
字符*消息;
国际时间(int time);;
char myClock[25];
//管道变量
int totalTime=0;
char-toSend[120];
内部主(空){
文件*fpoint;
字符读取缓冲区[80];
int=1;
mknod(FIFO_文件,S|u IFIFFO | 0666,0);
同时(再次){
fpoint=fopen(FIFO_文件,“r”);
fgets(读缓冲区,80,fpoint);
printf(“接收的字符串:%s\n,readbuffer”);
fclose(fpoint);
如果(strcmp(readbuffer,“stop”)==0)再次=0;
返回(0);
}
如果(ret==0){
如果(fork()==0){
while(重复){
总时间=0;
getTime();
生命支持();
导航();
getTime();
sprintf(toSend,[%s]\t睡眠\t\t[%d秒]\n),myClock,30-totalTime;
写入(myPipes[PipeStdOut]、toSend、strlen(toSend)+1);
睡眠(30-总时间);
}
}
否则{
同时(再次){
读取(myPipes[PipeStdIn],缓冲区,最大行);
再次=strcmp(缓冲器,“停止”);
如果(再次==0);
其他的
printf(“%s\n”,缓冲区);
}
}
}//出口总管
儿童(生命支持)-

#包括
#包括
#包括
#包括
#包括
#包括
#定义FIFO_文件“MYFIFO”
void getTime();
void writeDomain(字符*,整数);
void lifeSupport();
无效导航();
int生成(int);
无效倒计时(int);
void lifeSupport(){
getTime();
message=“开始生命支持系统\t”;
sprintf(toSend,[%s]%s],myClock,message);
写入(myPipes[PipeStdOut]、toSend、strlen(toSend)+1);
睡眠(1);
totalTime++;
getTime();
时间=生成(5);
消息=“调整呼吸气体水平”;
sprintf(发送,[%s]%s\t[%d秒]”,我的时钟,消息,时间;
写入(myPipes[PipeStdOut]、toSend、strlen(toSend)+1);
睡眠(时间);
总时间+=时间;
getTime();
时间=生成(3);
message=“调整环境”;
sprintf(发送,[%s]%s\t\t[%d秒]”,我的时钟,消息,时间;
写入(myPipes[PipeStdOut]、toSend、strlen(toSend)+1);
睡眠(时间);
总时间+=时间;
getTime();
message=“完成。生命支持已调整。\t[1秒]”;
sprintf(toSend,[%s]%s],myClock,message);
写入(myPipes[PipeStdOut]、toSend、strlen(toSend)+1);
睡眠(1);
totalTime++;
getTime();
message=“\t\t执行时间”;
sprintf(toSend,“%s[%d秒)]\n”,消息,(totalTime-1);
写入(myPipes[PipeStdOut]、toSend、strlen(toSend)+1);
睡眠(1);
totalTime++;
文件*fpoint;
int=1;
char strIn[80]=“使用来自命令行的消息”;
如果(argc!=2){
printf(“用法:NamedPipeClient[string]\n”);
出口(1);
}
while(同样=1){
if((fpoint=fopen(FIFO_文件,“w”))==NULL){
佩罗尔(“福彭”);
出口(1);
if((fpoint=fopen(FIFO_文件,“w”))==NULL){
佩罗尔(“福彭”);
出口(1);
}
}
}
和最后一个子项(导航。请记住,两个子项都向父项写入)-

#包括
#包括
#包括
#包括
#包括
#包括
#定义FIFO_文件“MYFIFO”
void getTime();
void writeDomain(字符*,整数);
void lifeSupport();
无效导航();
int生成(int);
无效倒计时(int);
无效导航(){
getTime();
message=“开始导航系统”;
sprintf(toSend,[%s]%s\t[1秒]”,myClock,消息);
写入(myPipes[PipeStdOut]、toSend、strlen(toSend)+1);
睡眠(1);
totalTime++;
getTime();
时间=生成(6);
message=“调整导航系统”;
sprintf(发送,[%s]%s\t[%d秒]”,我的时钟,消息,时间;
写入(myPipes[PipeStdOut]、toSend、strlen(toSend)+1);
睡眠(时间);
总时间+=时间;
getTime();
message=“完成。导航系统已调整\t[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 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);
    }
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
      ....
   }