gpt4 book ai didi

c - 线程显式调度 POSIX API 给出错误

转载 作者:行者123 更新时间:2023-11-30 16:39:55 24 4
gpt4 key购买 nike

我试图创建几个线程,其中一些线程将遵循开发人员提供的显式调度设置,并编写了以下代码:

pthread_t thid1,thid2,thid3,thid4;

int main()
{

int ret;
struct sched_param param1;

pthread_attr_t pthread_attr1,pthread_attr4;

ret = pthread_attr_init(&pthread_attr4);
if(ret>0) {
printf("error in the thread attr initialization\n");
}

ret = pthread_create(&thid4,&pthread_attr4,display_status_thread,NULL);

if(ret>0) { printf("error in thread creation for producer\n"); exit(4); }


pthread_attr_init(&pthread_attr1);

//setting the policy to realtime, priority based
pthread_attr_setschedpolicy(&pthread_attr1,SCHED_FIFO);

//realtime priority of 1 ( 1-99 is the range)
param1.sched_priority = 20;
pthread_attr_setschedparam(&pthread_attr1, &param1);

ret = pthread_attr_setinheritsched(&pthread_attr1,PTHREAD_EXPLICIT_SCHED);

if(ret>0) { perror("pthread_attr_setinheritsched failed."); exit(1); }

ret = pthread_create(&thid1,&pthread_attr1,func1,NULL);
//Here comes error " error in creating func1 thread.
if(ret>0) { printf("error in thread creation for func1\n"); exit(1); }

ret = pthread_create(&thid2,NULL,func2,NULL);
if(ret>0) { printf("error in thread creation for func2\n"); exit(2); }

ret = pthread_create(&thid3,NULL,func3,NULL);
if(ret>0) { printf("error in thread creation for func3\n"); exit(3); }


pthread_join(thid1,NULL);

pthread_join(thid2,NULL);

pthread_join(thid3,NULL);

pthread_join(thid4,NULL);

exit(0);

}

我在设置继承调度创建显式线程的地方收到错误。

出了什么问题? pthread_attr_setinheritsched() 的用法是否在语法或其他方面不正确?

谢谢

最佳答案

我很确定如果您显示错误,您就会发现问题

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. ATTRIBUTES top

这可能会打印正确的错误消息。

errno = ret; 
perror(msg);
exit(EXIT_FAILURE);

编辑:问题是EPERM,通过升级到sudo,它现在可以运行。

关于c - 线程显式调度 POSIX API 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46874369/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com