Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9009791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:17:31+00:00 2026-06-16T02:17:31+00:00

I’ve got two C files, server.c and client.c. The server has to create a

  • 0

I’ve got two C files, server.c and client.c. The server has to create a fifo file and constantly read in it, waiting for input. The client gets its PID and writes the PID in the fifo.
This is my server file which I launch first:

int main(){
  int fd;
  int fd1;
    int bytes_read;
    char * buffer = malloc(5);
    int nbytes = sizeof(buffer);

    if((fd = mkfifo("serverfifo",0666)) == -1) printf("create fifo error");
    else printf("create fifo ok");

    if ((fd1 = open("serverfifo",O_RDWR)) == -1) printf("open fifo error");
    else{
        printf("open fifo ok"); 
        while(1){
            bytes_read = read(fd,buffer,nbytes);
            printf("%d",bytes_read);
            }
        }

return(0);
}

And my client file :

int main(){

    int fd;
    int pid = 0;
    char *fifo;
    int bytes;

    if ((pid = getpid()) == 0)  printf("pid error");
    char pid_s[sizeof(pid)];
    sprintf(pid_s,"%d",pid); 


   if ((fd = open ("serverfifo",O_RDWR)) == -1)printf("open fifo error");
   else {
    printf("open fifo ok");

        bytes = write(fd,pid_s, sizeof(pid_s));
        printf("bytes = %d",bytes);

   }

    close(fd);
return(0);
}

The two main problems I’m getting are: When I write the pid to the file it returns the number of bytes I wrote so it looks okay but when I check the properties of the fifo file it says 0 bytes. The second problem is the read doesn’t work. If I do a printf before it shows, but after it doesn’t and the read isn’t returning anything it just freezes.
I realise there are a lot of similar posts on the site but I couldn’t find anything that helped.
I’m using Ubuntu and GCC compiler with CodeBlocks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:17:32+00:00Added an answer on June 16, 2026 at 2:17 am

    There are many things wrong here

    char pid_s[sizeof(pid)];
    sprintf(pid_s,"%d",pid); 
    

    sizeof(pid) returns the size of the pid value, not its string representation, i.e. it is sizeof(int) which is either 4 or 8, depending on your architecture. You then proceed to print it. If this works it works only by luck (you are on a 64 bit machine). The correct way to do is, if you choose to do it at all, is to allocate a suitably large buffer, and use snprintf to make sure you don’t overflow. PIDs fit in 5 digits, so something like this will do:

    char pid_s[8];
    snprintf(pid_s, sizeof(pid_s), "%d", pid);
    

    of course, you can skip this step all together and send the raw bytes of the pid instead

    write(fd, (void*)&pid, sizeof(pid))
    

    Now in the server you make similar mistakes:

    char * buffer = malloc(5);
    int nbytes = sizeof(buffer);
    

    sizeof(buffer) returns 4 or 8 again, but you allocated 5 bytes, the correct way to do this, if you want to allocate on the heap (using malloc), is this:

    char* buffer = malloc(8);
    int nbytes = 8;
    

    alternatively you can allocate on the stack:

    char buffer[8];
    int nbytes = sizeof(buffer);
    

    sizeof is sort of magical, in that if you pass in an array, it returns the size of the array (8*1) in this case.

    When you are reading, you read 5 bytes, which is likely not enough (because you wrote 8 bytes due to the earlier bug), so it would not finish. You should read like this

    int pid;
    read(fd, (void*)&pid, sizeof(pid));
    

    Also, if you were to actually read and write strings, you’d do something like this:

    // client
    char pid_s[8];
    snprintf(pid_s, sizeof(pid_s), "%d", pid);
    write(fd, pid_s, sizeof(pid_s));
    
    // server
    char pid_s[8];
    read(fd, pid_s, sizeof(pid_s));
    

    Note also that read may return less than what was written, and you need to call it again to keep reading…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.