【问题标题】:Example of strerror_rstrerror_r 示例
【发布时间】:2020-07-26 14:45:40
【问题描述】:

我是错误处理的新手;在我的代码中,我需要测试函数的返回值,并在发生错误时打印错误描述。

为了保持代码线程安全,我必须使用 strerror_r,但我有一些难以使用的地方。在以下代码中,发生了错误编号 22(ret_setschedparam 为 22)。如何使用strerror_r 打印错误号 22 的描述,即“无效参数”?

我认为这个原型应该是我需要的正确的strerror_r:

char *strerror_r(int errnum, char *buf, size_t buflen);

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <sched.h>
#include <errno.h>
#include <string.h>

void *task();

int main()
{
pthread_attr_t attr;
struct sched_param prio;
pthread_t tid;
int ret_create;
int ret_setschedparam;
int ret_getschedparam;
int ret_join;
char *buf_setschedparam;
size_t size_setschedparam = 1024;



 pthread_attr_init(&attr);

 prio.sched_priority = 12;
 ret_setschedparam = pthread_attr_setschedparam(&attr, &prio);
 if (ret_setschedparam != 0) {
  printf("Errore numero (pthread_attr_setschedparam): %s\n", strerror_r(errno, buf_setschedparam, size_setschedparam));
  exit(EXIT_FAILURE);
  }
    
 ret_create = pthread_create(&tid, &attr, task, NULL);
 printf("%d %d\n", ret_create, EPERM);
 if (ret_create != 0) {
  printf("Errore numero (pthread_create): %d\n", ret_create);

  exit(EXIT_FAILURE);
  }

 ret_getschedparam = pthread_attr_getschedparam(&attr, &prio);
 if (ret_getschedparam != 0) {
  printf("Errore numero (pthread_attr_getschedparam): %d\n", ret_getschedparam);
  exit(EXIT_FAILURE);
  }

 printf("Livello di priorità del thread: %d\n", prio.sched_priority);

 ret_join = pthread_join(tid, NULL);
 if (ret_join != 0) {
  printf("Errore numero (pthread_join): %d\n", ret_join);
  exit(EXIT_FAILURE);
  }

 return(0);
}


void *task()
{
 printf("I am a simple thread.\n");
 pthread_exit(NULL);
}

编译器给了我一个错误:它说 strerror_r 的输出是一个 int,而不是一个 char。

【问题讨论】:

    标签: c error-handling pthreads strerror


    【解决方案1】:

    我认为这个原型应该是我需要的正确strerro_r:

    请注意,这不是标准的 strerror_r 接口,而是 GNU 扩展。

    您可能希望使用-D_GNU_SOURCE 构建您的程序,或将#define _GNU_SOURCE 1 添加到文件顶部以获得此原型而不是标准原型。

    您也没有正确调用strerror_r。这个电话:

    char *buf_setschedparam;
    size_t size_setschedparam = 1024;
    
    ... strerror_r(errno, buf_setschedparam, size_setschedparam)
    

    strerror_r 承诺buf_setscheparam 指向大小为1024 的缓冲区。事实上,该指针未初始化,因此一旦您构建程序,它就会立即崩溃。

    另外,pthread_*函数没有设置errno,直接返回错误码。

    你想要:

    const size_t size_setschedparam = 1024;
    char buf_setschedparam[size_setschedparam];
    
    ... sterror_r(ret_setschedparam, buf_setschedparam, size_setschedparam);
    

    甚至更好:

    char buf[1024];
    
       ... sterror_r(ret_setschedparam, buf, sizeof(buf));
    

    【讨论】:

    • Hello @EmployedRussian 我修改了代码,但 printf 打印“Errore numero (pthread_attr_setschedparam): Success”而不是“Errore numero (pthread_attr_setschedparam): Invalid argument”。为什么?
    • Why? 因为您将0 作为第一个参数传递给strerror_r。改为传递无效参数的错误号。我猜你想从pthread_attr_setschedparam 函数传递返回值。
    • @KamilCuk 我错过了第二个错误(一行可以有多少错误?)。已更正。
    猜你喜欢
    • 2017-05-14
    • 1970-01-01
    • 2015-02-27
    • 2010-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 2017-01-03
    相关资源
    最近更新 更多