今晚在跑之前写的一个创建新线程的代码,发现:执行设置系统调度策略以及调度参数相关的参数后,调用pthread_create生成新的任务时,得到返回值为1的情况:
pthread_attr_init(&attr);
if (desc->stack_size != 0)
{
pthread_attr_setstacksize(&attr, desc->stack_size);
}
pthread_attr_getinheritsched(&attr, &inher);
if (inher != PTHREAD_EXPLICIT_SCHED)
{
log_warn("change sched!\n");
inher = PTHREAD_EXPLICIT_SCHED;
}
pthread_attr_setinheritsched(&attr, inher);
// 设置线程调度策略
policy = SCHED_FIFO;
pthread_attr_setschedpolicy(&attr, policy);
// 设置调度参数
param.sched_priority = desc->prio;
pthread_attr_setschedparam(&attr, ¶m);
ret = pthread_create(tinfo, &attr, desc->fn, desc->arg);
// TODO: return code = 1 ???
if (ret != 0)
{
log_err("task create err code = (%d)\n", ret);
pthread_attr_destroy(&attr);
free(tinfo);
return NULL;
}
打印信息:
Work in host mode!
Upgrade file = defines.mk
comm rule = 0x10
set port = /dev/ttyUSB0
Success open /dev/ttyUSB0
change sched!
task create err code = (1)
Create Thread phrase core failed!
comm core init failed!
comm init err!
但是从 http://www.man7.org/linux/man-pages/man3/pthread_create.3.html
查找相关返回值信息,找到了返回值为1的情况:
RETURN VALUE top
On success, pthread_create() returns 0; on error, it returns an error
number, and the contents of *thread are undefined.
ERRORS top
EAGAIN Insufficient resources to create another thread.
EAGAIN A system-imposed limit on the number of threads was
encountered. There are a number of limits that may trigger
this error: the RLIMIT_NPROC soft resource limit (set via
setrlimit(2)), which limits the number of processes and
threads for a real user ID, was reached; the kernel's system-
wide limit on the number of processes and threads,
/proc/sys/kernel/threads-max, was reached (see proc(5)); or
the maximum number of PIDs, /proc/sys/kernel/pid_max, was
reached (see proc(5)).
EINVAL Invalid settings in attr.
EPERM No permission to set the scheduling policy and parameters
specified in attr.
其中的ERRORS = EPERM的定义,在 erron.h 头文件中的定义,就是1,从解释看,就是说没有权限来设置调度策略及参数定义。
所以,这个代码涉及到修改调度策略,需要在ROOT权限下来执行。