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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:00:33+00:00 2026-06-11T17:00:33+00:00

I’m writing a program in which I use the system call fork() to create

  • 0

I’m writing a program in which I use the system call fork() to create a child process, and then create a grandchild, as well as creating a pipe between the child and grandchild. I think my implementation is fairly good, however when I run the program, it simply skips right through the prompts in my code.

Essentially we have this:

-Process starts
Fork() create child
Child creates pipe
Fork() creates grandchild, pipe inherited.

TL;DR- Code skips through UI prompts, not sure if I’m entering data correctly, not sure if I’m reading data into processes correctly.

How do I read inputs down the pipe?

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>

void main(int argc, char *argv[])
{
        int p[2];
        int pid, pid2;

        pid = fork();

        if(pid == 0)
        {
                pipe(p);
                pid2 = fork();

                switch(pid2)
                {
                        case -1:
                                printf("CASE 1");
                                exit(-1);
                        case 0:
                                close(0);
                                dup(p[0]);
                                close(p[0]);
                                close(p[1]);
                                execl("./Sort/sort", 0);
                                break;
                        default:
                                close(1);
                                dup(p[1]);
                                close(p[1]);
                                close(p[0]);
                                execl("./Pre/pre", 0);
                                break;
                }
          }
          else
          {
                wait(pid);
                printf("Process Completed\n");
                exit(0);
           }
}

Child process for pre:

#include <stdio.h>

void main (int argc, char *argv[])
{
        char n1[20];
        int g1;
        FILE *ofp, *ifp;
        int track;

        ofp = fopen("output.txt", "w");

        while(track != -1)
        {
                printf("Please enter the student's grade and then name, ");
                printf("separated by a space: ");
                scanf("%3d %s", &g1, n1);

                if (g1 >= 60)
                {
                        fprintf(ofp, "%s\n", n1);
                }

                printf("Add another name?(-1 to quit, 0 to continue): ");
                scanf("%d", &track);
        }
        fclose(ofp);

        ifp = fopen("output.txt", "r");
        printf("Students that made a 60+:\n");
        while(fscanf(ifp, "%s", n1) == 1)
                printf("%s\n", n1);

        fclose(ifp);

Child process for sort:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int stringcmp(const void *a, const void *b)
{
    const char **ia = (const char **)a;
    const char **ib = (const char **)b;
    return strcmp(*ia, *ib);
}

void main(int argc, char *argv[])
{
     int i = 0;
     int num = 0;
     int j = 0;
     char name[20];

          printf("How many names would you like to enter? ");
          scanf("%d", &num);

          char **input = malloc(num * sizeof(char*));

          for (i=0; i < num; i++)
          {
                printf("Please input a name(first only): ");
                scanf("%s", name);
                input[i] = strdup(name);
          }

          qsort(input, num, sizeof(char *), stringcmp);

          printf("Names:\n");

          for(j = 0; j < num; j++)
               printf("%s\n", input[j]);


          for( i = 0; i < num; i++ ) free(input[i]);
          free(input);
  • 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-11T17:00:34+00:00Added an answer on June 11, 2026 at 5:00 pm

    Observe that pre outputs prompts to stdout. That text will end up in the pipe, which is going to end up in sort‘s stdin.

    You should fprintf all your prompts to stderr.

    pre also doesn’t output text in the format expected by sort; sort expects an integer n followed by n words (first names). So, that should be the only text that pre outputs on stdout; everything else needs to go to a file or stderr.

    P.S. Also, use dup2(p[0], 0) instead of close(0); dup(p[0]) as it makes your intent clearer, as well as potentially avoiding threading issues (of course only relevant if you have threads, but it’s worth keeping in mind).

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
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 using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.