命名管道,使用Fork()

时间:2016-04-29 05:29:43

标签: c fork gnu named-pipes fifo

我遇到了一个很大的问题。因此,对于我的任务,我必须使用命名管道(FIFO)和使用Fork()。我有三个程序,lab6(这是我的主要/父/打印答案)然后我有我的生命支持(这是一个孩子),然后我有我的导航(也是一个孩子)。这两个孩子必须写入父母,父母将阅读并打印出来。很难解释答案应该是怎样的。它很复杂,但我的数学正确,当我尝试将此文件移动到单独的FIFO文件时,我只遇到了问题。我收到了细分错误。所以我的问题是我做错了什么?我试图将此代码转换为FIFO。附:不要担心任何时间或种子发生器或类似的东西。我只需要导航来计算它的部分并将其发送给父级,同样也需要生命支持,但我似乎无法使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

儿童(生命支持) -

       #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);
    }

我知道这很多,任何帮助都非常明显。如果这太多了,我会尊重规则并删除除此之外的问题,我认为这将是一个不错的挑战。大声笑 提前谢谢!

1 个答案:

答案 0 :(得分:0)

您似乎试图尽可能地将您的问题混淆给我们。您的程序示例最小 - 它归结为(主要)

namespace printingImageMode
{
    class Program
    {
        static void Main(string[] args)
        {
            Bitmap bmp = new Bitmap(@"C:\Users\falamarzi\Desktop\Kyan Graphic Viewer\jTest.jpg");

            int msb = (int)(bmp.Width & 0x0000ff00) >> 8;
            int lsb = (int)(bmp.Width & 0x000000ff);

        byte msbB = Convert.ToByte(msb);
        byte lsbB = Convert.ToByte(lsb);

        byte[] enter_To_Image_Printing_Mode_Command = new byte[] { (byte)AsciiControlChars.ESC, (byte)DensityCommand.EightDot_SD, msbB, lsbB };

        byte[] imageData = new byte[lsb + msb * 256];

        for (int i = 0; i < imageData.Length; i++)
        {
            imageData[i] = 0xff;
        }

        byte[] complete_Command = new byte[enter_To_Image_Printing_Mode_Command.Length + imageData.Length];

        enter_To_Image_Printing_Mode_Command.CopyTo(complete_Command, 0);
        imageData.CopyTo(complete_Command, enter_To_Image_Printing_Mode_Command.Length);

        SerialPort sPort = new SerialPort("COM5");
        sPort.Open();

        sPort.Write(complete_Command, 0, complete_Command.Length);
    }

}

public enum AsciiControlChars : byte
{
    ESC = 0x1b,
}

    public enum DensityCommand : byte
    {
        EightDot_SD = 0x00,
        EightDot_DD = 0x01,
        TwentyFourDot_SD = 0x20,
        TwentyFourDot_DD = 0x21,
    }
}

您创建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())并在此处开始破坏。

即使如果你有一些外部神奇地写入FIFO,你需要来检查readbuffer的返回值,尤其是在多进程环境中 - 你不能只是假设其他过程在你阅读时已经写了一些东西。