#include "sched.h" #include "pthread.h" #include "stdio.h" #include "stdlib.h" #include "semaphore.h" int producer(void * args); int consumer(void *args); pthread_mutex_t mutex; sem_t product; sem_t warehouse; char buffer[8][4]; int bp=0; main(int argc,char** argv) { pthread_mutex_init(&mutex,NULL); sem_init(&product,0,0); sem_init(&warehouse,0,8); int clone_flag,arg,retval; char *stack; clone_flag=CLONE_VM|CLONE_SIGHAND|CLONE_FS| CLONE_FILES; int i; for(i=0;i<2;i++) { //创建四个线程 arg = i; stack =(char*)malloc(4096); retval=clone((void*)producer,&(stack[4095]),clone_flag, (void*)&arg); stack =(char*)malloc(4096); retval=clone((void*)consumer,&(stack[4095]),clone_flag, (void*)&arg); } exit(1); } int producer(void* args) { int id = *((int*)args); int i; for(i=0;i<10;i++) { sleep(i+1); //表现线程速度差别 sem_wait(&warehouse); pthread_mutex_lock(&mutex); if(id==0) strcpy(buffer[bp],"aaa\0"); else strcpy(buffer[bp],"bbb\0"); bp++; printf("producer%d produce %s in %d\n",id,buffer[bp],bp-1); pthread_mutex_unlock(&mutex); sem_post(&product); } printf("producer%d is over!\n",id); } int consumer(void *args) { int id = *((int*)args); int i; for(i=0;i<10;i++) { sleep(10-i); //表现线程速度差别 sem_wait(&product); pthread_mutex_lock(&mutex); bp--; printf("consumer%d get %s in%d\n",id,buffer[bp],bp+1); strcpy(buffer[bp],"zzz\0"); pthread_mutex_unlock(&mutex); sem_post(&warehouse); } printf("consumer%d is over!\n",id); }这个代码在linu系统下有错误,应该如何修改
时间: 2023-11-30 14:03:25 浏览: 64
这份代码需要进行一些修改,才能在 Linux 系统下正常运行。以下是需要修改的地方:
1. `main` 函数没有返回类型,需要加上 `int`。
2. `producer` 和 `consumer` 函数需要加上返回类型,例如 `void*`。
3. 在 `for` 循环中创建线程时,需要传递一个指向 `int` 类型的指针作为参数,但是这里传递的是 `&arg`,应该改为 `(void*)&arg`。
4. `printf` 语句中的 `%s` 参数后面应该跟一个字符串,例如 `buffer[bp]`,而不是 `bp-1` 或 `bp+1`。
5. `strcpy` 函数需要传递一个字符串作为第二个参数,例如 `"aaa\0"`,而不是 `"aaa"`。
6. 在 `for` 循环中,`producer` 和 `consumer` 函数的调用顺序应该是先调用 `producer`,再调用 `consumer`。
7. 在 `main` 函数的最后,应该调用 `pthread_exit(NULL)`,而不是 `exit(1)`。
修改后的代码如下:
```
#include <sched.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
int producer(void *args);
int consumer(void *args);
pthread_mutex_t mutex;
sem_t product;
sem_t warehouse;
char buffer[8][4];
int bp = 0;
int main(int argc, char** argv) {
pthread_mutex_init(&mutex, NULL);
sem_init(&product, 0, 0);
sem_init(&warehouse, 0, 8);
int clone_flag, arg, retval;
char *stack;
int i;
for (i = 0; i < 2; i++) {
arg = i;
stack = (char*)malloc(4096);
retval = clone(producer, &(stack[4095]), CLONE_VM | CLONE_SIGHAND | CLONE_FS | CLONE_FILES, (void*)&arg);
stack = (char*)malloc(4096);
retval = clone(consumer, &(stack[4095]), CLONE_VM | CLONE_SIGHAND | CLONE_FS | CLONE_FILES, (void*)&arg);
}
pthread_exit(NULL);
}
void* producer(void* args) {
int id = *((int*)args);
int i;
for (i = 0; i < 10; i++) {
sleep(i + 1);
sem_wait(&warehouse);
pthread_mutex_lock(&mutex);
if (id == 0)
strcpy(buffer[bp], "aaa\0");
else
strcpy(buffer[bp], "bbb\0");
bp++;
printf("producer%d produce %s in %d\n", id, buffer[bp - 1], bp - 1);
pthread_mutex_unlock(&mutex);
sem_post(&product);
}
printf("producer%d is over!\n", id);
return NULL;
}
void* consumer(void* args) {
int id = *((int*)args);
int i;
for (i = 0; i < 10; i++) {
sleep(10 - i);
sem_wait(&product);
pthread_mutex_lock(&mutex);
bp--;
printf("consumer%d get %s in %d\n", id, buffer[bp], bp);
strcpy(buffer[bp], "zzz\0");
pthread_mutex_unlock(&mutex);
sem_post(&warehouse);
}
printf("consumer%d is over!\n", id);
return NULL;
}
```
阅读全文