|
INTERVIEW QUESTIONS
OPERATING SYSTEMS
UNIX PROGRAMMING
DETAILS
Question: How can a process detect if it's running in the background?
Answer: First of all: do you want to know if you're running in the background, or if you're running interactively? If you're deciding whether or not you should print prompts and the like, that's probably a better criterion. Check if standard input is a terminal:
sh: if [ -t 0 ]; then ... fi C: if(isatty(0)) { ... }
In general, you can't tell if you're running in the background. The fundamental problem is that different shells and different versions of UNIX have different notions of what "foreground" and "background" mean - and on the most common type of system with a better-defined notion of what they mean, programs can be moved arbitrarily between foreground and background! UNIX systems without job control typically put a process into the background by ignoring SIGINT and SIGQUIT and redirecting the standard input to "/dev/null"; this is done by the shell. Shells that support job control, on UNIX systems that support job control, put a process into the background by giving it a process group ID different from the process group to which the terminal belongs. They move it back into the foreground by setting the terminal's process group ID to that of the process. Shells that do *not* support job control, on UNIX systems that support job control, typically do what shells do on systems that don't support job control.
|
|
|
Category |
Unix Programming Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.2) By 8459 users |
Added on |
8/26/2014 |
Views |
69730 |
Rate it! |
|
|
Question:
How can a process detect if it's running in the background?
Answer:
First of all: do you want to know if you're running in the background, or if you're running interactively? If you're deciding whether or not you should print prompts and the like, that's probably a better criterion. Check if standard input is a terminal:
sh: if [ -t 0 ]; then ... fi C: if(isatty(0)) { ... }
In general, you can't tell if you're running in the background. The fundamental problem is that different shells and different versions of UNIX have different notions of what "foreground" and "background" mean - and on the most common type of system with a better-defined notion of what they mean, programs can be moved arbitrarily between foreground and background! UNIX systems without job control typically put a process into the background by ignoring SIGINT and SIGQUIT and redirecting the standard input to "/dev/null"; this is done by the shell. Shells that support job control, on UNIX systems that support job control, put a process into the background by giving it a process group ID different from the process group to which the terminal belongs. They move it back into the foreground by setting the terminal's process group ID to that of the process. Shells that do *not* support job control, on UNIX systems that support job control, typically do what shells do on systems that don't support job control. Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
How do I find the creation time of a file?
|
View Answer
|
|
How do I ring the terminal bell during a shell script?
|
View Answer
|
|
How do I find the creation time of a file?
|
View Answer
|
|
How do I construct a shell glob-pattern that matches all files except "." and ".." ?
|
View Answer
|
|
How do I tell inside .cshrc if I'm a login shell?
|
View Answer
|
|
How do I redirect stdout and stderr separately in csh?
|
View Answer
|
|
How do I {set an environment variable, change directory} inside a program or shell script and have that change affect my current shell?
|
View Answer
|
|
Why do I get [some strange error message] when I "rsh host command" ?
|
View Answer
|
|
How do I rename "*.foo" to "*.bar", or change file names to lowercase?
|
View Answer
|
|
How do I get the current directory into my prompt?
|
View Answer
|
|
How do I get a recursive directory listing?
|
View Answer
|
|
How do I remove a file with funny characters in the filename ?
|
View Answer
|
|
How do I remove a file whose name begins with a "-" ?
|
View Answer
|
|
What happened to the pronunciation list that used to be part of this document?
|
View Answer
|
|
What are some useful Unix or C books?
|
View Answer
|
|
How does the gateway between "comp.unix.questions" and the "info-unix" mailing list work?
|
View Answer
|
|
What does {some strange unix command name} stand for?
|
View Answer
|
|
When someone refers to 'rn(1)' or 'ctime(3)', what does the number in parentheses mean?
|
View Answer
|
|
Who helped you put this list together?
|
View Answer
|
|
Describe about awk and sed?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All Unix Programming Interview Questions & Answers - Exam Mode /
Learning Mode
|