面试题答案
一键面试在Linux C语言多线程编程中,可以通过使用pthread_cleanup_push
和pthread_cleanup_pop
函数来确保文件资源在出现错误时能被正确关闭。以下是具体代码示例:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
// 线程函数声明
void* thread_function(void* arg);
// 清理函数,用于关闭文件
void cleanup_handler(void* arg) {
int fd = *((int*)arg);
close(fd);
printf("File closed in cleanup handler.\n");
}
int main() {
pthread_t tid;
int ret;
// 创建线程
ret = pthread_create(&tid, NULL, thread_function, NULL);
if (ret != 0) {
fprintf(stderr, "Error creating thread: %d\n", ret);
return 1;
}
// 等待线程结束
ret = pthread_join(tid, NULL);
if (ret != 0) {
fprintf(stderr, "Error joining thread: %d\n", ret);
return 1;
}
return 0;
}
void* thread_function(void* arg) {
int fd;
// 打开文件
fd = open("test.txt", O_RDWR | O_CREAT, 0666);
if (fd == -1) {
perror("open");
return NULL;
}
// 注册清理函数
pthread_cleanup_push(cleanup_handler, &fd);
// 模拟一些可能出错的操作
if (1) { // 这里简单假设出错情况
printf("Simulated error occurred.\n");
// 出错时,清理函数会被调用关闭文件
pthread_exit(NULL);
}
// 取消注册清理函数(如果没有出错)
pthread_cleanup_pop(0);
// 正常的文件读写操作
// 这里省略具体的读写代码
close(fd);
printf("File closed normally.\n");
pthread_exit(NULL);
}
在上述代码中:
cleanup_handler
函数用于关闭文件描述符。pthread_cleanup_push
用于注册清理函数,当线程通过pthread_exit
、响应取消请求或调用pthread_cleanup_pop(1)
时,注册的清理函数会被调用。pthread_cleanup_pop(0)
用于取消注册清理函数,如果传递参数为1
,则会调用清理函数并取消注册。在正常情况下,文件会在pthread_cleanup_pop(0)
之后被正常关闭;如果在pthread_cleanup_push
之后出现错误,清理函数会被调用以关闭文件,避免资源泄漏。