2

I want to make a shell script that finds all active processes and to print to the user the scheduling policy.I want the result to be like this.

pid 3042's current scheduling policy: SCHED_OTHER pid 3042's current scheduling priority: 0 pid 3043's current scheduling policy: SCHED_OTHER pid 3043's current scheduling priority: 0 pid 3044's current scheduling policy: SCHED_OTHER pid 3044's current scheduling priority: 0

I have managed to do this but only for a single process with the use of ps and chrt commands.

  • 1
    For the schuduling policy, you can parse the output from /proc/pid/sched. I will try to write a script later – Karim Manaouil Nov 28 '17 at 19:01
1

I've come up with the following command using ps and awk which gives very similar output with only a small difference in that it shows abbreviated names of the scheduling class. The following mapping could help you :

  • TS is SCHED_OTHER
  • RR is SCHED_RR
  • FF is SCHED_FIFO

The command:

ps -e -o s,pid,cls,pri | grep ^R | awk -v sq="'" '{print "pid",$2,sq,"s current scheduling policy:",$3,"\npid",$2,sq,"s current priority:",$4}'

Running the above command on my host gives the following output:

pid 8456 ' s current scheduling policy: TS 
pid 8456 ' s current priority: 19
pid 12552 ' s current scheduling policy: TS 
pid 12552 ' s current priority: 19

EDIT

Based on the comment, the following command gives an exact output using chrt and assuming that an active process is a running or a runnable process R:

ps -e -o s,pid | grep ^R | awk '{system("chrt -p " $2)}'

In case you want to get the output for all the processes (Running, Sleeping, Stopped & Zombie), you would like to use the following command:

ls /proc | grep -e ^[0-9] | awk '{system("chrt -p " $0)}'|more
  • 1
    Actually you don't have to print all that.If you just run the command chrt -p 8456 for example it has exactly the desirable result.My struggle is when i try to use the chrt command for 2 or more processes.If you got any other ideas i'd be grateful. – Spyros Nov 29 '17 at 12:06
  • @Spyros I edited the answer to include the update you have requested. Assuming that an active process is a running or a runnable process (State = R). – Karim Manaouil Nov 29 '17 at 17:56
  • I didin't specify that i want to do that only for the current user and just for the active processes of that user.If you want to try that scenario you are free to do so.I found a solution i dont know if its optimal but i'll post it later in case someone faces the same problem.Thank you for your answer anyway. – Spyros Nov 30 '17 at 10:09
  • @Spyros You didn't mention all those details in your original description. You can actually do minor changes on the ps command above to fit your exact needs. However, posting your solution is a valuable share with the community. – Karim Manaouil Dec 1 '17 at 20:47
  • Manaouli I think I mentioned it was about active processes but I didn't mention clearly that they had to be user's processes only. Anyway thank you again for the time you spent answering. – Spyros Dec 1 '17 at 23:46
1

I found two ways for doing it they may not be optimal but they get the job done.

  1. #! /bin/bash ps -u |grep [0-9]|awk '{print $2}' > test.txt cat test.txt |while read line do chrt -p $line 2 > /dev/null done

  2. 2.

With this way you don't create unnecessary file.

ps -u|grep [0-9]|awk '{system("chrt -p" $2)} 2 > /dev/null

0

ps -c is OK, but most real applications only change the scheduling policy of a subset of their threads. So they might be running partly in realtime, but will just show up as TS. For a full view, look at the individual threads by adding a -T:

eric ) ps -cT -p (pidof pulseaudio)
    PID    SPID CLS PRI TTY          TIME CMD
  88821   88821 TS   30 ?        00:00:25 pulseaudio
  88821   88822 RR   45 ?        00:00:22 alsa-sink-ALC25
  88821   88823 RR   45 ?        00:00:00 alsa-source-ALC
eric ) ps -cT -p (pidof jackdbus)
    PID    SPID CLS PRI TTY          TIME CMD
  86902   86902 TS   19 ?        00:00:00 jackdbus
  86902  109091 TS   19 ?        00:00:00 jackdbus
  86902  109092 FF   50 ?        00:00:00 jackdbus
  86902  109093 TS   19 ?        00:00:00 jackdbus

So from here you can see jack is running in FIFO and pulseaudio is running in round robin. Both of those processes just read TS if -T isn't specified.

If the commands don't work, switch to a better shell and try again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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