|
|
4 years ago | |
|---|---|---|
| README.md | 4 years ago | |
| 新建 DOC 文档.doc | 4 years ago | |
README.md
打开自行练习环境 cd /data/workspace/myshixun/case1/ //切换目录至代码文件目录 ls //查看目录列表文件 vim hellothread.cpp //按i进行修改操作 按esc退出修改操作 若要保存并退出 esc退出编辑模式后 :或/或? 命令提示符+wq或x q不保存退出 g++ -pthread hellothread.cpp -o daling_123.out //-o 重命名否则默认为a.out -pthread引入函数库文件 ./daling_123.out //执行.out可执行文件 (若是无限循环,使用Ctrl+C退出); (若要 创建文件 切换到指定目录后 touch 文件名); (tips:按键盘上下键可以回溯历史指令) 实验1.2 #include <stdio.h> #include <stdlib.h>//for exit() #include <pthread.h>//for pthread() #include <unistd.h> //for fork()and execl() #include <sys/types.h> #include <semaphore.h>//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(voidarg) { 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(10001000/20); //sleep: unit is weimiao; 1 seconds = 10001000 weimiao,except 20 equal to 0.05 seconds; in=(in+1)%n; sem_post(&full); //wait and signal of signmount // usleep(10001000/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(10001000/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); 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 <stdio.h> #include <stdlib.h>//for exit() #include <pthread.h>//for pthread() #include <unistd.h> //for fork()and execl() #include <sys/types.h> #include <semaphore.h>//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(voidarg) { 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(10001000/20); //sleep: unit is weimiao; 1 seconds = 10001000 weimiao,except 20 equal to 0.05 seconds; in=(in+1)%n; sem_post(&full); //wait and signal of signmount usleep(10001000/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(10001000/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:程序明显卡顿)
实验2.4 4个生产者两个消费者 #include <stdio.h> #include <stdlib.h>//for exit() #include <pthread.h>//for pthread() #include <unistd.h> //for fork()and execl() #include <sys/types.h> #include <semaphore.h>//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(voidarg) { 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(10001000/20); //sleep: unit is weimiao; 1 seconds = 10001000 weimiao,except 20 equal to 0.05 seconds; in=(in+1)%n; sem_post(&full); //wait and signal of signmount usleep(10001000/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(10001000/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 <stdio.h> #include <stdlib.h>//for exit() #include <pthread.h>//for pthread() #include <unistd.h> //for fork()and execl() #include <sys/types.h> #include <semaphore.h>//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(voidarg) { 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(10001000/20); //sleep: unit is weimiao; 1 seconds = 10001000 weimiao,except 20 equal to 0.05 seconds; in=(in+1)%n; sem_post(&full); //wait and signal of signmount usleep(10001000/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(10001000/2); } }
int main() { pthread_t tid[7]; //初始有10个缓冲区 //线程号数组:根据需要,有多少线程,就定位多大 sem_init(&empty,0,10); //整形信号量初始化函数:第3个参数表示信号量的初始值,第2个参数可暂时不管 sem_init(&full,0,0); for(int i=0;i<6;i++) { pthread_create(&tid[i],NULL,producer,NULL); //create thread } pthread_create(&tid[6],NULL,consumer,NULL); for(int i=0;i<7;i++) { pthread_join(tid[i],NULL); //等待所有线程结束,若不然,main结束后其他线程将被终止 } printf("main is over\n"); }
实验3.3.1 A,B,C,A,B,C,…… #include<stdio.h>//for printf() #include<stdlib.h>//for exit() #include<pthread.h>//for thread() #include<unistd.h>// for fork()and execl() #include<sys/types.h> #include<semaphore.h>// for signal() and wait() //order by A,B,C //thread order by p1,p2,p3 //when p1 printf A wait p3 printf C sem_t a,b,c; void* p1(void arg) { for(int i=0;i<10;i++) { sem_wait(&a); printf("A\n"); sem_post(&b); } } void p2(void arg) { for(int i=0;i<10;i++) { sem_wait(&b); printf("B\n"); sem_post(&c); } } void p3(void *arg) { for(int i=0;i<10;i++) { sem_wait(&c); printf("C\n"); sem_post(&a); } } int main() { pthread_t tid[3]; sem_init(&a,0,1); sem_init(&b,0,0); sem_init(&c,0,0); 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<stdio.h>//for printf() #include<stdlib.h>//for exit() #include<pthread.h>//for thread() #include<unistd.h>// for fork()and execl() #include<sys/types.h> #include<semaphore.h>// 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); for(int i=0;i<3;i++) { pthread_join(tid[i],NULL); } printf("main is over\n"); }