- unstable 4.31.0-1
| pthread_setschedparam(3) | Library Functions Manual | pthread_setschedparam(3) |
الاسم¶
pthread_setschedparam, pthread_getschedparam - ضبط/الحصول على سياسة الجدولة ومعاملات خيط
المكتبة¶
مكتبة مسالك POSIX (libpthread، -lpthread)
موجز¶
#include <pthread.h>
int pthread_setschedparam(pthread_t thread, int policy,
const struct sched_param *param);
int pthread_getschedparam(pthread_t thread, int *restrict policy,
struct sched_param *restrict param);
الوصف¶
تضبط الدالة pthread_setschedparam() سياسة الجدولة ومعاملات الخيط thread.
يحدد policy سياسة الجدولة الجديدة لـ thread. القيم المدعومة لـ policy ودلالاتها موصوفة في sched(7).
تحدد البنية المشار إليها بـ param معاملات الجدولة الجديدة لـ thread. تُحتفظ معاملات الجدولة في البنية التالية:
struct sched_param {
int sched_priority; /* أولوية الجدولة */
};
كما هو موضح، يُدعم معامل جدولة واحد فقط. لتفاصيل النطاقات المسموح بها لأولويات الجدولة في كل سياسة جدولة، انظر sched(7).
تُرجع الدالة pthread_getschedparam() سياسة الجدولة ومعاملات الخيط thread، في المخازن المؤقتة المشار إليها بـ policy و param، على التوالي. قيمة الأولوية المُرجعة هي تلك التي ضُبطت بواسطة أحدث استدعاء لـ pthread_setschedparam() أو pthread_setschedprio(3) أو pthread_create(3) الذي أثر على thread. لا تعكس الأولوية المُرجعة أي تعديلات مؤقتة في الأولوية نتيجة استدعاءات لدوال وراثة الأولوية أو سقف الأولوية (انظر، على سبيل المثال، pthread_mutexattr_setprioceiling(3) و pthread_mutexattr_setprotocol(3)).
قيمة الإرجاع¶
عند النجاح، تُرجع هاتان الدالتان 0؛ عند الخطأ، تُرجعان رقم خطأ غير صفري. إذا فشلت pthread_setschedparam()، لا تُغير سياسة الجدولة ومعاملات thread.
الأخطاء¶
يمكن أن تفشل كلتا الدالتين مع الخطأ التالي:
- ESRCH
- تعذر العثور على أي خيط بالمعرف thread.
قد تفشل pthread_setschedparam() بالإضافة إلى ذلك مع الأخطاء التالية:
- EINVAL
- policy ليست سياسة معروفة، أو param غير منطقي بالنسبة لـ policy.
- EPERM
- لا يمتلك المستدعي الصلاحيات المناسبة لضبط سياسة الجدولة والمعاملات المحددة.
يُوثق POSIX.1 أيضًا خطأ ENOTSUP ("جرت محاولة لضبط السياسة أو معاملات الجدولة إلى قيمة غير مدعومة") لـ pthread_setschedparam().
السمات¶
للاطلاع على شرح للمصطلحات المستخدمة في هذا القسم، انظر attributes(7).
| الواجهة | السمة | القيمة |
| pthread_setschedparam(), pthread_getschedparam() | سلامة الخيوط | MT-Safe |
المعايير¶
POSIX.1-2008.
التاريخ¶
glibc 2.0 POSIX.1-2001.
ملاحظات¶
للحصول على وصف للصلاحيات المطلوبة لتغيير سياسة جدولة خيط وأولويته، وتأثير ذلك، وتفاصيل النطاقات المسموح بها للأولويات في كل سياسة جدولة، انظر sched(7).
أمثلة¶
يوضح البرنامج أدناه استخدام pthread_setschedparam() و pthread_getschedparam()، بالإضافة إلى استخدام عدد من دوال pthreads الأخرى المتعلقة بالجدولة.
في التشغيل التالي، يضبط الخيط الرئيسي سياسة الجدولة الخاصة به إلى SCHED_FIFO بأولوية 10، ويُهيئ كائن سمات خيط بسمة سياسة جدولة SCHED_RR وسمة أولوية جدولة 20. ثم يضبط البرنامج (باستخدام pthread_attr_setinheritsched(3)) سمة وراثة الجدولة لكائن سمات الخيط إلى PTHREAD_EXPLICIT_SCHED، مما يعني أن الخيوط المُنشأة باستخدام كائن السمات هذا يجب أن تأخذ سمات الجدولة الخاصة بها من كائن سمات الخيط. ثم ينشئ البرنامج خيطًا باستخدام كائن سمات الخيط، ويعرض ذلك الخيط سياسة الجدولة والأولوية الخاصة به.
$ su; # Need privilege to set real-time scheduling policies Password: # ./a.out -mf10 -ar20 -i e; Scheduler settings of main thread
policy=SCHED_FIFO, priority=10 Scheduler settings in 'attr'
policy=SCHED_RR, priority=20
inheritsched is EXPLICIT Scheduler attributes of new thread
policy=SCHED_RR, priority=20
في المخرجات أعلاه، يمكن رؤية أن سياسة الجدولة والأولوية أُخذت من القيم المحددة في كائن سمات الخيط.
التشغيل التالي هو نفسه السابق، باستثناء أن سمة وراثة الجدولة ضُبطت إلى PTHREAD_INHERIT_SCHED، مما يعني أن الخيوط المُنشأة باستخدام كائن سمات الخيط يجب أن تتجاهل سمات الجدولة المحددة في كائن السمات وتأخذ بدلاً من ذلك سمات الجدولة الخاصة بها من الخيط المُنشئ.
# ./a.out -mf10 -ar20 -i i; Scheduler settings of main thread
policy=SCHED_FIFO, priority=10 Scheduler settings in 'attr'
policy=SCHED_RR, priority=20
inheritsched is INHERIT Scheduler attributes of new thread
policy=SCHED_FIFO, priority=10
في المخرجات أعلاه، يمكن رؤية أن سياسة الجدولة والأولوية أُخذت من الخيط المنشئ، وليس من كائن سمات الخيط.
لاحظ أنه لو حذفنا الخيار -i i، لكان المخرج نفسه، لأن PTHREAD_INHERIT_SCHED هو المبدئي لسمة الجدولة الموروثة.
مصدر البرنامج¶
/* pthreads_sched_test.c */
#include <err.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
[[noreturn]]
static void
usage(char *prog_name, char *msg)
{
if (msg != NULL)
fputs(msg, stderr);
fprintf(stderr, "Usage: %s [options]\n", prog_name);
fprintf(stderr, "Options are:\n");
#define fpe(msg) fprintf(stderr, "\t%s", msg) /* Shorter */
fpe("-a<policy><prio> Set scheduling policy and priority in\n");
fpe(" thread attributes object\n");
fpe(" <policy> can be\n");
fpe(" f SCHED_FIFO\n");
fpe(" r SCHED_RR\n");
fpe(" o SCHED_OTHER\n");
fpe("-A Use default thread attributes object\n");
fpe("-i {e|i} Set inherit scheduler attribute to\n");
fpe(" 'explicit' or 'inherit'\n");
fpe("-m<policy><prio> Set scheduling policy and priority on\n");
fpe(" main thread before pthread_create() call\n");
exit(EXIT_FAILURE);
}
static int
get_policy(char p, int *policy)
{
switch (p) {
case 'f': *policy = SCHED_FIFO; return 1;
case 'r': *policy = SCHED_RR; return 1;
case 'o': *policy = SCHED_OTHER; return 1;
default: return 0;
}
}
static void
display_sched_attr(int policy, const struct sched_param *param)
{
printf(" policy=%s, priority=%d\n",
(policy == SCHED_FIFO) ? "SCHED_FIFO" :
(policy == SCHED_RR) ? "SCHED_RR" :
(policy == SCHED_OTHER) ? "SCHED_OTHER" :
"???",
param->sched_priority);
}
static void
display_thread_sched_attr(char *msg)
{
int policy, s;
struct sched_param param;
s = pthread_getschedparam(pthread_self(), &policy, ¶m);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_getschedparam");
printf("%s\n", msg);
display_sched_attr(policy, ¶m);
}
static void *
thread_start(void *arg)
{
display_thread_sched_attr("Scheduler attributes of new thread");
return NULL;
}
int
main(int argc, char *argv[])
{
int s, opt, inheritsched, use_null_attrib, policy;
pthread_t thread;
pthread_attr_t attr;
pthread_attr_t *attrp;
char *attr_sched_str, *main_sched_str, *inheritsched_str;
struct sched_param param;
/* Process command-line options. */
use_null_attrib = 0;
attr_sched_str = NULL;
main_sched_str = NULL;
inheritsched_str = NULL;
while ((opt = getopt(argc, argv, "a:Ai:m:")) != -1) {
switch (opt) {
case 'a': attr_sched_str = optarg; break;
case 'A': use_null_attrib = 1; break;
case 'i': inheritsched_str = optarg; break;
case 'm': main_sched_str = optarg; break;
default: usage(argv[0], "Unrecognized option\n");
}
}
if (use_null_attrib
&& (inheritsched_str != NULL || attr_sched_str != NULL))
{
usage(argv[0], "Can't specify -A with -i or -a\n");
}
/* Optionally set scheduling attributes of main thread,
and display the attributes. */
if (main_sched_str != NULL) {
if (!get_policy(main_sched_str[0], &policy))
usage(argv[0], "Bad policy for main thread (-m)\n");
param.sched_priority = strtol(&main_sched_str[1], NULL, 0);
s = pthread_setschedparam(pthread_self(), policy, ¶m);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_setschedparam");
}
display_thread_sched_attr("Scheduler settings of main thread");
printf("\n");
/* Initialize thread attributes object according to options. */
attrp = NULL;
if (!use_null_attrib) {
s = pthread_attr_init(&attr);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_attr_init");
attrp = &attr;
}
if (inheritsched_str != NULL) {
if (inheritsched_str[0] == 'e')
inheritsched = PTHREAD_EXPLICIT_SCHED;
else if (inheritsched_str[0] == 'i')
inheritsched = PTHREAD_INHERIT_SCHED;
else
usage(argv[0], "Value for -i must be 'e' or 'i'\n");
s = pthread_attr_setinheritsched(&attr, inheritsched);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_attr_setinheritsched");
}
if (attr_sched_str != NULL) {
if (!get_policy(attr_sched_str[0], &policy))
usage(argv[0], "Bad policy for 'attr' (-a)\n");
param.sched_priority = strtol(&attr_sched_str[1], NULL, 0);
s = pthread_attr_setschedpolicy(&attr, policy);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_attr_setschedpolicy");
s = pthread_attr_setschedparam(&attr, ¶m);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_attr_setschedparam");
}
/* If we initialized a thread attributes object, display
the scheduling attributes that were set in the object. */
if (attrp != NULL) {
s = pthread_attr_getschedparam(&attr, ¶m);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_attr_getschedparam");
s = pthread_attr_getschedpolicy(&attr, &policy);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_attr_getschedpolicy");
printf("Scheduler settings in 'attr'\n");
display_sched_attr(policy, ¶m);
pthread_attr_getinheritsched(&attr, &inheritsched);
printf(" inheritsched is %s\n",
(inheritsched == PTHREAD_INHERIT_SCHED) ? "INHERIT" :
(inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" :
"???");
printf("\n");
}
/* Create a thread that will display its scheduling attributes. */
s = pthread_create(&thread, attrp, &thread_start, NULL);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_create");
/* Destroy unneeded thread attributes object. */
if (!use_null_attrib) {
s = pthread_attr_destroy(&attr);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_attr_destroy");
}
s = pthread_join(thread, NULL);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_join");
exit(EXIT_SUCCESS);
}
انظر أيضًا¶
getrlimit(2), sched_get_priority_min(2), pthread_attr_init(3), pthread_attr_setinheritsched(3), pthread_attr_setschedparam(3), pthread_attr_setschedpolicy(3), pthread_create(3), pthread_self(3), pthread_setschedprio(3), pthreads(7), sched(7)
ترجمة¶
تُرجمت هذه الصفحة من الدليل بواسطة زايد السعيدي <zayed.alsaidi@gmail.com>
هذه الترجمة هي وثيقة مجانية؛ راجع رخصة جنو العامة الإصدار 3 أو ما بعده للاطلاع على شروط حقوق النشر. لا توجد أي ضمانات.
إذا وجدت أي أخطاء في ترجمة صفحة الدليل هذه، يرجى إرسال بريد إلكتروني إلى قائمة بريد المترجمين: kde-l10n-ar@kde.org.
| 21 سبتمبر 2025 | صفحات دليل لينكس 6.18 |