簡體   English   中英

Scanf 正在讀取命名管道程序上的 ctrl+c

[英]Scanf is reading ctrl+c on named-pipes program

我正在嘗試使用 2 C 程序(讀取和寫入)創建一個命名為 pipe 或 FIFO 的程序。 這兩個程序都在 while(1) 循環中運行。 這些程序應該在按下 ctrl+c 時終止,但是當我在寫入程序上按下 ctrl+c 時,它仍然向讀取程序發送一些東西。 如果我輸入普通數字,它工作正常,但是當我在寫入程序上按 ctrl+c 終止時,它將這個隨機數提供給讀取程序

#normal numbers shows:
BMI: 16.1, Overweight

#on ctrl+c input:
BMI = 165534.6 (some random numbers)

這是程序的代碼

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
    int fd, fd1, fd2;
    
    //FIFO file path
    char * myfifo = "/tmp/myfifo";

    mkfifo(myfifo,0666);

    float weight, height, bmi = 0;
    char arr1[80];
    while(1)
    {
        //open
        fd = open(myfifo, O_WRONLY);
        fd1 = open(myfifo, O_WRONLY);
        fd2 = open(myfifo, O_WRONLY);

        printf("Name            : ");
        scanf("%[^\n]", arr1);
        getchar();
        write(fd, arr1, strlen(arr1)+1);
        close(fd);
        
        printf("Body Weight in kg    : ");
        scanf("%f",  &weight);
        getchar();
        write(fd1, &weight, sizeof(weight));
        printf("%f\n", weight);
        close(fd1);

        printf("Body Height in cm    : ");
        scanf("%f",  &height);
        getchar();
        printf("\n---------------------------\n\n");
        write(fd2, &height, sizeof(height));
        printf("%f\n", height);
        close(fd2);

    }
    return 0;
}

這是讀取程序的代碼,它在 while(1) 循環中進行

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
    int fd, fd1, fd2;
    char * myfifo = "/tmp/myfifo";
    mkfifo(myfifo,0666);
    float weight, height, bmi = 0;
    char str1[80];
    while(1)
    {
        //First open in read only and read
        fd = open(myfifo, O_RDONLY);
        fd1 = open(myfifo, O_RDONLY);
        fd2 = open(myfifo, O_RDONLY);
        read(fd, str1, 80);
        read(fd1, &weight, sizeof(weight));
        read(fd2, &height, sizeof(height));
        
        height = (height / 100);
        
        bmi = weight / (height * height);

        //Print the read string and close
        printf("%s BMI : %.1f\n", str1, bmi);
        close(fd);
        close(fd1);
        close(fd2);

        if(bmi < 18.5)
        {
            printf("Details = Underweight\n");
        }
        else if(bmi >= 18.5 && bmi <= 22.9)
        {
            printf("Details = Normal\n");
        }
        else if(bmi >= 23.0 && bmi <= 24.9)
        {
            printf("Details = Overweight\n");
        }
        else if(bmi >= 25.0 && bmi <= 29.9)
        {
            printf("Details = Obese I (Obesitas Class I)\n");
        }
        else
        {
            printf("Details = Obese II (Obesitas Class II)\n");
        }

    }
    return 0;
}

所以,我想知道,有沒有辦法讓 ctrl+c 終止這兩個進程? 這樣程序在按下 cttrl+c 后就不會發送任何東西。 我認為出現此問題是因為即使按下 ctrl+c,讀取程序仍會從寫入中讀取一些輸入。

這是因為您沒有檢查 read() 可能會在閱讀器中返回 0。 因此,當它返回 0(= 讀取 0 個字符)時,您將顯示緩沖區中的所有垃圾。 當 read() 返回 0 時,您應該終止讀取器,因為這意味着另一端已關閉(寫入器已退出)。

作為一般規則,嘗試檢查服務/系統調用的所有返回碼。 您可以在不檢查任何內容的情況下制作初稿。 但是當它出現功能障礙時,添加對返回碼的檢查。 這通常有助於...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM