当前位置: 技术问答>linux和unix
小弟编了一个非常简化的多线程模拟电梯程序,编译通过,可是运行啥结果也没有,请高手指点!
来源: 互联网 发布时间:2015-10-22
本文导语: 源程序如下: #include #include #include #include #include int currDir=0; int currNo=0; pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER; pthread_cond_t v=PTHREAD_COND_INITIALIZER; int i,j,k, jj,ii,kk,x; pthread_t upthread; pthread_t downthread; pthr...
源程序如下:
#include
#include
#include
#include
#include
int currDir=0;
int currNo=0;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t v=PTHREAD_COND_INITIALIZER;
int i,j,k, jj,ii,kk,x;
pthread_t upthread;
pthread_t downthread;
pthread_t upfirst;
void process()
{
int y=0;
for(y=0;y0)
pthread_cond_wait(&v,&mutex);
if(currDir!=0)
currDir=0;
currNo++;
pthread_mutex_unlock(&mutex);
process();
currNo--;
pthread_cond_signal(&v);
pthread_mutex_unlock(&mutex);
if(pthread_kill(pthread_self(),SIGKILL))
fprintf(stderr,"sldkfjsldfk");
}
void* down(void* b){
j=*((int*)b); printf("a customer arrives floor %d",&j);
pthread_mutex_lock(&mutex);
while(currDir!=1&&currNo>0)
pthread_cond_wait(&v,&mutex);
if(currDir!=1)
currDir=1;
currNo++;
pthread_mutex_unlock(&mutex);
process();
pthread_mutex_lock(&mutex);
currNo--;
pthread_cond_signal(&v);
pthread_mutex_unlock(&mutex);
if(pthread_kill(pthread_self(),SIGKILL))
fprintf(stderr,"sldkfjsldfk");
}
void main() {
int i,j,k,iRandom;
int iSeed=time(NULL);
srand(iSeed);
i=rand(1,100);
printf("pre i is:%dn",i);
i=i%5+1;
printf("i is: %dn",i);
ii=pthread_create(&upfirst,NULL,up,(void*)&i);
if(ii!=0){
perror("create thread error");
exit(1);}
for(x=0;x
#include
#include
#include
#include
#include
int currDir=0;
int currNo=0;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t v=PTHREAD_COND_INITIALIZER;
int i,j,k, jj,ii,kk,x;
pthread_t upthread;
pthread_t downthread;
pthread_t upfirst;
void process()
{
int y=0;
for(y=0;y0)
pthread_cond_wait(&v,&mutex);
if(currDir!=0)
currDir=0;
currNo++;
pthread_mutex_unlock(&mutex);
process();
currNo--;
pthread_cond_signal(&v);
pthread_mutex_unlock(&mutex);
if(pthread_kill(pthread_self(),SIGKILL))
fprintf(stderr,"sldkfjsldfk");
}
void* down(void* b){
j=*((int*)b); printf("a customer arrives floor %d",&j);
pthread_mutex_lock(&mutex);
while(currDir!=1&&currNo>0)
pthread_cond_wait(&v,&mutex);
if(currDir!=1)
currDir=1;
currNo++;
pthread_mutex_unlock(&mutex);
process();
pthread_mutex_lock(&mutex);
currNo--;
pthread_cond_signal(&v);
pthread_mutex_unlock(&mutex);
if(pthread_kill(pthread_self(),SIGKILL))
fprintf(stderr,"sldkfjsldfk");
}
void main() {
int i,j,k,iRandom;
int iSeed=time(NULL);
srand(iSeed);
i=rand(1,100);
printf("pre i is:%dn",i);
i=i%5+1;
printf("i is: %dn",i);
ii=pthread_create(&upfirst,NULL,up,(void*)&i);
if(ii!=0){
perror("create thread error");
exit(1);}
for(x=0;x