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

  • Home
  • SEARCH
  • 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 7987821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:15:26+00:00 2026-06-04T12:15:26+00:00

Title may be a little confusing, so let me explain. I am trying to

  • 0

Title may be a little confusing, so let me explain. I am trying to write a simple shell to practice my programming. I have got the get a command, fork, exec loop working. However, when I press CTRL-C while child process is still executing, my shell terminates, instead of child process (but child process would keep running). Here is the main function:

int main()
{
    dynarray *args; /* pointer to a dynamic array */
    int bytes_read;
    size_t nbytes = 0;
    char *command;
    pid_t pid;
    printf("Enter command: ");
    while ((bytes_read = getline(&command, &nbytes, stdin)) != -1) {
        if (bytes_read == -1) {
            perror("getline");
            exit(EXIT_FAILURE);
        }
        pid = fork();
        if (pid == -1) {
            perror("fork");
            exit(EXIT_FAILURE);
        }
        else if (pid == 0) { /* child process */
            args = newdynarray();
            char *arg = strtok(command, " \n");
            while (arg != NULL) {
                addstring(args, arg);
                arg = strtok(NULL, " \n");
            }
            if (args->nval == 0) {
                freedynarray(args);
                continue;
            }

            addstring(args, NULL);
            char *fullpath = find_executable(args->strings[0]);
            if (fullpath == NULL) {
                fprintf(stderr, "Couldn't find executable: %s\n", command);
                exit(EXIT_FAILURE);
            }
            if (execv(fullpath, args->strings) == -1) {
                perror("execv");
                exit(EXIT_FAILURE);
            }
        } else {
            int status;
            waitpid(pid, &status, 0);
        }
        printf("Enter command: ");
    } 
    return 0;
}

I didn’t include other parts, because I don’t think they are relevant. How can I make my child process catch all the input from stdin until it terminates?

  • 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-04T12:15:28+00:00Added an answer on June 4, 2026 at 12:15 pm

    How can I make my child process catch all the input from stdin until it terminates? Signals generated from stdin keys (such as control C) will be sent to the last process to use stdin, so there’s nothing you can do unless you can force your child to use the path.

    Instead, you need to create a signal handler in your shell process to catch SIGINT (and others), and resend the signal (using the kill() function) to the process you want to receive it.

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

Sidebar

Related Questions

The title may have been confusing, but please let me explain: Currently when I
The phrasing of the title may be a little confusing. I have an app
Ok Title may not be clear so let me explain. I have 2 classes,
Ok so my question title may have been a little confusing. Here's my example:
The title may be a little confusing, but I don't know how else to
Ok so the title may have been confusing so i have posted 2 code
The title may sound a little strange, so I'll try to explain my problem:
I understand the title may be a little vague, but, hopefully, I can explain
I realise that the title may be very confusing, so to explain what I
Ok. I know the title may be confusing. Logic That I have implemented is

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.