Script getting stuck in while trying to continuously read from a pipe in a while loop

I am trying to implement a script in which a pipe is getting created and the default value 0 is stored in the pipe. Simultaneously, a c code file is getting executed which based on a condition updates the value in pipe to 1.

In the script, the pipe value is being read continuously, based on which if the pipe value is updated then another variable’s flag value will be changed to 1 and the loop will end. But the problem is that read takes a lot time to read due to which the script is getting stuck inspite of using the timeout feature.

#!/bin/bash

pipe_name="/tmp/my_pipe"
initial_value="0"

# Create the named pipe if it does not exist
if [ ! -p "$pipe_name" ]; then
    mkfifo "$pipe_name"
fi

# Function to initialize the pipe with the initial value
initialize_pipe() {
    echo "$initial_value" > "$pipe_name" &
}

# Initialize the pipe
initialize_pipe

STOP_FH_LOGS="$initial_value"

while true; do
    # Using read with a timeout to avoid blocking indefinitely
    if read -t 1 new_value < "$pipe_name"; then
        STOP_FH_LOGS=$new_value
        
        # Check the read value and write it back to the pipe
        if [ "$STOP_FH_LOGS" = "0" ]; then
            echo "$STOP_FH_LOGS" > "$pipe_name" &
            echo "Condition still 0"
        elif [ "$STOP_FH_LOGS" = "1" ]; then
            echo "Value changed to 1"
            break
        else
            echo "New STOP_FH_LOGS error value: $STOP_FH_LOGS"
            # Optionally, write back the new value if needed
            echo "$STOP_FH_LOGS" > "$pipe_name" &
        fi
    else
        # Handle timeout or no data read scenario
        echo "Timeout or no data read"
        # Ensure the pipe is not empty
        if [ ! -p "$pipe_name" ]; then
            initialize_pipe
        fi
    fi
done

A sample c code trying to change the code in the pipe is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#define PIPE_NAME "/tmp/my_pipe"
#define INITIAL_VALUE "0"
#define NEW_VALUE "1"

void write_to_pipe(const char *value) {
    int pipe_fd = open(PIPE_NAME, O_WRONLY);
    if (pipe_fd == -1) {
        perror("Failed to open pipe for writing");
        exit(EXIT_FAILURE);
    }

    if (write(pipe_fd, value, strlen(value)) == -1) {
        perror("Failed to write to pipe");
        close(pipe_fd);
        exit(EXIT_FAILURE);
    }

    close(pipe_fd);
}

int main() {
    char buffer[2];
    int pipe_fd;

    while (1) {
        pipe_fd = open(PIPE_NAME, O_RDONLY);
        if (pipe_fd == -1) {
            perror("Failed to open pipe for reading");
            exit(EXIT_FAILURE);
        }

        ssize_t num_read = read(pipe_fd, buffer, 1);
        if (num_read == -1) {
            perror("Pipe read error");
            close(pipe_fd);
            exit(EXIT_FAILURE);
        }

        buffer[num_read] = '';
        close(pipe_fd);

        // Check if the current value is "0"
        if (strcmp(buffer, "0") == 0) {
            write_to_pipe(NEW_VALUE);
            break;
        }

        sleep(1); // Sleep to avoid busy-waiting
    }

    return 0;
}

Tried the timeout condition but it is still getting stuck, so need suggestions as to what can be done

New contributor

Abhir Raj Shrivastava is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật