• 2


    
    #include "sched.h" 
    #include "pthread.h"
    #include "stdio.h"
    #include "stdlib.h"
    #include "semaphore.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_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);
    		sleep(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");
    		else
    			strcpy(buffer[bp],"bbb");
    		bp++;
    		printf("producer %d produce %s in %d 
    ",id,buffer[bp-1],bp-1);
    		pthread_mutex_unlock(&mutex);
    		sem_post(&product);
    	}
    	printf("producer %d is over!
    ",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 
    ",id,buffer[bp],bp);
    		strcpy(buffer[bp],"zzz");
    		pthread_mutex_unlock(&mutex);
    		sem_post(&warehouse);
    	}
    	printf("consumer %d is over! 
    ",id);
    }
    
    
  • 相关阅读:
    贰、js的基础(二)类型转换
    贰、js的基础(一)
    ajax的异步请求小结
    壹、js的概述
    sass的用法小结(四)进阶篇
    sass的用法小结(三)
    sass的用法小结(二)
    sass的用法小结(一)
    H5页面在线制作工具搜集
    H5教程:移动页面性能优化
  • 原文地址:https://www.cnblogs.com/1112wlt/p/14076179.html
Copyright © 2020-2023  润新知