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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:23:02+00:00 2026-05-26T12:23:02+00:00

I need to simulate the following bash commands using C under Linux (with fork,

  • 0

I need to simulate the following bash commands using C under Linux (with fork, exec, kill, signal, wait, waitpid, dup2, open, sleep, pipe etc).

[0] echo 'tail-f $1' > /tmp/rtail
[1]/tmp/rtail ~/.bash_history >> /tmp/1.txt &
PID of process [1] should be saved.
[2] Expect termination of the command started on step [1]. After termination print on the screen: "Program 1 terminated."

So far I have this code:

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>  
#include <sys/wait.h> 

int main(int argc, char *argv[]) {
    pid_t pID = fork();
    if (pID == 0) // child
    {
        int file = open("/tmp/rtail", O_CREAT | O_WRONLY);

        //Now we redirect standard output to the file using dup2
        dup2(file, 1);

        puts("tail -f $1");
        close(file);
        system("chmod 777 /tmp/rtail");
        exit(0);
    } else if (pID < 0) // failed to fork
    {
        printf("Failed to fork");
        exit(1);
        // Throw exception
    } else // parent
    {
        pid_t pID2 = fork();
        if (pID2 == 0) {
            char tmp1[20];
            sprintf(tmp1, "echo %i > /tmp/pidprog1", getpid());
            system(tmp1);


            int file = open("/tmp/1.txt", O_APPEND | O_WRONLY);

            //Now we redirect standard output to the file using dup2
            dup2(file, 1);
            FILE* proc = popen("sh /tmp/rtail ~/.bash_history", "r");
            char tmp[20];
            while (fgets(tmp, 40, proc) != NULL) {
                printf(tmp);
            }
            fclose(proc);

            exit(0);
        }
        else if (pID2 < 0) // failed to fork
        {
            printf("Failed to fork");
            exit(1);
            // Throw exception
        } else {
            FILE* fl = fopen("/tmp/pidprog1", "r");
            char buff[10];
            fgets(buff, 10, fl);
            int pidc = atoi(buff);
            fclose(fl);
            int status;
            waitpid(pidc, &status, 0);
            printf("Program 1 terminated\n");
        }
    }

    // Code executed by both parent and child.  

    return 0;
}

The problem is that when I manually kill the process using PID saved into /tmp/pidprog1, parent process doesn’t stop waiting and doesn’t print “Program 1 terminated” line.

  • 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-26T12:23:02+00:00Added an answer on May 26, 2026 at 12:23 pm

    The parent is very likely reading a garbage value into pidc. You are doing nothing to ensure that the grandchild has actually written the pid before the parent tries to read it. You need to use wait to ensure that valid pids are in the file. (Or, just keep track of the pids from the return value of fork.)

    You are not doing enough error checking: what happens if any open fails? (eg, when you try
    to open /tmp/1.txt for appending but it doesn’t already exist?)

    Why are you using fgets to read 40 characters into a buffer of size 20?

    Why are you dup’ing and using fputs instead of just writing to the fd?

    Why are you printing error messages to stdout instead of stderr ( use perror ).

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

Sidebar

Related Questions

I have started testing my UI using qUnit, so I need to simulate some
I need to simulate Poisson wait times. I've found many examples of simulating the
I need to simulate a test scenario in which I call the getBytes() method
I need to simulate multiple embedded server devices that are typically used for motor
I need to simulate how my application will look when a user is driving
I'm writing an app where I need to simulate key press events on a
I am developing a Windows .NET application (WinForms) and I need to simulate a
I'm looking for a jquery plugin to simulate a vertical marquee. I need it
I am working on a Java project and need to have a keypress simulate
I have a class derived from Dictionary. I need this class to simulate a

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.