1

I am trying to write a simple shell which accepts command line input and execute it as a background task.

How do I allow the child process to report to the parent process once it is completed, like the Bash shell?

user@user-desktop:~$ sleep 10 &
[1] 3729
user@user-desktop:~$ sleep 2 &
[2] 3730
user@user-desktop:~$ 
[1]-  Done                    sleep 10
[2]+  Done                    sleep 2

2 Answers 2

3

Since this is your homework, I won't give you full answer.

The GNU Glibc manual list the requirnment for job control shell. Let's see if you can understand it.

Basically:

  • you have to change the control terminal to make jobs run in background

  • you have to handle SIGCHLD (or wait) to monitor jobs

Ask again after you have read it.

Sign up to request clarification or add additional context in comments.

2 Comments

hi, I have read gnu.org/software/libc/manual/html_node/…. But I still don't have any idea in implementing the control terminal you are mentioning. Can you elaborate further?
@liangtech, click the "next" link. It's the "tcsetpgrp" in 27.6.4 Foreground and Background
0

You need to code a signal handler that handles the SIGCHLD (sometimes named SIGCLD) signal which is sent to parent processes when a child terminates.

6 Comments

And do what in the signal handler ?
I understand I can implement a signal handler to "listen" for SIGCHLD when the child process terminates it will send a SIGCHLD signal. But what is the [2]+ beside the command [2]+ Done sleep 2
@liangteh: The "done" message is not written by the child - it's the parent process (the shell) that writes out that one of the child processes has terminated. ---
@ktf how about [1] and [2] what are they suppose to represent?
@liangteh Its the job number. all processes that run in the background of a shell are numbered. with the command 'fg 1' you can get the job 1 to the foreground again.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.