面试题答案
一键面试性能优化方面
- 数据结构设计:
- 尽量简化数据结构,减少不必要的嵌套和指针层级。例如,如果有多层嵌套的结构体,可以考虑扁平化处理。
- 使用高效的数据结构,如哈希表在查找场景下效率更高。
// 简单的哈希表示例 #define TABLE_SIZE 1024 typedef struct { int key; int value; } HashEntry; HashEntry hashTable[TABLE_SIZE]; int hashFunction(int key) { return key % TABLE_SIZE; } void insert(int key, int value) { int index = hashFunction(key); hashTable[index].key = key; hashTable[index].value = value; } int lookup(int key) { int index = hashFunction(key); if (hashTable[index].key == key) { return hashTable[index].value; } return -1; // 未找到 }
- 同步机制:
- 对于读多写少的场景,使用读写锁(
pthread_rwlock
)代替互斥锁(pthread_mutex
)。
#include <pthread.h> #include <stdio.h> pthread_rwlock_t rwlock; int sharedData = 0; void *reader(void *arg) { pthread_rwlock_rdlock(&rwlock); printf("Reader read: %d\n", sharedData); pthread_rwlock_unlock(&rwlock); return NULL; } void *writer(void *arg) { pthread_rwlock_wrlock(&rwlock); sharedData++; printf("Writer wrote: %d\n", sharedData); pthread_rwlock_unlock(&rwlock); return NULL; } int main() { pthread_rwlock_init(&rwlock, NULL); pthread_t r1, w1; pthread_create(&r1, NULL, reader, NULL); pthread_create(&w1, NULL, writer, NULL); pthread_join(r1, NULL); pthread_join(w1, NULL); pthread_rwlock_destroy(&rwlock); return 0; }
- 考虑使用无锁数据结构,如无锁队列(
liblfds
库),在某些场景下可避免锁争用。
- 对于读多写少的场景,使用读写锁(
- 内存管理:
- 减少共享内存的碎片化,合理分配内存块大小。例如,使用内存池技术,预先分配一大块共享内存,然后按固定大小的块进行分配和回收。
- 及时释放不再使用的共享内存。
- 系统调用优化:
- 批量操作共享内存,减少系统调用次数。例如,使用
mmap
映射一大块共享内存,而不是频繁调用shmat
和shmdt
。
- 批量操作共享内存,减少系统调用次数。例如,使用
异常排查与解决
- 内存段错误:
- 使用
gdb
调试,在程序崩溃处设置断点,查看变量值和堆栈信息。
gdb your_program (gdb) run (gdb) bt # 查看堆栈
- 检查共享内存的访问边界,确保读写操作在合法范围内。例如,检查数组访问是否越界。
int *sharedArray = (int *)shmat(shmid, NULL, 0); int size = 10; for (int i = 0; i < size; i++) { if (i < size) { sharedArray[i] = i; // 正确访问 } else { // 此处会导致段错误,需修正 } }
- 使用
- 同步机制失效:
- 使用
strace
工具查看系统调用,检查同步函数(如pthread_mutex_lock
等)的调用情况,看是否有异常返回。
strace -f your_program
- 增加日志输出,记录同步操作的时间和状态,以便分析同步冲突的原因。
#include <stdio.h> #include <pthread.h> pthread_mutex_t mutex; void *threadFunction(void *arg) { printf("Thread trying to lock mutex\n"); pthread_mutex_lock(&mutex); printf("Thread locked mutex\n"); // 临界区操作 pthread_mutex_unlock(&mutex); printf("Thread unlocked mutex\n"); return NULL; } int main() { pthread_mutex_init(&mutex, NULL); pthread_t thread; pthread_create(&thread, NULL, threadFunction, NULL); pthread_join(thread, NULL); pthread_mutex_destroy(&mutex); return 0; }
- 使用