Question 1
It is possible for an user to use real time priority for a process as well. This configuration could be set from /etc/security/limits.conf
file. I see the below contents in that file.
# /etc/security/limits.conf
#
#Each line describes a limit for a user in the form:
#
#<domain> <type> <item> <value>
If we check the item section, we see the below entry which enables to set a real time priority for the users.
# - rtprio - max realtime priority
Question 2 and Question 3
To set scheduling policy to SCHED_FIFO
, enter:
chrt -f -p [1..99] {pid}
To set scheduling policy to SCHED_RR
, enter:
chrt -r -p [1..99] {pid}
So to answer question 3, we should verify the scheduling algorithms available and the priorities using the chrt -m
command and then use any scheduling algorithm that suits our need. To set different priorities, we could use the commands as above.
#!/bin/sh
#This is a process with an id of $$
( sleep 1000 )& #this creates an idle child
( (sleep 1000)& sleep 1000 )& #this creates an idle child and grandchild
wait #this waits for direct children to finish
Running the above as ./1.sh &
on my system created the following process tree:
$ command ps -o pid,ppid,pgrp,stat,args,wchan --forest
PID PPID PGRP STAT COMMAND WCHAN
24949 4783 24949 Ss /bin/bash wait
25153 24949 25153 S \_ /bin/sh ./1.sh sigsuspend
25155 25153 25153 S | \_ sleep 1000 hrtimer_nanosleep
25156 25153 25153 S | \_ sleep 1000 hrtimer_nanosleep
25158 25156 25153 S | \_ sleep 1000 hrtimer_nanosleep
You can notice that the tree has the same process group (PGRP) of 25153, which is identical to the PID of the first process.
The shell creates a process group whenever you start a new command in interactive mode (or with job control explicitly turned on).
The PGRP mechanism allows the shell to send a signal to the whole process group at once without creating a race condition. This is used for job control, and when your script runs and a foreground job, for sending:
- (SIG)INTR when the user presses C-C
- (SIG)QUIT when the user presses C-\
- (SIG)STP when the user presses C-Z
You can do the same by doing, for example:
kill -INTR -25153
where INTR is the signal and 25153 is the process group you want to send the signal to. The -
before the 25153 means you're targeting a PGRP id rather than a PID.
In your case, the signal you should be sending is -TERM
(request termination). Term is the default signal kill
sends, however, you have to specify it explicitly if you're targeting a group rather than a PID.
kill -TERM -25153
If you want to kill the process group of the last background job you started, you can do:
kill -TERM -$!
Best Answer
Your confusion stems from mixing two things: (1) keeping the process descriptors organized, and (2) the parent/child relationship.
You don't need the parent/child relationship to decide which process to run next, or (in general) which process to deliver a signal to. So, the Linux
task_struct
(which I found inlinux/sched.h
for the 3.11.5 kernel source) has:You're correct, a tree struct exists for the child/parent relationship, but it seems to be concealed in another list, and a pointer to the parent.
The famed doubly-linked list isn't obvious in the 3.11.5
struct task_struct
structure definition. If I read the code correctly, the uncommented struct elementstruct list_head tasks;
is the "organizing" doubly-linked list, but I could be wrong.