其他分享
首页 > 其他分享> > x

x

作者:互联网

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>

sem_t chopstick[5];
sem_t count;
void *philosopher(void *arg)
{
    int i=(int)arg;
    while(1){

	        sleep(3);     //thinking
	        int try= sem_trywait(&count);  //请求进入房间进餐
	        if(try!=0){
	        	continue;
	        }
	        printf("%d号哲学家正在思考\n",i);
	        try=sem_trywait(&chopstick[i]);    //请求左手边的筷子
	        if(try!=0){
	        	sem_post(&count);
	        	continue;
	        }
	        printf("%d号哲学家得到第%d根筷子\n",i,i);
	        try=sem_trywait(&chopstick[(i+1)%5]);  //请求右手边的筷子
	        if(try!=0){
			    sem_post(&count);
			    sem_post(&chopstick[i]);
			    continue;
	        }
	        printf("%d号哲学家得到第%d根筷子,可以进餐\n",i,(i+1)%5);

			printf("%d号哲学家进餐\n",i);
			sleep(3);
			        
			printf("%d号哲学家放下第%d根筷子\n",i,(i+1)%5);
			sem_post(&chopstick[(i+1)%5]);    //释放左手边的筷子

			printf("%d号哲学家放下第%d根筷子\n",i,i);
			sem_post(&chopstick[i]);  //释放右手边的筷子
			    	      
			sem_post(&count);   //释放信号量
    }
}

int main()
{
    pthread_t philosopher_threads[5];
    //创建线程
    sem_init(&count,0,4);
    sem_init(&chopstick,0,1);
    for(int i=0;i<5;i++){
        pthread_create(&philosopher_threads[i],NULL,philosopher,(void*)(int)i);
    }

    //挂起线程
    for(int i=0;i<5;i++){
        pthread_join(philosopher_threads[i],NULL);
    }

    
}
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>

sem_t chopstick[5]={1,1,1,1,1};
sem_t count=4;
void *philosopher(void *arg)
{
    int i=(int)arg;
    while(1){
    	if(i%2==0){
    		printf("%d号哲学家正在思考\n",i);
	        sleep(3);     //thinking
	        int try= sem_trywait(&count);  //请求进入房间进餐
	        if(try!=0){
	        	continue;
	        }
	        try=sem_trywait(&chopstick[i]);    //请求左手边的筷子
	        if(try!=0){
	        	sem_post(&count);
	        	continue;
	        }
	        printf("%d号哲学家得到第%d根筷子\n",i,i);
	        try=sem_trywait(&chopstick[(i+1)%5]);  //请求右手边的筷子
	        if(try!=0){
			    sem_post(&count);
			    sem_post(&chopstick[i]);
			    continue;
	        }
	        printf("%d号哲学家得到第%d根筷子,可以进餐\n",i,(i+1)%5);

			printf("%d号哲学家进餐\n",i);
			sleep(3);
			        
			printf("%d号哲学家放下第%d根筷子\n",i,i);
			sem_post(&chopstick[i]);    //释放左手边的筷子

			printf("%d号哲学家放下第%d根筷子\n",i,(i+1)%5);
			sem_post(&chopstick[(i+1)%5]);  //释放右手边的筷子
			    	      
			sem_post(&count);   //释放信号量
	         
	        
    	}else{
    		printf("%d号哲学家正在思考\n",i);
	        sleep(3);     //thinking
	        int try= sem_trywait(&count);  //请求进入房间进餐
	        if(try!=0){
	        	continue;
	        }
	        try=sem_trywait(&chopstick[(i+1)%5]);    //请求右手边的筷子
	        if(try!=0){
	        	sem_post(&count);
	        	continue;
	        }
	        printf("%d号哲学家得到第%d根筷子\n",i,(i+1)%5);
	        try=sem_trywait(&chopstick[i]);  //请求左手边的筷子
	        if(try!=0){
			    sem_post(&count);
			    sem_post(&chopstick[(i+1)%5]);
			    continue;
	        }
	        printf("%d号哲学家得到第%d根筷子,可以进餐\n",i,i);

			printf("%d号哲学家进餐\n",i);
			sleep(3);
			        
			printf("%d号哲学家放下第%d根筷子\n",i,(i+1)%5);
			sem_post(&chopstick[(i+1)%5]);    //释放右手边的筷子

			printf("%d号哲学家放下第%d根筷子\n",i,i);
			sem_post(&chopstick[i]);  //释放左手边的筷子
			    	      
			sem_post(&count);   //释放信号量
    		
    	}
        
    }
}

int main()
{
    pthread_t philosopher_threads[5];
    //创建线程
    for(int i=0;i<5;i++){
        pthread_create(&philosopher_threads[i],NULL,philosopher,(void*)(int)i);
    }

    //挂起线程
    for(int i=0;i<5;i++){
        pthread_join(philosopher_threads[i],NULL);
    }

    
}

标签:,筷子,chopstick,try,printf,post,sem
来源: https://www.cnblogs.com/ha-chuochuo/p/14847608.html