根据这些问题修改以上代码
时间: 2024-10-14 08:11:54 浏览: 18
查看修改源代码工具
针对上述提到的问题,我们可以改进代码如下:
1. 添加适当的错误处理:当`fork`、`semop`或其他系统调用失败时,记录错误信息并退出程序,以便于调试。
2. 对于父子进程之间的交互,为了确保任务按预期顺序执行,可以引入一些互斥锁或队列来保证流程控制。
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#define MAX_THREADS 3
#define NUM_FRUITS 5
#define SEMAPHORE_NAME "fruits"
typedef struct {
int id;
bool hasFruit;
pthread_mutex_t mutex;
sem_t *sem;
} ThreadData;
void* put_fruit(void* arg) {
ThreadData* data = (ThreadData*)arg;
while (!data->hasFruit) {
pthread_mutex_lock(&data->mutex);
if (rand() % NUM_FRUITS == 0) {
sem_post(data->sem); // 释放信号量,表示有水果
data->hasFruit = true;
}
pthread_mutex_unlock(&data->mutex);
sleep(rand() % 3); // 模拟放水果时间
}
}
void* eat_fruit(void* arg) {
ThreadData* data = (ThreadData*)arg;
while (data->hasFruit) {
pthread_mutex_lock(&data->mutex);
if (data->hasFruit) {
printf("Eating fruit...\n");
sem_wait(data->sem); // 请求信号量,表示等待水果
data->hasFruit = false;
} else {
printf("No fruit available.\n");
}
pthread_mutex_unlock(&data->mutex);
sleep(rand() % 5); // 模拟吃水果时间
}
}
int main(int argc, char *argv[]) {
int sem_id;
ThreadData threads[MAX_THREADS];
if ((sem_id = sem_open(SEMAPHORE_NAME, O_CREAT, 0666, NUM_FRUITS)) == SEM_FAILED) {
perror("sem_open");
exit(EXIT_FAILURE);
}
for (int i = 0; i < MAX_THREADS; i++) {
threads[i].id = i;
threads[i].hasFruit = false;
threads[i].sem = &sem_id;
pthread_mutex_init(&threads[i].mutex, NULL);
}
for (int i = 0; i < MAX_THREADS; i++) {
switch (i) {
case 0: put_fruit_thread = pthread_create(&threads[i].thread, NULL, put_fruit, &threads[i]); break;
case 1: eat_fruit_thread = pthread_create(&threads[i].thread, NULL, eat_fruit, &threads[i]); break;
default: break; // No need to create more than two threads
}
if (put_fruit_thread == NULL || eat_fruit_thread == NULL) {
perror("pthread_create");
exit(EXIT_FAILURE);
}
}
for (int i = 0; i < MAX_THREADS; i++) {
if (pthread_join(threads[i].thread, NULL) != 0) {
perror("pthread_join");
exit(EXIT_FAILURE);
}
}
sem_close(sem_id);
return 0;
}
```
在这个版本中,我们使用了互斥锁`pthread_mutex_t`来保护共享数据,并在`put_fruit`和`eat_fruit`线程之间同步。同时,我们也改进了信号量的使用,不再直接修改其值,而是通过`sem_post`和`sem_wait`来增加或减少信号量计数。这使得进程间的交互更为安全和有序。
阅读全文