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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:57:24+00:00 2026-06-05T08:57:24+00:00

At a course the teacher gave us some code (on the chalk board) but

  • 0

At a course the teacher gave us some code (on the chalk board) but he has horrible hand writing, and I can’t make a few parts out. Also new at both pipe and fork so that doesn’t help.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
void main () {
    int a[] = {1,2,3,4}, f[2];  // ok so we initialize an array and a "pipe folder" (pipefd in manual) ?
    pipe (f);                   // not 100% sure what this does ?
    if (fork () == 0) {         // if child
        close (f[0]);               // close pipe read-end
        a[0] += a[1];               
        write (f[1], &a[0], sizeof (int))      // fixed
        close (f[1]);               // close pipe write-end
        exit(0);                    // closes child and sends status update to parent ?
    }
    else {                          // if parent
        close (f[1])                // close write-end of pipe
        a[2]+=a[3];
        read (f[0], &a, sizeof(int))          // fixed
        wait (0);                   // waits for child to ... close ? or just finish ? or is it the same thing
        a[0]+= a[2]; close (f[0]); 
        printf ("%d\n, "a[0]);
   }
}

Does the child and parent go in some particular order. I’m guessing parent waits for child to close, if close (f[1]) returns no error it continues ? (btw what does the “0” stand for in wait(0)) and only then continues ?

What am I misunderstanding? am I getting something right ?

I thought I should mention that I did some research using man but I find it tremendously confusing. As far as I am concerned they are for users who already either know what they are doing but forgot some details (like what to include and what -p does) or people who have a more than basic understanding.

  • 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-05T08:57:26+00:00Added an answer on June 5, 2026 at 8:57 am
    1. pipe creates two file descriptors. One you write to and the other you read from. What you write into one, you can read from the other. File descriptors in UNIX are integers (int).
    2. File descriptors are inherited by child processes. Hence the pipe enables one to communicate between processes. In this case the child writes some data that the parent can then read
    3. Instead of using 8 in the read/write about, use sizeof i.e. sizeof(int). The compile will give the right value for the number of bytes that store and int
    4. wait(0) waits for the child to terminate.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Of course, I can explain it in whole books. But I read a few
I'm in a class that has a labview course, the teacher gave us a
Of course we can do this, but is it alright to do so? Are
Recently a teacher said PHP isn't a real programming language, but only gave, in
I remember when I was in some course of C programming, a teacher once
I attend a database course at my school. The teacher gave us a simple
In our discrete mathematics course in my university, the teacher shows his students the
I'm a teacher(instructor) of CS in the university. The course is based on Cormen
I have a data model in Doctrine/symfony. I have a 'Course' which has many
Look at code below: <?php $output += <table border='1'> <tr> <th>Session ID</th> <th>TeacherUsername</th> <th>Teacher

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.