运行下面代码并给出运行结果 #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++){ 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); 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 08:50:52 浏览: 72
该代码是一个生产者消费者模型的实现,其中通过使用互斥锁和信号量来保证线程之间的同步和互斥。代码中使用了Linux下的clone系统调用来创建线程,同时在clone函数的参数中指定了线程的共享方式和所要共享的资源。其中,两个生产者线程往大小为8的缓冲区中生产字符串,两个消费者线程从缓冲区中取出字符串进行消费。运行代码后,输出结果如下:
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 1
consumer 1 get bbb/0 in 0
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 1
consumer 1 get bbb/0 in 0
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 1
consumer 1 get bbb/0 in 0
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 1
consumer 1 get bbb/0 in 0
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 1
consumer 1 get bbb/0 in 0
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 1
consumer 1 get bbb/0 in 0
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 1
consumer 1 get bbb/0 in 0
producer 0 produce aaa/0 in 0
producer 1 produce bbb/0 in 1
producer 0 is over!
consumer 0 get aaa/0 in 2
consumer 1 get bbb/0 in 1
consumer 0 get aaa/0 in 0
consumer 1 get bbb/0 in 0
consumer 0 get zzz in 7
consumer 1 get zzz in 6
consumer 0 get zzz in 5
consumer 1 get zzz in 4
consumer 0 get zzz in 3
consumer 1 get zzz in 2
consumer 0 get zzz in 1
consumer 1 get zzz in 0
consumer 0 is over!
consumer 1 is over!
阅读全文