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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:26:52+00:00 2026-06-12T16:26:52+00:00

I’m a little confused on how to properly use pipe() to pass integer values

  • 0

I’m a little confused on how to properly use pipe() to pass integer values between two processes.

In my program I first create a pipe, then I fork it. I assume I have “Two” pipes then?

From what I understand, this is my assignment.
My parent goes through a for loop checking an integer value “i” for a certain operation, increases a count variable, and saves value into an array. After each check my parent should pass an integer value, “i” to my child through a pipe. My child then uses that integer value, does some check on the value, and should increase a count variable, and save the result in a [shared?] array. Eventually; the child should return it’s final count to the parent, who then prints out the two counts, and the “Shared” array.

-> I’m not sure I need to have a shared array or to save the results at all. I may only need the counts – the homework was ambiguous and I’m awaiting a response from the professor. Also; can I even do a shared array between processes? It sounds like a start of some problem to me.

-> Here are my questions:
One; how do I use pipes for integers? I’ve only seen them for character arrays and previous answers don’t seem to think this is possible or legal..? I’m not sure. There was no resolution that I could find on it.

-> How do I use a unidirectional pipe to pass integers to a child? And have the child return something? I’m not sure how I’m able to… differentiate between the two pipes. I do “know” [or think I know] that I have to close one unused portion of each pipe to avoid “Some vague problem”.

Sorry for the dumb questions; I haven’t been taught processes (aside from fork) or pipes (at all) yet in this class – so I’m not really sure where to start!

Heres parts of my code – it’s not pretty and it doesn’t work and I don’t expect it to. It’s more of a shell placeholder. Once I figure out how to use a pipe – I’d Probably make the code make sense.

int main(void)
{
int fd[2];
pid_t childpid;
pid_t parentpid;
int i;
int threecount = 0;
int fivecount = 0;;
int results [MAXSIZE];

parentpid = getpid(); //Get current process ID number

pipe(fd);
childpid = fork(); 

if(childpid == 0){
close(fd[0]); //Closing this for some other reason

}
int j = 0;

if(childpid > 0)
close(fd[1]); //Closing this for some reason


if( childpid == -1 )
{
    perror("Failed to fork\n");
    return 1;
}
if (childpid > 0)
{
    for(i = 1; i < MAXSIZE;i++)
    {
        if(i % 5 == 0)
        {
            fivecount++;
            i = results[j];
            j++; 
            wait(NULL);
        }
    }
}
else if (childpid == 0)
    {
        if(i % 3 == 0) //This i here should probably be the i value above, piped to the child
        {
            threecount++;
            i = results[j];  //This should be part of th pipe
            j++;    //Trying to keep count of that shared array, not really the right way to do it though.
        }

    }
    printf("%d  %d  \n", fivecount,threecount);
    return 0;

}

  • 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-12T16:26:53+00:00Added an answer on June 12, 2026 at 4:26 pm

    This is about as lame (and no error checking, btw) a sample as I can muster for using a pipe to send int from a parent to a child process, where the child was launched from fork(). It gets more complicated (obviously) for sending and receiving data, but i can’t do everything for you. This just forks and waits for an int (actually, the number of bytes that are used by an int) from the child.

    Update: Added send+response two-way communication example after this one. See the second code listing for more information.

    Hope it helps.

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[])
    {
        int fd[2];
        int val = 0;
    
        // create pipe descriptors
        pipe(fd);
    
        // fork() returns 0 for child process, child-pid for parent process.
        if (fork() != 0)
        {
            // parent: writing only, so close read-descriptor.
            close(fd[0]);
    
            // send the value on the write-descriptor.
            val = 100;
            write(fd[1], &val, sizeof(val));
            printf("Parent(%d) send value: %d\n", getpid(), val);
    
            // close the write descriptor
            close(fd[1]);
        }
        else
        {   // child: reading only, so close the write-descriptor
            close(fd[1]);
    
            // now read the data (will block)
            read(fd[0], &val, sizeof(val));
            printf("Child(%d) received value: %d\n", getpid(), val);
    
            // close the read-descriptor
            close(fd[0]);
        }
        return 0;
    }
    

    Output:

    Parent(5943) send value: 100
    Child(5945) received value: 100
    

    Update: Expanded to include send+response using two pipe sets

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/wait.h>
    
    // some macros to make the code more understandable
    //  regarding which pipe to use to a read/write operation
    //
    //  Parent: reads from P1_READ, writes on P1_WRITE
    //  Child:  reads from P2_READ, writes on P2_WRITE
    #define P1_READ     0
    #define P2_WRITE    1
    #define P2_READ     2
    #define P1_WRITE    3
    
    // the total number of pipe *pairs* we need
    #define NUM_PIPES   2
    
    int main(int argc, char *argv[])
    {
        int fd[2*NUM_PIPES];
        int val = 0, len, i;
        pid_t pid;
    
        // create all the descriptor pairs we need
        for (i=0; i<NUM_PIPES; ++i)
        {
            if (pipe(fd+(i*2)) < 0)
            {
                perror("Failed to allocate pipes");
                exit(EXIT_FAILURE);
            }
        }
    
        // fork() returns 0 for child process, child-pid for parent process.
        if ((pid = fork()) < 0)
        {
            perror("Failed to fork process");
            return EXIT_FAILURE;
        }
    
        // if the pid is zero, this is the child process
        if (pid == 0)
        {
            // Child. Start by closing descriptors we
            //  don't need in this process
            close(fd[P1_READ]);
            close(fd[P1_WRITE]);
    
            // used for output
            pid = getpid();
    
            // wait for parent to send us a value
            len = read(fd[P2_READ], &val, sizeof(val));
            if (len < 0)
            {
                perror("Child: Failed to read data from pipe");
                exit(EXIT_FAILURE);
            }
            else if (len == 0)
            {
                // not an error, but certainly unexpected
                fprintf(stderr, "Child: Read EOF from pipe");
            }
            else
            {
                // report what we received
                printf("Child(%d): Received %d\n", pid, val);
    
                // now double it and send it back
                val *= 2;
    
                printf("Child(%d): Sending %d back\n", pid, val);
                if (write(fd[P2_WRITE], &val, sizeof(val)) < 0)
                {
                    perror("Child: Failed to write response value");
                    exit(EXIT_FAILURE);
                }
            }
    
            // finished. close remaining descriptors.
            close(fd[P2_READ]);
            close(fd[P2_WRITE]);
    
            return EXIT_SUCCESS;
        }
    
        // Parent. close unneeded descriptors
        close(fd[P2_READ]);
        close(fd[P2_WRITE]);
    
        // used for output
        pid = getpid();
    
        // send a value to the child
        val = 42;
        printf("Parent(%d): Sending %d to child\n", pid, val);
        if (write(fd[P1_WRITE], &val, sizeof(val)) != sizeof(val))
        {
            perror("Parent: Failed to send value to child ");
            exit(EXIT_FAILURE);
        }
    
        // now wait for a response
        len = read(fd[P1_READ], &val, sizeof(val));
        if (len < 0)
        {
            perror("Parent: failed to read value from pipe");
            exit(EXIT_FAILURE);
        }
        else if (len == 0)
        {
            // not an error, but certainly unexpected
            fprintf(stderr, "Parent(%d): Read EOF from pipe", pid);
        }
        else
        {
            // report what we received
            printf("Parent(%d): Received %d\n", pid, val);
        }
    
        // close down remaining descriptors
        close(fd[P1_READ]);
        close(fd[P1_WRITE]);
    
        // wait for child termination
        wait(NULL);
    
        return EXIT_SUCCESS;
    }
    

    (compile with, e.g., gcc thisfile.c -o test)

    Output

    Parent(2794): Sending 42 to child
    Child(2797): Received 42
    Child(2797): Sending 84 back
    Parent(2794): Received 84
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

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.