?? main.c
字號(hào):
#include "stdio.h"
#include <pthread.h>
#include <sys/time.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
/*緩沖區(qū)的最大數(shù)*/
#define Size 20
//記錄生產(chǎn)者的人數(shù)
int sh=0;
//記錄消費(fèi)者的人數(shù)
int xiao1=0;
/*緩沖區(qū)的結(jié)構(gòu)體*/
struct huan{
int isHave; //標(biāo)記緩沖區(qū)是否有內(nèi)容,有內(nèi)容時(shí),消費(fèi)者才可以使用,
//沒內(nèi)容時(shí),生產(chǎn)者才 可以往緩沖區(qū)內(nèi)寫東西
//0表示沒有內(nèi)容,1表示有內(nèi)容
int jud; //標(biāo)志緩沖區(qū)是否在被使用中,0代表未被使用,1表示使用中
int context; //緩沖的內(nèi)容
};
//描述生產(chǎn)者,或者是消費(fèi)者
struct person{
int end; //記錄是否完成任務(wù),0表示完成,1表示未完成任務(wù)
char * name; /*線程名*/
pthread_t thread; /*線程句柄*/
};
/*控制線程*/
//pthread_t thread;
//緩沖區(qū)
struct huan * huan[20];
//生產(chǎn)者,最多有10個(gè)生產(chǎn)者
struct person * sheng[10];
//消費(fèi)者,最多有10個(gè)消費(fèi)者
struct person * xiao[10];
//要放進(jìn)入緩沖的內(nèi)容的標(biāo)記
int context=1;
int timesj(int i) //timesj函數(shù),可產(chǎn)生一個(gè)隨機(jī)數(shù)返回
{
int xt;
//為偽隨機(jī)數(shù)初始化
xt = rand() %i+1;
return xt;
}
//生產(chǎn)者往緩沖區(qū)內(nèi)放東西
//i是緩沖區(qū)的下標(biāo)值,context是要放入緩沖區(qū)的內(nèi)容,currentSheng是當(dāng)前放進(jìn)去的生產(chǎn)者
void put(int i,int context, struct person * currentSheng){
huan[i]->jud=1; //標(biāo)志緩沖區(qū)正在使用
printf("\n生產(chǎn)者%s 往緩沖區(qū)%d 中寫入%d",¤tSheng->name,i,context);
//輸出當(dāng)前生產(chǎn)者的名字,緩沖區(qū)的下標(biāo)值和對(duì)應(yīng)緩沖區(qū)里的內(nèi)容
huan[i]->context=context; // 往緩沖區(qū)中放入內(nèi)容
huan[i]->isHave=1; //表示有內(nèi)容
huan[i]->jud=0; //釋放空間,緩沖區(qū)未被使用
}
//消費(fèi)者往緩沖區(qū)內(nèi)拿東西
//i是緩沖區(qū)的下標(biāo)值,currentXiao是當(dāng)前拿緩沖區(qū)東西的消費(fèi)者
int take(int i,struct person * currentXiao){
huan[i]->jud=1; //標(biāo)志緩沖區(qū)正在使用
printf("\n消費(fèi)者%s 往緩沖區(qū)%d 中拿出%d",¤tXiao->name,i,huan[i]->context);
//輸出當(dāng)前消費(fèi)者的名字,緩沖區(qū)的下標(biāo)值和對(duì)應(yīng)緩沖區(qū)里的內(nèi)容
huan[i]->isHave=0; //表示沒有內(nèi)容
huan[i]->jud=0; //釋放空間,緩沖區(qū)未被使用
}
void * shengchan(struct person * currentSheng){
int i=0;
for(i=0;i<Size;i++){
//如果緩沖區(qū)沒有內(nèi)容而且緩沖區(qū)未被使用
if(huan[i]->isHave==0&&huan[i]->jud==0){
put(i,context++,currentSheng); //當(dāng)前生產(chǎn)者往里面放內(nèi)容
sleep(timesj(3)); //掛起一段時(shí)間
//線程沉睡若干秒,模仿線程因?yàn)楣ぷ鞫褂昧说臅r(shí)間
}
}
currentSheng->end=1; //當(dāng)前生產(chǎn)者任務(wù)未完成
}
void * xiaofei(struct person * currentXiao){
int i=0;
while(context<20)
for(i=0;i<Size;i++){
//如果緩沖區(qū)有內(nèi)容而且緩沖區(qū)未被使用
if(huan[i]->isHave==1&&huan[i]->jud==0){
take(i,currentXiao); //當(dāng)前消費(fèi)者取出緩沖區(qū)的內(nèi)容
sleep(timesj(3)); //掛起一段時(shí)間
//線程沉睡若干秒,模仿線程因?yàn)楣ぷ鞫褂昧说臅r(shí)間
}
}
currentXiao->end=1; //當(dāng)前消費(fèi)者未完成任務(wù)
}
int getNumber(){
int a;
a=-99999;
while(1){
scanf("%d",&a);
getchar();
if(a<=-99999){
printf("\n你輸入的不是數(shù)字,請(qǐng)重新輸入:");
}
else break;
}
return a;
}
int main(){
int i=0,j=0;
time_t t;
//為隨機(jī)函數(shù)做準(zhǔn)備
srand((unsigned) time(&t)); // 以系統(tǒng)時(shí)間做種子,初始化rand()
for(i=0;i<Size;i++){
huan[i]=(struct huan *)malloc(sizeof(struct huan));
//動(dòng)態(tài)分配存儲(chǔ)區(qū),返回一個(gè)指向struct huan的指針
huan[i]->isHave=0; //緩沖區(qū)沒有內(nèi)容
huan[i]->jud=0; //緩沖區(qū)未被使用
}
printf("請(qǐng)輸入生產(chǎn)者的數(shù)量:");
/*
scanf("%d",&j);
getchar();
*/
j=getNumber();
sh=j;
for(i=0;i<j;i++){
sheng[i]=(struct person *)malloc(sizeof(struct person ));
//動(dòng)態(tài)分配存儲(chǔ)區(qū),返回一個(gè)指向struct person的指針
printf("請(qǐng)輸入生產(chǎn)者的名稱:");
scanf("%s",&sheng[i]->name);
getchar();
sheng[i]->end=0; //標(biāo)志生產(chǎn)者完成任務(wù)
}//for完成
printf("請(qǐng)輸入消費(fèi)者的數(shù)量:");
// scanf("%d",&j);
// getchar();
j=getNumber();
xiao1=j;
for(i=0;i<j;i++){
xiao[i]=(struct person *)malloc(sizeof(struct person));
//動(dòng)態(tài)分配存儲(chǔ)區(qū),返回一個(gè)指向struct person的指針
printf("請(qǐng)輸入消費(fèi)者的名稱:");
scanf("%s",&xiao[i]->name);
getchar();
xiao[i]->end=0; // 標(biāo)志消費(fèi)者完成任務(wù)
}
for(i=0;i<sh;i++){
//動(dòng)態(tài)分配存儲(chǔ)區(qū),返回一個(gè)指向&sheng[i]->thread的指針
memset(&sheng[i]->thread, 0, sizeof(sheng[i]->thread));
//創(chuàng)建線程(LINUX) ,(調(diào)用(void *)shengchan方法,sheng[i]是方法的參數(shù),&sheng[i]->thread為線 程地址)
if((pthread_create(&sheng[i]->thread, NULL, (void *)shengchan,sheng[i])) != 0) {
printf("線程創(chuàng)建失敗!\n");
getchar();
exit(0);
return ;
}//if完成
}
for(i=0;i<xiao1;i++){
//動(dòng)態(tài)分配存儲(chǔ)區(qū),返回一個(gè)指向&sheng[i]->thread的指針
//memset(&sheng[i]->thread, 0, sizeof(sheng[i]->thread));
//動(dòng)態(tài)分配存儲(chǔ)區(qū),返回一個(gè)指向&xiao[i]->thread的指針
memset(&xiao[i]->thread, 0, sizeof(xiao[i]->thread));
//創(chuàng)建線程(LINUX) ,(調(diào)用(void *)xiaofei方法,xiao[i]是方法的參數(shù),&xiao[i]->thread為線程地址)
if((pthread_create(&xiao[i]->thread, NULL, (void *)xiaofei,xiao[i])) != 0) {
printf("線程創(chuàng)建失敗!\n");
getchar();
exit(0);
return ;
}
}
while(1){
sleep(18);
for(i=0;i<sh;i++) //如果生產(chǎn)者執(zhí)行完,下一個(gè)繼續(xù)生產(chǎn)
if(sheng[i]->end==0){
continue;
}
for(i=0;i<xiao1;i++) //如果消費(fèi)者執(zhí)行完,下一個(gè)繼續(xù)消費(fèi)
if(xiao[i]->end==0){
continue;
}
printf("\n演示完畢\n");
return;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -