tags:

views:

95

answers:

2

I asked a related question 'Difference between process group id and job id in Unix', but I have a doubt that was not answered. In that answer, it was written that job id is built in data related to shell (kernel has nothing to do with it), so foreground / background is understanding of shell, but when we do "ps x" then it display in stats as "R+" or "R" as foreground and background process respectively but ps is executed by kernel so how does kernel know that job is back/foreground.

A: 

I wouldn't say 'kernel has nothing to do with it' since the shell uses routines in the kernel to achieve its purposes.

The shell decides whether a process is foreground or background, but the kernel must be aware of it too, and can report the status of processes when the shell asks.

pavium
What kernel routines does shell use?
avd
I think we could say the shell uses *any* useful kernel routine. The kernel could be regarded as a library of routines for accessing disks, display, I/O devices, etc. Any time the shell (or any executable program) needs to access hardware, it would use a kernel routine. New hardware might require special drivers, but new versions of the kernel keep *adding* support for hardware in the form of new routines.
pavium
I need exact routine, I am implementing my own shell, Please tell its urgent
avd
+1  A: 

Job IDs are definitely shell features.

However, the operating system has the notion of current terminal process group ID, which is also used by the shell to facilitate job control. Processes which have the same process group ID as the current terminal process receive keyboard signal such as SIGINT and are able to do I/O on the terminal. These processes are foreground processes. If the process group ID is different, then the process is a background process. The ps command displays + when the process is a foreground process in this notion. It still does not need to know anything about job control as implemented by the shell.

laalto
How does shell informs kernel of current terminal process? Its use exec to execute function. Which other routine is involved to do this?
avd