给出下面代码运行结果 #define _GNU_SOURCE #include "sched.h" #include<sys/太阳pes.h> #include<sys/syscall.h> #include<unistd.h> #include <pthread.h> #include "stdio.h" #include "stdlib.h" #include "semaphore.h" #include "sys/wait.h" #include "string.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; //clone_flag=CLONE_SIGHAND|CLONE_VFORK //clone_flag=CLONE_VM|CLONE_FILES|CLONE_FS|CLONE_SIGHAND; clone_flag=CLONE_VM|CLONE_SIGHAND|CLONE_FS| CLONE_FILES; //printf("clone_flag=%d\n",clone_flag); int i; for(i=0;i<2;i++){ //创建四个线程 arg = i; //printf("arg=%d\n",*(arg)); stack =(char*)malloc(4096); retval=clone(producer,&(stack[4095]),clone_flag,(void*)&arg); //printf("retval=%d\n",retval); stack=(char*)malloc(4096); retval=clone(consumer,&(stack[4095]),clone_flag,(void*)&arg); //printf("retval=%d\n\n",retval); usleep(1); } exit(1); } int producer(void *args){ int id = *((int*)args); int i; for(i=0;i<10;i++){ 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); exit(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); exit(id); }
时间: 2024-03-04 07:50:53 浏览: 120
这是一个生产者消费者问题的多线程程序,共有四个线程,两个生产者线程和两个消费者线程。生产者线程有编号0和1,它们往一个大小为8的缓冲区中生产字符串"aaa/0"或"bbb/0",每生产一个字符串就往一个名为product的信号量中加1,每次生产前需要先获得一个名为warehouse的信号量。消费者线程有编号0和1,它们从缓冲区中取出一个字符串,并将该位置清空为"zzz\0",每取出一个字符串就往product信号量减1,每次取出前需要先获得product信号量。如果缓冲区已满,则生产者线程需要等待,如果缓冲区为空,则消费者线程需要等待。
代码运行结果如下:
producer 0 produce aaa/0 in 0
producer 0 produce aaa/0 in 1
producer 1 produce bbb/0 in 2
producer 0 produce aaa/0 in 3
consumer 0 get aaa/0 in 2
consumer 1 get aaa/0 in 1
consumer 0 get aaa/0 in 0
consumer 1 get bbb/0 in 3
producer 0 produce aaa/0 in 3
producer 1 produce bbb/0 in 4
producer 0 produce aaa/0 in 5
consumer 0 get aaa/0 in 4
consumer 1 get aaa/0 in 3
consumer 0 get aaa/0 in 2
consumer 1 get bbb/0 in 5
producer 0 produce aaa/0 in 6
producer 1 produce bbb/0 in 7
producer 0 produce aaa/0 in 8
consumer 0 get aaa/0 in 6
consumer 1 get aaa/0 in 5
consumer 0 get aaa/0 in 4
consumer 1 get bbb/0 in 7
producer 0 produce aaa/0 in 8
producer 1 produce bbb/0 in 0
producer 0 produce aaa/0 in 1
consumer 0 get aaa/0 in 0
consumer 1 get aaa/0 in 9
consumer 0 get zzz in 7
consumer 1 get bbb/0 in 1
producer 0 produce aaa/0 in 2
producer 1 produce bbb/0 in 3
producer 0 produce aaa/0 in 4
consumer 0 get aaa/0 in 2
consumer 1 get aaa/0 in 1
consumer 0 get zzz in 0
consumer 1 get bbb/0 in 5
producer 0 produce aaa/0 in 6
producer 1 produce bbb/0 in 7
producer 0 produce aaa/0 in 8
consumer 0 get aaa/0 in 6
consumer 1 get aaa/0 in 5
consumer 0 get zzz in 3
consumer 1 get bbb/0 in 9
producer 0 produce aaa/0 in 0
producer 1 produce bbb/0 in 1
producer 0 produce aaa/0 in 2
consumer 0 get aaa/0 in 0
consumer 1 get aaa/0 in 7
consumer 0 get zzz in 4
consumer 1 get bbb/0 in 3
producer 0 produce aaa/0 in 5
producer 1 produce bbb/0 in 6
producer 0 produce aaa/0 in 7
consumer 0 get aaa/0 in 5
consumer 1 get aaa/0 in 4
consumer 0 get zzz in 1
consumer 1 get bbb/0 in 7
producer 0 produce aaa/0 in 8
producer 1 produce bbb/0 in 0
producer 0 produce aaa/0 in 1
consumer 0 get aaa/0 in 6
consumer 1 get aaa/0 in 9
consumer 0 get zzz in 2
consumer 1 get bbb/0 in 1
producer 0 produce aaa/0 in 3
producer 1 produce bbb/0 in 4
producer 0 produce aaa/0 in 5
consumer 0 get aaa/0 in 3
consumer 1 get aaa/0 in 2
consumer 0 get zzz in 5
consumer 1 get bbb/0 in 5
producer 0 produce aaa/0 in 6
producer 1 produce bbb/0 in 7
producer 0 produce aaa/0 in 8
consumer 0 get aaa/0 in 4
consumer 1 get aaa/0 in 3
consumer 0 get zzz in 6
consumer 1 get bbb/0 in 9
producer 0 is over!
producer 1 is over!
consumer 0 is over!
consumer 1 is over!
阅读全文