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 4001328
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:53:27+00:00 2026-05-20T07:53:27+00:00

I have a problem with a simple program im making with fork and pipes

  • 0

I have a problem with a simple program im making with fork and pipes for learning purpose.
I want a child that send some data to the parent and this (the parent) send it again to the child.

The result is that the parent acts like the pipe is non-blocking and the child acts if the pipe is blocking. But i didnt use any code to tell that the pipes are non-blocking at all.

Let me show it:

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

void WriteStr(char* inStr)
{
    write(1,inStr,strlen(inStr));
}

int main(void){

    char *cad1 = "Introdueix un valor:  ";
int bytesr = 0;
char valor[256];

//Un pipe serveix solament per una comunicació unidereccional, 
//si es necessari que pare i fill es comuniquin en amdos sentits, 
//s'hauran d'utilitzar dos pipes.

int pip_fill[2];    //pip_fp[0]: file descriptor de lectura
                //pip_fp[1]: file descriptor d'escriptura

int pip_pare[2];    //pip_pf[0]: file descriptor de lectura
                //pip_pf[1]: file descriptor d'escriptura
int pid, nbytes = 0;
char cad[256];
int num = 0;

if (pipe (pip_fill)<0){
    write (1,"Pipe error!!!!\n",15);
    exit (1);
}

if (pipe (pip_pare)<0){
    write (1,"Pipe error!!!!\n",15);
    exit (1);
}

fflushnou();

pid = fork();
switch (pid){
    case -1:    
            //Error
            write (2,"Tractar_Valor: Error!!!!\n",25);
            exit (-1);
    case 0:
            //Fill
            //printf("Fill: fp[0]:%d fp[1]:%d pf[0]:%d pf[1]:%d \n",pip_fp[0],pip_fp[1],pip_pf[0],pip_pf[1]);
            close(pip_fill[0]);
                close(pip_pare[1]);

            while(1){
                bytesr = read(0, valor, 256);

                valor[bytesr] = '\0';

                write(pip_fill[1],valor,strlen(valor)); //el fill escriu a la pipe la var valor

                WriteStr("Fill avans\n");
                nbytes = read(pip_pare[0],cad,strlen(cad)); //el fill llegeix de la pipe la var un cop ha estat tractada per el pare
                WriteStr("Fill despres\n");

                if (nbytes != 0){   //vol dir que hem llegit algo per la pipe pip_pf
                    write(1,cad,strlen(cad));   //pintem cad per pantalla
                }

                sleep(1);
            }
    default:
            //Pare
            close(pip_fill[1]);
            close(pip_pare[0]);
            close(0);

            while(1){   

                nbytes = read(pip_fill[0],valor,strlen(valor));//el pare llegeix de la pipe la var valor 
                //WriteStr("Pare despres\n");
                if (nbytes != 0){   //vol dir que hem llegit algo per la pipe pip_fp
                    //tractem la variable valor
                    num = atoi(valor);
                    num = num*2;

                    sprintf(cad,"Valor actual de la variable: %d \n\n",num);

                    write(1,cad,strlen(cad));

                    write(pip_pare[1],cad,strlen(cad)); //el pare escriu a la pipe la var tractada
                }

                sleep(1);
            }
}
return 0;
}

The actual behaviour is that child accepts input, and then stucks reading ‘pip_pare[0]’. At the same time, parent process is looping and reading all the time from ‘pip_fill[0]’ a value of 0.

So, i am little confused about that, why parent is reading and loopinf without bloking in the ‘read’ function ??

Any suggestion to fix it ?

Thanks for the help 🙂

LLORENS

  • 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-05-20T07:53:28+00:00Added an answer on May 20, 2026 at 7:53 am
    nbytes = read(pip_pare[0],cad,strlen(cad));
    

    I think you probably meant sizeof(cad) here.

    write(1,cad,strlen(cad));
    

    And nbytes here.

    nbytes = read(pip_fill[0],valor,strlen(valor));
    

    And this along similar lines, but there’s a hidden trap in this version which I shall leave as an exercise!

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

Sidebar

Related Questions

I have a strange build problem. I have a simple test program that sends
i have very simple problem. I need to create model, that represent element of
I have a very simple problem and a solution that will work, but I'm
I am making an application that does some custom image processing. The program will
I am having a very strange problem in a simple program and have been
I have a simple problem when querying the SQL Server 2005 database. I have
I have a problem with a simple included file. The file being included is
Seems like a simple problem: I have an SVN repo inside our firewall. I
I have a very simple problem which requires a very quick and simple solution
I have a seemingly simple problem though i am unable to get my head

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.