【发布时间】:2017-04-23 12:46:03
【问题描述】:
这似乎是一个非常简单的问题,但我真的不知道这里出了什么问题。我编写了一个日志记录函数和一个主函数,它启动一个重复调用该函数的线程。 open 调用成功,并且文件描述符在调用 write 之前没有损坏。仍然没有写入任何字节,并且 errno 设置为 BAD FILE DESCRIPTOR。 (我省略了大多数可读性错误检查)
日志功能:
static pthread_once_t once = PTHREAD_ONCE_INIT;
static int logfd;
static void _log_init(void)
{
int flags = O_CREAT | O_TRUNC | O_APPEND;
int perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
logfd = open("log.txt", flags, perms);
}
void log_data(const char *fmt, ...)
{
int bufsize;
char *buf;
pthread_once(&once, _log_init);
va_list va;
va_start(va, fmt);
bufsize = vsnprintf(NULL, 0, fmt, va) + 1;
va_end(va);
va_start(va, fmt);
vsnprintf(buf, bufsize, fmt, va);
va_end(va);
if (write(logfd, buf, bufsize) == -1)
perror("write");
free(buf);
}
主要功能:
static void *thread_log(void *arg)
{
int thread = *((int *)arg);
free(arg);
for (int i = i; i < 10; ++i)
log_data("thread %d\n", thread);
return (void *) NULL;
}
int main()
{
pthread_t t;
int *arg;
arg = malloc(sizeof(int));
*arg = 1;
pthread_create(&t, NULL, thread_log, (void *) arg);
pthread_join(t, NULL);
return 0;
}
【问题讨论】:
-
作为记录,您使用
stdarg.h作为您的参数列表。文件描述符是使用来自fcntl.h的open函数创建的。然后write来自unistd.h。打开后你的文件描述符是什么值?通话之间会发生变化吗?当您说它“损坏”时,您的意思是它变得无效还是实际上改变了价值?