创建具有不同调度优先级的线程
Creating threads with different scheduling priorities
这里是 C 语言的新用户。我一直在学习创建基本的 C 程序,我的教授给了我们第一个大任务,我绝望地迷路了。
对于其中的一部分,我应该创建三个不同的线程,分别使用 SCHED_OTHER
、SCHED_RR
和 SCHED_FIFO
作为它们各自的调度优先级。
这 3 个线程中的每一个都将等待 "main" 线程发出它们应该启动 运行 a "task".
的信号
此任务是一个具有非生成和非阻塞指令的函数调用,需要 5 到 10 秒才能完成。
基本上,有两个问题:
How to create a thread in C with a defined priority(SCHED_OTHER
, SCHED_RR
, or SCHED_FIFO
) (我如何使用pthread_create()
?)
我将如何着手编写这些任务? (伪代码就可以)
How to I create a thread in C with a defined priority(SCHED_OTHER, SCHED_RR, or SCHED_FIFO) (how do I use pthread_create()?) (pseudocode is fine)
最后一点,"pseudocode is fine"是重要的一点。你应该考虑把它移到问题的前面,因为在我看到它之前,我什至认为这是一次让别人为你做作业的钓鱼探险。
当你创建一个新的POSIX线程时,你告诉pthread_create()
函数新线程应该运行哪个函数。当线程 return 从该函数退出,或使用 pthread_exit()
退出时,它停止执行。通常,另一个线程 通过 pthread_join()
获取 它,它也可以从线程函数中提供 return 值(指针)。
线程函数接受一个空指针(void *
)作为参数,return是一个。两者都不需要是很常见的。如果要传递整数值,可以使用 (void *)(intptr_t)value
或 (void *)(uintptr_t)value
将有符号或无符号的 value
转换为指针;和 (intptr_t)ptr
或 (uintptr_t)ptr
将指针 ptr
转换为有符号或无符号整数类型。这些类型在 POSIX.
中的 <stdint.h>
中指定
您还可以提供一组属性作为 C 库的规范,说明创建的线程应具有何种属性。这些属性在创建过程中不会被消耗,您可以使用同一组属性来创建任意数量的线程。普通变量可以保存属性;您不需要为其动态分配内存。对线程属性最常见的做法是将堆栈大小设置为合理的值,因为默认堆栈大小往往很大(大约 8 兆字节),这通常是进程尝试创建线程时遇到的第一个限制大量线程。
因此,要创建三个线程,每个线程都有一个特定的 调度程序策略 ,您可以:
Initialize a set of pthread attributes,
using pthread_attr_init()
Set the scheduler policy attribute,
using pthread_attr_setschedpolicy()
Create the thread using pthread_create(),
but do remember to check for errors
Set the scheduler policy attribute,
using pthread_attr_setschedpolicy()
Create the thread using pthread_create(),
but do remember to check for errors
Set the scheduler policy attribute,
using pthread_attr_setschedpolicy()
Create the thread using pthread_create(),
but do remember to check for errors
Discard the thread attributes,
using pthread_attr_destroy()
The main/initial thread is free to twiddle
its proverbial thumbs here.
Wait for each of the three threads to exit
using three calls to pthread_join()
Done.
您可能还希望在设置调度程序策略属性(以及是否设置调度程序优先级)时检查错误,因为并非所有策略和优先级都允许非特权用户使用。我希望你会有很多
retcode = ... pthreads-related function call ...;
if (retcode) {
fprintf(stderr, "pthread_suchandsuch() failed: %s.\n", strerror(retcode));
exit(EXIT_FAILURE);
}
在你的代码中。它可能看起来很冗长,但是当您寻找某事不起作用的原因时,它会非常有用。 (另外,使用 gcc -Wall -O2 -pthread sources.. -o binary
来编译你的程序。)
我强烈建议您将浏览器 window 打开 Linux man pages online,这是 Linux 和 [=51= 的核心手册页的最新且维护良好的来源]y 系统。
这里是 C 语言的新用户。我一直在学习创建基本的 C 程序,我的教授给了我们第一个大任务,我绝望地迷路了。
对于其中的一部分,我应该创建三个不同的线程,分别使用 SCHED_OTHER
、SCHED_RR
和 SCHED_FIFO
作为它们各自的调度优先级。
这 3 个线程中的每一个都将等待 "main" 线程发出它们应该启动 运行 a "task".
此任务是一个具有非生成和非阻塞指令的函数调用,需要 5 到 10 秒才能完成。
基本上,有两个问题:
How to create a thread in C with a defined priority(
SCHED_OTHER
,SCHED_RR
, orSCHED_FIFO
) (我如何使用pthread_create()
?)我将如何着手编写这些任务? (伪代码就可以)
How to I create a thread in C with a defined priority(SCHED_OTHER, SCHED_RR, or SCHED_FIFO) (how do I use pthread_create()?) (pseudocode is fine)
最后一点,"pseudocode is fine"是重要的一点。你应该考虑把它移到问题的前面,因为在我看到它之前,我什至认为这是一次让别人为你做作业的钓鱼探险。
当你创建一个新的POSIX线程时,你告诉pthread_create()
函数新线程应该运行哪个函数。当线程 return 从该函数退出,或使用 pthread_exit()
退出时,它停止执行。通常,另一个线程 通过 pthread_join()
获取 它,它也可以从线程函数中提供 return 值(指针)。
线程函数接受一个空指针(void *
)作为参数,return是一个。两者都不需要是很常见的。如果要传递整数值,可以使用 (void *)(intptr_t)value
或 (void *)(uintptr_t)value
将有符号或无符号的 value
转换为指针;和 (intptr_t)ptr
或 (uintptr_t)ptr
将指针 ptr
转换为有符号或无符号整数类型。这些类型在 POSIX.
<stdint.h>
中指定
您还可以提供一组属性作为 C 库的规范,说明创建的线程应具有何种属性。这些属性在创建过程中不会被消耗,您可以使用同一组属性来创建任意数量的线程。普通变量可以保存属性;您不需要为其动态分配内存。对线程属性最常见的做法是将堆栈大小设置为合理的值,因为默认堆栈大小往往很大(大约 8 兆字节),这通常是进程尝试创建线程时遇到的第一个限制大量线程。
因此,要创建三个线程,每个线程都有一个特定的 调度程序策略 ,您可以:
Initialize a set of pthread attributes,
using pthread_attr_init()
Set the scheduler policy attribute,
using pthread_attr_setschedpolicy()
Create the thread using pthread_create(),
but do remember to check for errors
Set the scheduler policy attribute,
using pthread_attr_setschedpolicy()
Create the thread using pthread_create(),
but do remember to check for errors
Set the scheduler policy attribute,
using pthread_attr_setschedpolicy()
Create the thread using pthread_create(),
but do remember to check for errors
Discard the thread attributes,
using pthread_attr_destroy()
The main/initial thread is free to twiddle
its proverbial thumbs here.
Wait for each of the three threads to exit
using three calls to pthread_join()
Done.
您可能还希望在设置调度程序策略属性(以及是否设置调度程序优先级)时检查错误,因为并非所有策略和优先级都允许非特权用户使用。我希望你会有很多
retcode = ... pthreads-related function call ...;
if (retcode) {
fprintf(stderr, "pthread_suchandsuch() failed: %s.\n", strerror(retcode));
exit(EXIT_FAILURE);
}
在你的代码中。它可能看起来很冗长,但是当您寻找某事不起作用的原因时,它会非常有用。 (另外,使用 gcc -Wall -O2 -pthread sources.. -o binary
来编译你的程序。)
我强烈建议您将浏览器 window 打开 Linux man pages online,这是 Linux 和 [=51= 的核心手册页的最新且维护良好的来源]y 系统。