Monday, April 29, 2024
 Popular · Latest · Hot · Upcoming
15
rated 0 times [  15] [ 0]  / answers: 1 / hits: 29833  / 3 Years ago, fri, july 23, 2021, 3:00:19

By default programs run with Time Sharing (TS policy) on Linux. How to run a program with SCHED_RR policy on Linux from command line?


Thanks for giving information about chrt(1) command.
I have used the command to run Firefox with RR policy,
but as you see below, only main thread of Firefox runs
with RR policy. Could you tell me how to run all other
threads of Firefox also with RR policy.


$ ps -Lo pid,tid,class 2051
PID TID CLS
2051 2051 RR
2051 2055 TS
2051 2056 TS
2051 2057 TS
2051 2058 TS
2051 2059 TS
2051 2060 TS
2051 2061 TS
2051 2063 TS
2051 2067 TS
2051 2068 TS
2051 2069 TS
2051 2070 TS
2051 2072 TS
2051 2073 TS
2051 2074 TS
2051 2075 TS
2051 2077 TS
2051 2078 TS
2051 2080 TS
2051 2356 RR
2051 2386 TS
2051 2387 TS

Edit:
I ran the following simple pthreads program and tested like the above.
Unfortunately chrt command only changes the class of the main thread.
Please see below.


$ ps -Lo pid,tid,class 3552
PID TID CLS
3552 3552 TS
3552 3553 TS
3552 3554 TS
3552 3555 TS
3552 3556 TS
3552 3557 TS

$ sudo chrt --rr -p 30 3552
...
$ ps -Lo pid,tid,class 3552
PID TID CLS
3552 3552 RR
3552 3553 TS
3552 3554 TS
3552 3555 TS
3552 3556 TS
3552 3557 TS

---- Program----


#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5

void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!
", tid);
long k = 1;
long a[10000];
int i = 1;
long b[10000];

for (k = 0; k < 400000000; k++) {
if (i == 9999) {
i = 1;
}
a[i] = ((k + i) * (k - i))/2;
a[i] = k/2;
b[i] = i * 20;
b[i] = a[i] - b[i];
i++;
int j = 0;
for (j = 0; j < i; j++) {
k = j - i;
}
}

pthread_exit(NULL);

}

int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %ld
", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d
", rc);
exit(-1);
}
}
pthread_exit(NULL);
}

More From » schedule

 Answers
6

Use the chrt command with chrt --rr <priority between 1-99> <command>


Example:


# Note: `sudo` is not required if you are root
chrt --rr 99 ls

# use `sudo` otherwise
sudo chrt --rr 99 ls

Note that setting SCHED_RR require root permissions, so you either have to be root or run it with sudo.


You can also use chrt to give a running process realtime priority:


chrt -p --rr <priority between 1-99> <pid>


The same commands applies for other scheduling classes as well, albeit with a different parameter instead of -rr:


Scheduling policies:
-b | --batch set policy to SCHED_BATCH
-f | --fifo set policy to SCHED_FIFO
-i | --idle set policy to SCHED_IDLE
-o | --other set policy to SCHED_OTHER
-r | --rr set policy to SCHED_RR (default)

Edit:


In the Firefox case, it must be spesific to Firefox. In a multithreaded application I wrote myself, all the threads keep the RR class. As seen in your output, two threads have class RR, so it's not only the parent thread either.


Edit 2:


Try starting the process with chrt instead of rescheduling an existing pid. It appears that if you reschedule, only the first thread gets RR class. However, if you start it with chrt, every thread gets it.


[#44388] Friday, July 23, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
restlerfin

Total Points: 347
Total Questions: 112
Total Answers: 108

Location: Ukraine
Member since Wed, Dec 16, 2020
3 Years ago
;