Hello Michael,
I've investigated the effects of sched_setscheduler() on threads. Things
seem worse than I expected.
AFAICS, you can change scheduling attribute of a thread using
sched_setscheduler(3) without the changes being reflected in the
thread's scheduling parameters as retrieved with pthread_getschedparam(3).
Output from the schedtest program below:
<copy>
main > call sched_setscheduler(0,FIFO,1)
main > pthread_getschedparam() : Policy=FIFO, prio=1
main > sched_getscheduler() : Policy=FIFO, prio=1
-----
Thread > pthread_getschedparam() : Policy=*NOT* FIFO, prio=0
Thread > sched_getscheduler() : Policy=*NOT* FIFO, prio=0
-----
Thread > call sched_setscheduler(0,FIFO,2)
Thread > pthread_getschedparam() : Policy=*NOT* FIFO, prio=0
Thread > sched_getscheduler() : Policy=FIFO, prio=2
-----
main > pthread_getschedparam() : Policy=FIFO, prio=1
main > sched_getscheduler() : Policy=FIFO, prio=1
-----
</copy>
Let's hope that my program is broken!
A+,
Loïc
--
/***************************************************************************/
/* schedtest.c- test interaction between sched_setscheduler and threads
/***************************************************************************/
/*
* compile with: cc -pthread schedtest.c -o schedtest
*/
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
/**********************************************************************/
/* our macro for errors checking */
/**********************************************************************/
#define COND_CHECK(func, cond, errv) \
if ( (cond) ) \
{ \
fprintf(stderr, "\n[CHECK FAILED at %s:%d]\n| %s(...)=%d (%s)\n\n",\
__FILE__,__LINE__,func,errv,strerror(errv)); \
exit(EXIT_FAILURE); \
}
#define UnixCheck(func,rc) COND_CHECK(func, (rc==-1), errno)
#define PthreadCheck(func,rc) COND_CHECK(func,(rc!=0), rc)
void
sched_fifo(const char* thr, int prio)
{
struct sched_param param;
int rc;
printf("%s > call sched_setscheduler(0,FIFO,%d)\n", thr, prio);
param.sched_priority = prio;
rc = sched_setscheduler(0, SCHED_FIFO, ¶m);
UnixCheck("sched_setscheduler", rc);
}
void
sched_info(const char* thr)
{
struct sched_param param;
int policy;
int rc;
rc = pthread_getschedparam(pthread_self(), &policy, ¶m);
PthreadCheck("pthread_getschedparam", rc);
printf("%s > pthread_getschedparam() : Policy=%s, prio=%d\n",
thr,
(policy==SCHED_FIFO) ? "FIFO" : "*NOT* FIFO",
param.sched_priority);
policy = sched_getscheduler(0);
UnixCheck("sched_getscheduler", policy);
rc = sched_getparam(0, ¶m);
UnixCheck("sched_getparam", rc);
printf("%s > sched_getscheduler() : Policy=%s, prio=%d\n",
thr,
(policy==SCHED_FIFO) ? "FIFO" : "*NOT* FIFO",
param.sched_priority);
printf("-----\n");
}
void*
thread(void* ignore)
{
sleep(2);
sched_info("Thread");
sched_fifo("Thread", 2);
sched_info("Thread");
return NULL;
}
int
main()
{
pthread_t tid;
int rc;
rc = pthread_create(&tid, NULL, thread, NULL);
PthreadCheck("pthread_create", rc);
sched_fifo("main ", 1);
sched_info("main ");
rc = pthread_join(tid, NULL);
PthreadCheck("pthread_join", rc);
sched_info("main ");
return EXIT_SUCCESS;
}
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html