You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

pthread_attr_setinheritsched函数的具体作用详解——基于POSIX线程优先级调度的调试疑问

Understanding pthread_attr_setinheritsched and Your Thread Scheduling Issues

Let's break down exactly what that line does, why you're seeing those confusing behaviors, and how to fix them.

What does pthread_attr_setinheritsched(&tattr1, PTHREAD_EXPLICIT_SCHED); do?

This function controls how a new thread inherits scheduling attributes from its parent (the thread that creates it):

  • PTHREAD_EXPLICIT_SCHED: Tells the pthread library to use the explicit scheduling settings you've configured in the attribute object (in your case, SCHED_RR policy and the priority you set with param1.sched_priority). It ignores the parent thread's scheduling properties entirely.
  • The default behavior (if you don't call this function) is PTHREAD_INHERIT_SCHED, where the new thread copies all scheduling attributes from the creating thread (your main thread, in this case).

Why your two test cases behave the way they do

1. Deleting the line: Threads run but ignore priorities

When you omit this line, the default PTHREAD_INHERIT_SCHED takes effect. Your main thread is using the default scheduling policy: SCHED_OTHER (the standard time-sharing policy for non-real-time processes).

SCHED_OTHER doesn't support explicit priority values—any priority settings you tried to configure in tattr1/tattr2 get ignored. All threads inherit the main thread's SCHED_OTHER policy, so the OS schedules them in a round-robin fashion without respecting your priority rules.

2. Keeping the line: Threads fail to run entirely

This is almost certainly a permission issue, combined with a possible invalid priority value:

  • Real-time scheduling policies like SCHED_RR require elevated privileges. On Linux, your process needs either:
    • Root access (run with sudo), or
    • The CAP_SYS_NICE capability assigned to the executable (via sudo setcap cap_sys_nice+ep ./your_program).
  • If you don't have these permissions, pthread_create will silently fail (return an error code like EPERM), but your code doesn't check the return value of pthread_create—so it looks like the threads never start.
  • Additionally, your priority value for tattr2 is 0, which is below the minimum valid priority for SCHED_RR (on Linux, the range is 1 to 99). Setting an invalid priority will also cause pthread_create to fail.

Fixes to get your priority scheduling working

  1. Check for pthread_create errors (always do this!):

    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    // ...
    
    int ret;
    ret = pthread_create(&tid_1, &tattr1, taskOne, NULL);
    if (ret != 0) {
        fprintf(stderr, "Failed to create taskOne: %s\n", strerror(ret));
        exit(EXIT_FAILURE);
    }
    // Repeat this check for all pthread_create calls
    

    This will tell you exactly why threads aren't starting (permission denied, invalid priority, etc.).

  2. Fix priority values to be within the valid range:
    Use these calls to get the valid priority bounds for SCHED_RR on your system:

    int min_prio = sched_get_priority_min(SCHED_RR);
    int max_prio = sched_get_priority_max(SCHED_RR);
    printf("SCHED_RR priority range: %d to %d\n", min_prio, max_prio);
    

    Adjust your priorities to fit this range (e.g., set tattr1 to min_prio and tattr2 to max_prio if you want task3/task4 to run first).

  3. Run with sufficient privileges:
    Either execute your program with sudo, or assign the CAP_SYS_NICE capability to avoid running as root every time.

  4. Double-check your priority assignment:
    Wait a second—you said you want task3 and task4 to run before task1 and task2, but your code assigns task1/task2 to tattr1 (priority 10) and task3/task4 to tattr2 (priority 0). That's backwards! You need to set tattr2 to a higher priority than tattr1 to get the desired behavior.

内容的提问来源于stack exchange,提问作者Typhaon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 18:02:43