Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Can someone tell me what the relationship between a specified nice level and child processes ?

for example, if I have a default nice of 0, and i start a script with nice 5, which in turn starts some child processes (in this case about 20 in parallel), what is the nice of the child processes ?

Thanks,

share|improve this question
1  
Only the root user can decrease the niceness of a process, and the value is inherited by child processes in any sensible fork implementation. –  jw013 May 4 '12 at 13:52
    
@jw013 - For clarity, I am scripting, using & to spawn child processes, not using c & fork. –  NWS May 4 '12 at 13:58
2  
unless you are on a non-Unix/Linux OS, it's a fairly safe bet that your shell spawns background processes with fork. –  jw013 May 4 '12 at 14:14

1 Answer 1

up vote 12 down vote accepted

Yes. However, if the nice value of the parent process changes after forking the child processes, the child processes do not inherit the new nice value.

You can easily observe this with the monitoring tool top. If the nice field (NI) is not shown by default, you can add it by pressing f and choosing I. This will add the NI column to the top display.

* I: NI = Nice value

 PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
1937 root      20   0  206m  66m  45m S  6.2  1.7  11:03.67 X                                         

Good information from man 2 fork

fork() creates a new process by duplicating the calling process. The new process, referred to as the child, is an exact duplicate of the calling process, referred to as the parent, except for the following points:

  • The child has its own unique process ID, and this PID does not match the ID of any existing process group (setpgid(2)).
  • The child's parent process ID is the same as the parent's process ID.
  • The child does not inherit its parent's memory locks (mlock(2), mlockall(2)).
  • Process resource utilizations (getrusage(2)) and CPU time counters (times(2)) are reset to zero in the child.
  • The child's set of pending signals is initially empty (sigpending(2)).
  • The child does not inherit semaphore adjustments from its parent (semop(2)).
  • The child does not inherit record locks from its parent (fcntl(2)).
  • The child does not inherit timers from its parent (setitimer(2), alarm(2), timer_create(2)).
  • The child does not inherit outstanding asynchronous I/O operations from its parent (aio_read(3), aio_write(3)), nor does it inherit any asynchronous I/O contexts from its parent (see io_setup(2)).
share|improve this answer
1  
Thank you, things work nicely now :) –  NWS May 4 '12 at 14:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.