0

I was trying to test named pipe when i encountered this error. I have a file named client.c who writes to a named pipe. And I have a file named server.c who reads from a named pipe and print its value. Looks like there is some issue with server.c

Client code.

//-------CLIENT-----------
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#define FIFO_FILE "MYFIFO"
int main()
{
    int fd, ch;
    char readbu[80];
    int write_byte;
    fd=open(FIFO_FILE,O_WRONLY);
    while(1)
    {
        printf("Enter choice \n1.Pepsi\n2.Coke\n3.Limca\n4.Maza\n");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1: strcpy(readbu,"Pepsi\0");  
            write_byte=write(fd,readbu,sizeof(readbu));
            break;
            case 2: strcpy(readbu,"Coke\0");  
            write_byte=write(fd,readbu,sizeof(readbu));
            break;
            case 3: strcpy(readbu,"Limca\0");  
            write_byte=write(fd,readbu,sizeof(readbu));
            break;
            case 4: strcpy(readbu,"Maza\0");  
            write_byte=write(fd,readbu,sizeof(readbu));
            break;
            default:printf("Invalid");
            break;
        }
    }
    return 0;
}

Server code:

// Server code    
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#define FIFO_FILE "MYFIFO"
int main()
{
    int fd;
    char readbuf[80];
    char end[10];
    int to_end;
    int read_bytes;
    mknod(FIFO_FILE,S_IFIFO|0640,0);
    strcpy(end,"end");
    while(1)
    {
        fd=open(FIFO_FILE,O_RDONLY);
        read_bytes=read(fd,readbuf,sizeof(readbuf));
        readbuf[read_bytes]='\0';
        printf("Rec str = %s of len %d", readbuf,(int)strlen(readbuf));
        to_end=strcmp(readbuf,end);
        if(to_end==0)
        { close(fd);
            break;
        }`enter code here`
    }
    return 0;
}

At server side above code does not print any output.

If i change the printf statements to below then I observe that first iteration it prints blank line and then for other iteration it print old values.

printf("\nRec str = %s of len %d", readbuf,(int)strlen(readbuf));

If i change the printf statements to below then I observe that it prints correct values.

printf("Rec str = %s of len %d\n", readbuf,(int)strlen(readbuf));

I am completely confused how the \n makes so much difference.

I tried to check the value in readbuf using gdb and it actually contains the correct values. Kindly help me understand the trick here.

5
  • If you're running on Linux, run your server process under strace and you'll see what's going on, especially if you use strace to emit what data your server process actually reads from the pipe. Commented Oct 2, 2018 at 21:44
  • strcpy(readbu,"Pepsi\0"); What is this \0 doing here exactly? Commented Oct 2, 2018 at 21:44
  • 1
    stdout in line buffered. The contents of the buffer are not displayed on the screen until a \n is printed. So putting the \n at the end of the printf works correctly. Commented Oct 2, 2018 at 21:44
  • Also, you shouldn't have to do fd=open(FIFO_FILE,O_RDONLY); in every loop iteration. Commented Oct 2, 2018 at 21:45
  • @AndrewHenle when I checked in gdb I observed readbuf actually had correct data. I understand stdout is line buffered. But any printf works if we dont put any \n there. And why it prints old values when the readbuf actually has the correct data ? from where it got the old data ? Commented Oct 2, 2018 at 21:47

1 Answer 1

2

The output buffer is not normally flushed until it is either full, a newline is inserted, or an explicit fflush( stdout ) call.

When you have the newline at the start of the output, it flushes the previously buffered data, and buffers the following data.

The following will resolve the issue:

printf("\nRec str = %s of len %d", readbuf,(int)strlen(readbuf));
fflush( stdout ) ;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Clifford for this answer. It helped me a lot. I verified and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.