You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
przpe8i37 bfb0e130df
Update README.md
4 years ago
README.md Update README.md 4 years ago

README.md

ThreadPrograming

#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); for(int i=0;i<2;i++) { pthread_join(tid[i],NULL); //等待所有线程结束若不然main结束后其他线程将被终止 } printf("main is over\n"); }

#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"); }

#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
//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_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() { pthread_t tid[3]; 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"); }