gpt4 book ai didi

c - pthread_attr_setschedparam() 返回非零但 errno = 成功 ...(Linux、C、Ubuntu 18.04.4)

转载 作者:行者123 更新时间:2023-12-04 19:07:15 25 4
gpt4 key购买 nike

美好的一天所有堆栈溢出窥视!
我的程序中有此代码会因错误退出......但成功了吗?
不知道为什么?
输出:

  • 日期时间 - RLIMIT_RTTIME:软=-1,硬=-1
  • 日期时间 - RLIMIT_RTPRIO:软=-1,硬=-1
  • 日期时间 - RLIMIT_CPU:软=-1,硬=-1
  • dateTime - main() - pthread_attr_setschedParam()
  • 成功
  • #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <stdbool.h>
    #include <string.h>
    #include <unistd.h>
    #include <errno.h>
    #include <pthread.h>
    #include <signal.h>
    #include <sys/time.h>
    #include <sys/resource.h>



    char strNowTime[26];



    //-----------------------------------
    // signal Handler stuff.
    //-----------------------------------
    static
    struct sigaction mySigActTerm;

    volatile
    int myTerminate = 0;

    void terminateHandler(int signum, siginfo_t *info, void *ptr)
    {
    // set a flag here and get out.
    myTerminate = 1;
    }



    void getNowTime(char* str)
    {
    time_t rawtime;
    time(&rawtime);
    ctime_r(&rawtime, str);

    // clobber the unwanted newline.
    str[24] = '\0';
    }



    void myResLimit()
    {
    struct
    rlimit procLimit;

    getrlimit(RLIMIT_RTTIME, &procLimit);
    getNowTime(strNowTime);
    fprintf(stderr, "%s - RLIMIT_RTTIME: soft=%lld, hard=%lld\n", strNowTime, (long long) procLimit.rlim_cur, (long long)procLimit.rlim_max);

    getrlimit(RLIMIT_RTPRIO, &procLimit);
    getNowTime(strNowTime);
    fprintf(stderr, "%s - RLIMIT_RTPRIO: soft=%lld, hard=%lld\n", strNowTime, (long long) procLimit.rlim_cur, (long long) procLimit.rlim_max);

    getrlimit(RLIMIT_CPU, &procLimit);
    getNowTime(strNowTime);
    fprintf(stderr, "%s - RLIMIT_CPU: soft=%lld, hard=%lld\n", strNowTime, (long long) procLimit.rlim_cur, (long long) procLimit.rlim_max);
    }



    void* serialThread(void* arg)
    {
    while (1) {

    }
    }



    //-----------------------------------
    // the one and only MAIN.
    //-----------------------------------
    int main()
    {
    //-----------------------------------------------
    // locals.
    int rtn;
    int myErr;

    pthread_t serialThdID;

    pthread_attr_t* serialAttr;

    struct
    sched_param serialParam;


    //-----------------------------------------------
    // Log OS resource limits.
    myResLimit();

    //-----------------------------------------------
    // initialize the signals struct.
    // ... and setup signals.
    memset(&mySigActTerm, 0, sizeof(mySigActTerm));
    mySigActTerm.sa_sigaction = terminateHandler;
    mySigActTerm.sa_flags = SA_SIGINFO;

    sigaction(SIGTERM, &mySigActTerm, NULL);


    //-----------------------------------------------
    // setup the pthread attributes struct.
    if ((serialAttr = malloc(sizeof(pthread_attr_t))) == NULL) {
    myErr = errno;
    getNowTime(strNowTime);
    fprintf(stderr, "%s - main() - pthread_attr_t malloc()\n%s\n", strNowTime, strerror(myErr));
    exit(EXIT_FAILURE);
    }
    memset(serialAttr, 0, sizeof(pthread_attr_t));

    //-----------------------------------------------
    // set initial default pthread attr values.
    if (pthread_attr_init(serialAttr) != 0) {
    myErr = errno;
    getNowTime(strNowTime);
    fprintf(stderr, "%s - main() - pthread_attr_init()\n%s\n", strNowTime, strerror(myErr));
    exit(EXIT_FAILURE);
    }

    //-----------------------------------------------
    // set for best near real time policy.
    if (pthread_attr_setschedpolicy(serialAttr, SCHED_FIFO) !=0) {
    myErr = errno;
    getNowTime(strNowTime);
    fprintf(stderr, "%s - main() - pthread_attr_setschedpolicy()\n%s\n", strNowTime, strerror(myErr));
    exit(EXIT_FAILURE);
    }

    //-----------------------------------------------
    // set to explicit inherit or attr obj will be ignored.
    if (pthread_attr_setinheritsched(serialAttr, PTHREAD_EXPLICIT_SCHED) !=0) {
    myErr = errno;
    getNowTime(strNowTime);
    fprintf(stderr, "%s - main() - pthread_attr_setinheritsched()\n%s\n", strNowTime, strerror(myErr));
    exit(EXIT_FAILURE);
    }

    //-----------------------------------------------
    // set to un-limited thread priority.
    serialParam.sched_priority = 0;
    if (pthread_attr_setschedparam(serialAttr, &serialParam) !=0) {
    myErr = errno;
    getNowTime(strNowTime);
    fprintf(stderr, "%s - main() - pthread_attr_setschedparam()\n%s\n", strNowTime, strerror(myErr));
    exit(EXIT_FAILURE);
    }

    //-----------------------------------------------
    // start the new thread.
    rtn = pthread_create(&serialThdID, serialAttr, serialThread, NULL);
    myErr = errno;
    if(rtn == 0) {
    getNowTime(strNowTime);
    fprintf(stderr, "%s - starting serial thread.\n", strNowTime);
    }
    else {
    getNowTime(strNowTime);
    fprintf(stderr, "%s - main() - pthread_create() returned %d\n%s\n", strNowTime, rtn, strerror(myErr));
    exit(EXIT_FAILURE);
    }

    //-----------------------------------------------
    // no need to keep this junk if pthread_create() succeeded.
    if (pthread_attr_destroy(serialAttr) != 0) {
    myErr = errno;
    getNowTime(strNowTime);
    fprintf(stderr, "%s - main() - pthread_attr_destroy()\n%s\n", strNowTime, strerror(myErr));
    }
    // research proves this is needed if we malloc'ed!
    free(serialAttr);



    while (myTerminate == 0) {

    }
    }

    最佳答案

    pthread_attr_setschedpolicy未设置 errno .来自 manual :

    RETURN VALUE

    On success, these functions return 0; on error, they return a nonzero error number.


    也就是说,错误号在返回值本身而不是在 errno中。 .
    事实上,没有一个 pthread 函数设置 errno .见 pthread_create手册中有关如何调用 perror 的示例当 pthread 函数失败时。它实际上是手动设置 errno第一的:
    #define handle_error_en(en, msg) \
    do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

    关于c - pthread_attr_setschedparam() 返回非零但 errno = 成功 ...(Linux、C、Ubuntu 18.04.4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65999944/

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