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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:26:58+00:00 2026-05-20T10:26:58+00:00

I have to write program that create process using pipe() . My first task

  • 0

I have to write program that create process using pipe().

My first task is to write a parent process that generates four child processes using the fork() function.

Once the fork() is successful, replace the child process with another process rover1, rover2, rover3, and rover4, though all of them have the same code.

The function of the processes is as follows.

  1. Each child process is initially given its own number. It receives a new number from the parent. Using the following formula it creates its own new number as follows and forwards it to the parent:

    mynumber = (3 * mynumber + 4 * numberreceived)/7

  2. This process continues until the parent sends the message that the system is stable. The parent also has its initial number. It receives numbers of all the children and computes its new number as follows:

    mynumber = (3 * mynumber + numbers sent by all the children)/7

  3. The parent will send this number to all its children. This process will continue until the parent finds that its number is not changing anymore. At that time it will tell the children the system has become stable.

This is what I did but my professor said I have to use exec() to execute the child and replace child process with another child process. I am not sure how to use exec(). Could you please help me with this.

I am attaching only first child generation.

// I included stdio.h, unistd.h stdlib.h and errno.h 
int main(void)
{
  // Values returned from the four fork() calls
  pid_t rover1, rover2, rover3, rover4;

  int parentnumber, mynumber1, mynumber2, mynumber3, mynumber4;

  int childownnumber1 = 0, status = 1, childownnumber2 = 0,
      childownnumber3 = 0, childownnumber4 = 0, numberreceived = 0;

  printf("Enter parent number: ");
  printf("%d", parentnumber);
  printf("Enter each children number");
  printf("%d %d %d %d", mynumber1, mynumber2, mynumber3, mynumber4);

  // Create pipes for communication between child and parent
  int p1[2], p2[2];
  // Attempt to open pipe
  if (pipe(p1) == -1) {
    perror("pipe call error");
    exit(1);
  }
  // Attempt to open pipe
  if (pipe(p2) == -1) {
    perror("pipe call error");
    exit(1);
  }

  // Parent process generates 4 child processes
  rover1 = fork();

  // if fork() returns 0, we're in the child process;
  // call exec() for each child to replace itself with another process
  if (rover1 == 0) {
    for(; numberreceived != 1; ) {  
      close(p1[1]); // Close write end of pipe
      close(p2[0]); // Close read end of second pipe

      // Read parent's number from pipe
      read(p1[0], &numberreceived, sizeof(int));

      if (numberreceived == 1) {
        // System stable, end child process
        close(p1[0]);
        close(p2[1]);
        _exit(0); // End child process
      }

      mynumber1 = (int)((3*mynumber1 + 4*numberreceived)/7.0);

      printf("\nrover1 number: ");
      printf("%i", mynumber1);

      // Write to pipe
      write(p2[1], &mynumber1, sizeof(int));    
    }       
  }
  /* Error:
   * If fork() returns a negative number, an error happened;
   * output error message
   */
  if (rover1 < 0) {
    fprintf(stderr,
            "can't fork, child process 1 not created, error %d\n",
            errno);
    exit(EXIT_FAILURE);
  }
}
  • 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-20T10:26:58+00:00Added an answer on May 20, 2026 at 10:26 am

    The exec family of functions is used to replace the current process with a new process. Note the use of the word replace. Once exec is called, the current process is gone and the new process starts. If you want to create a separate process, you must first fork, and then exec the new binary within the child process.

    Using the exec functions is similar to executing a program from the command line. The program to execute as well as the arguments passed to the program are provided in the call to the exec function.

    For example, the following execcommand* is the equivalent to the subsequent shell command:

    execl("/bin/ls", "/bin/ls", "-r", "-t", "-l", (char *) 0);

    /bin/ls -r -t -l

    * Note that “arg0” is the command/file name to execute


    Since this is homework, it is important to have a good understanding of this process. You could start by reading documentation on pipe, fork, and exec combined with a few tutorials to gain a better understanding each step.

    The following links should help to get you started:

    • IBM developerWorks: Delve into UNIX process creation
    • YoLinux Tutorial: Fork, Exec and Process control
    • Pipe, Fork, Exec and Related Topics
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have to write program, which will create two child processes These processes would
I have been assigned wit the task to write a program that takes a
I wrote a little program using fork to create new processes which use pipe
I am trying to write a c++ program that creates a child process, runs
I have to write a program that sniffs network packets (part1-the simple part). And
I have to write a Java program that tells what coins to give out
I have to write a daemon program that constantly runs in the background and
I wanted to write a program that test if two files are duplicates (have
I have a program that looks like this. I need to consistently write something
I have a robot that I need to write an autonomous program for. 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.