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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:53:49+00:00 2026-06-17T21:53:49+00:00

The question is the title. I couldn’t figure out why the terminal will shut

  • 0

The question is the title. I couldn’t figure out why the terminal will shut down immediately after ‘ls’ is executed. A Linux shell is like this:

1.while (1) {

2. char *cmd = read_command();
3. int child_pid = fork();
4. if (child_pid == 0) {
5. exec(cmd);
6. }else {
7. waitpid(child_pid);
8. }
9.}

So, if we run ‘exec ls’ in shell, cmd is a string of ‘exec ls’. A child process is forked in line 3. In line 5, exec(cmd) will replace the child process but won’t affect the father process. If the father process is not affected, why the terminal shuts down then?

Please show me the flaws in my reasoning above.

  • 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-17T21:53:50+00:00Added an answer on June 17, 2026 at 9:53 pm

    If you run ls, your shell process will start up another process to run the ls program, then it will wait for it to finish. When it finishes, control is returned to the shell.

    With exec ls, you actually replace your shell program in the current process with the ls program so that, when it finishes, there’s no shell waiting for it.

    Most likely, you will have either a terminal program or init as the parent which is what will take over when your process exits. That’s why your shell disappears, because you explicitly told it to.

    See this answer for an explanation of the shell/ls (non-exec) situation.


    As for your update, the shell does not always create a separate process to do stuff. There are a large number of internal commands (such as cd or alias) that do not involve making other processes (this depends on your shell, of course but, as one example, you can see the bash internal commands by entering man bash-builtins at a command prompt).

    exec is one of these. It simply replaces the shell itself (ie, not a forked child process) with the program you specify. That’s why it doesn’t act as you think.

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

Sidebar

Related Questions

I hope the title of this question is somewhat clear, I couldn't figure out
Sorry for vague question title, i couldn't figure out something more specific. I have
I really couldn't figure out a good title for this question, but I have
I couldn't figure out how to word the title of this question to make
I will have to admit the title of this question sucks... I couldn't get
This might be a noob question. But I couldn't figure it out by myself.
Possible Duplicate: UITextField focus the title may be a bit confusing.. couldn't figure out
I couldn't find a suitable title for my question. Here is the problem... After
Not entirely sure my question title describes what I want to do, but couldn't
Sorry for the title, I couldn't argue something better. Here is my question: 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.