diff --git a/README.md b/README.md index 0c669ac..5e68f1e 100644 --- a/README.md +++ b/README.md @@ -1,65 +1,195 @@ -# ThreadPrograming -#include +实验1.2 +#include #include //for exit() -#include //for pthread() +#include //for pthread() #include //for fork()and execl() -#include +#include #include //for sem_wait()and sem_post() int n=10,in=0,out=0,buffer[10]; //global variables:buffer sem_t defines signal and two signal inited in main //n用作取模,in和out分别当作生产者的放和消费者的取,buffer[10]代表十个缓冲区,可同时读和写 -sem_t empty,full; //当库存为空时,消费者不再拿。当库存为满时,生产者不再生产。 +sem_t empty,full; //当库存为空时,消费者不再拿。当库存为满时,生产者不再生产。 void* producer(void*arg) { - int tag=pthread_self()%100; //线程id的模,可重复 - int nextPro; //用作输出当前线程产品名 - srand(time(NULL)+tag); //pthread_self(): thread number Srand: rand seed; - // one producer produces constantly - while(1) - { - nextPro=rand()%97; - sem_wait(&empty); - buffer[in]=nextPro; - printf("production:%d %d\n",nextPro,buffer[in]); //buffer[in]可能在多线程中发生冲突而改变 - usleep(1000*1000/20); //sleep: unit is weimiao; 1 seconds = 1000*1000 weimiao,except 20 equal to 0.05 seconds; - in=(in+1)%n; - sem_post(&full); //wait and signal of signmount - usleep(1000*1000/2); - } + int tag=pthread_self()%100; //线程id的模,可重复 + int nextPro; //用作输出当前线程产品名 + srand(time(NULL)+tag); //pthread_self(): thread number Srand: rand seed; + // one producer produces constantly + while(1) + { + nextPro=rand()%97; + sem_wait(&empty); + buffer[in]=nextPro; + printf("production:%d %d\n",nextPro,buffer[in]); //buffer[in]可能在多线程中发生冲突而改变 + // usleep(1000*1000/20); //sleep: unit is weimiao; 1 seconds = 1000*1000 weimiao,except 20 equal to 0.05 seconds; + in=(in+1)%n; + sem_post(&full); //wait and signal of signmount + // usleep(1000*1000/2); + } } //consumer process void* consumer(void *arg) { - int item; - while(1) - { - sem_wait(&full); - item = buffer[out]; - sem_post(&empty); - printf("consume:%d\n",buffer[out]); - out=(out+1)%n; - usleep(1000*1000/2); - } + int item; + while(1) + { + sem_wait(&full); + item = buffer[out]; + sem_post(&empty); + printf("consume:%d\n",buffer[out]); + out=(out+1)%n; + usleep(1000*1000/2); + } } int main() { - pthread_t tid[2]; - //初始有10个缓冲区 - //线程号数组:根据需要,有多少线程,就定位多大 - sem_init(&empty,0,10); //整形信号量初始化函数:第3个参数表示信号量的初始值,第2个参数可暂时不管 - sem_init(&full,0,0); - pthread_create(&tid[0],NULL,producer,NULL); //create thread - pthread_create(&tid[1],NULL,consumer,NULL); - for(int i=0;i<2;i++) - { - pthread_join(tid[i],NULL); //等待所有线程结束,若不然,main结束后其他线程将被终止 - } - printf("main is over\n"); + pthread_t tid[2]; + //初始有10个缓冲区 + //线程号数组:根据需要,有多少线程,就定位多大 + sem_init(&empty,0,10); //整形信号量初始化函数:第3个参数表示信号量的初始值,第2个参数可暂时不管 + sem_init(&full,0,0); + pthread_create(&tid[0],NULL,producer,NULL); + pthread_create(&tid[1],NULL,consumer,NULL); + for(int i=0;i<2;i++) + { + pthread_join(tid[i],NULL); //等待所有线程结束,若不然,main结束后其他线程将被终止 + } + printf("main is over\n"); +} + +实验1.3 +#include +#include //for exit() +#include //for pthread() +#include //for fork()and execl() +#include +#include //for sem_wait()and sem_post() + +int n=10,in=0,out=0,buffer[10]; //global variables:buffer sem_t defines signal and two signal inited in main +//n用作取模,in和out分别当作生产者的放和消费者的取,buffer[10]代表十个缓冲区,可同时读和写 +sem_t empty,full; //当库存为空时,消费者不再拿。当库存为满时,生产者不再生产。 + +void* producer(void*arg) +{ + int tag=pthread_self()%100; //线程id的模,可重复 + int nextPro; //用作输出当前线程产品名 + srand(time(NULL)+tag); //pthread_self(): thread number Srand: rand seed; + // one producer produces constantly + while(1) + { + nextPro=rand()%97; + sem_wait(&empty); + buffer[in]=nextPro; + printf("production:%d %d\n",nextPro,buffer[in]); //buffer[in]可能在多线程中发生冲突而改变 + usleep(1000*1000/20); //sleep: unit is weimiao; 1 seconds = 1000*1000 weimiao,except 20 equal to 0.05 seconds; + in=(in+1)%n; + sem_post(&full); //wait and signal of signmount + usleep(1000*1000/2); + } +} +//consumer process +void* consumer(void *arg) +{ + int item; + while(1) + { + sem_wait(&full); + item = buffer[out]; + sem_post(&empty); + printf("consume:%d\n",buffer[out]); + out=(out+1)%n; + usleep(1000*1000/2); + } +} + +int main() +{ + pthread_t tid[2]; + //初始有10个缓冲区 + //线程号数组:根据需要,有多少线程,就定位多大 + sem_init(&empty,0,10); //整形信号量初始化函数:第3个参数表示信号量的初始值,第2个参数可暂时不管 + sem_init(&full,0,0); + pthread_create(&tid[0],NULL,producer,NULL); //create thread + pthread_create(&tid[1],NULL,consumer,NULL); + pthread_join(tid[0],NULL); //等待所有线程结束,若不然,main结束后其他线程将被终止 + usleep(1000*1000/2); + pthread_join(tid[1],NULL); + } + printf("main is over\n"); +} +(ps:程序明显卡顿) + + +实验3.2 4个生产者两个消费者 +#include +#include //for exit() +#include //for pthread() +#include //for fork()and execl() +#include +#include //for sem_wait()and sem_post() + +int n=10,in=0,out=0,buffer[10]; //global variables:buffer sem_t defines signal and two signal inited in main +//n用作取模,in和out分别当作生产者的放和消费者的取,buffer[10]代表十个缓冲区,可同时读和写 +sem_t empty,full; //当库存为空时,消费者不再拿。当库存为满时,生产者不再生产。 + +void* producer(void*arg) +{ + int tag=pthread_self()%100; //线程id的模,可重复 + int nextPro; //用作输出当前线程产品名 + srand(time(NULL)+tag); //pthread_self(): thread number Srand: rand seed; + // one producer produces constantly + while(1) + { + nextPro=rand()%97; + sem_wait(&empty); + buffer[in]=nextPro; + printf("production:%d %d\n",nextPro,buffer[in]); //buffer[in]可能在多线程中发生冲突而改变 + usleep(1000*1000/20); //sleep: unit is weimiao; 1 seconds = 1000*1000 weimiao,except 20 equal to 0.05 seconds; + in=(in+1)%n; + sem_post(&full); //wait and signal of signmount + usleep(1000*1000/2); + } +} +//consumer process +void* consumer(void *arg) +{ + int item; + while(1) + { + sem_wait(&full); + item = buffer[out]; + sem_post(&empty); + printf("consume:%d\n",buffer[out]); + out=(out+1)%n; + usleep(1000*1000/2); + } } +int main() +{ + pthread_t tid[6]; + //初始有10个缓冲区 + //线程号数组:根据需要,有多少线程,就定位多大 + sem_init(&empty,0,10); //整形信号量初始化函数:第3个参数表示信号量的初始值,第2个参数可暂时不管 + sem_init(&full,0,0); + for(int i=0;i<4;i++) + { + pthread_create(&tid[i],NULL,producer,NULL); //create thread + } + for(int i=4;i<6;i++) + { + pthread_create(&tid[i],NULL,consumer,NULL); + } + for(int i=0;i<6;i++) + { + pthread_join(tid[i],NULL); //等待所有线程结束,若不然,main结束后其他线程将被终止 + } + printf("main is over\n"); +} +实验3.2 6个生产者一个消费者 #include #include //for exit() #include //for pthread() @@ -123,6 +253,7 @@ int main() printf("main is over\n"); } +实验3.3.1 A,B,C,A,B,C,…… #include//for printf() #include//for exit() #include//for thread() @@ -132,63 +263,32 @@ int main() //order by A,B,C //thread order by p1,p2,p3 //when p1 printf A wait p3 printf C - //order by A,A,A B,B,B C,C,C - //thread order by p1 3 times,p2 3 times,p3 times - //when p1 printf A wait 3 times,printf B wait 3 times,printf C wait 3 times; - //sem_t a,b,c,add; sem_t a,b,c; void* p1(void *arg) { for(int i=0;i<10;i++) { - // sem_wait(&a); //a sign first ,add sign follow - // if(i%3==0) //when printf A first time wait - // { - // sem_wait(&add); - // } - sem_wait(&a); - printf("A\n"); + sem_wait(&a); + printf("A\n"); sem_post(&b); - // if(i%3==2) //when printf A last time signal - // { - // sem_post(&add); - // } } } void* p2(void *arg) { for(int i=0;i<10;i++) { - // sem_wait(&b); - // if(i%3==0) - // { - // sem_wait(&add); - // } sem_wait(&b); printf("B\n"); sem_post(&c); - // if(i%3==2) - // { - // sem_post(&add); - // } } } void* p3(void *arg) { for(int i=0;i<10;i++) { - // sem_wait(&c); - // if(i%3==0) - // { - // sem_wait(&add); - // } sem_wait(&c); printf("C\n"); sem_post(&a); - // if(i%3==2) - // { - // sem_post(&add); - // } } } int main() @@ -197,7 +297,86 @@ int main() sem_init(&a,0,1); sem_init(&b,0,0); sem_init(&c,0,0); -// sem_init(&add,0,1); + pthread_create(&tid[0],NULL,p1,NULL); + pthread_create(&tid[1],NULL,p2,NULL); + pthread_create(&tid[2],NULL,p3,NULL); + for(int i=0;i<3;i++) + { + pthread_join(tid[i],NULL); + } + printf("main is over\n"); +} + +实验3.3.2 A,A,A,B,B,B,C,C,C + #include//for printf() + #include//for exit() + #include//for thread() + #include// for fork()and execl() + #include + #include// for signal() and wait() + //order by A,A,A B,B,B C,C,C + //thread order by p1 3 times,p2 3 times,p3 times + //when p1 printf A wait 3 times,printf B wait 3 times,printf C wait 3 times; + //sem_t a,b,c,add; + sem_t a,b,c; + void* p1(void *arg) + { + for(int i=0;i<30;i++) + { + sem_wait(&a); //a sign first ,add sign follow + if(i%3==0) //when printf A first time wait + { + sem_wait(&add); + } + printf("A\n"); + sem_post(&b); + if(i%3==2) //when printf A last time signal + { + sem_post(&add); + } + } +} +void* p2(void *arg) +{ + for(int i=0;i<30;i++) + { + sem_wait(&b); + if(i%3==0) + { + sem_wait(&add); + } + printf("B\n"); + sem_post(&c); + if(i%3==2) + { + sem_post(&add); + } + } +} +void* p3(void *arg) +{ + for(int i=0;i<30;i++) + { + sem_wait(&c); + if(i%3==0) + { + sem_wait(&add); + } + printf("C\n"); + sem_post(&a); + if(i%3==2) + { + sem_post(&add); + } + } +} +int main() +{ + pthread_t tid[3]; + sem_init(&a,0,3); + sem_init(&b,0,0); + sem_init(&c,0,0); + sem_init(&add,0,1); pthread_create(&tid[0],NULL,p1,NULL); pthread_create(&tid[1],NULL,p2,NULL); pthread_create(&tid[2],NULL,p3,NULL);