编程范式--并发编程相关代码

串行式销售模式

#include 

void sell (int p, int num) {
	while (num > 0) {
		printf("售票点#%d售出1张票\n", p);
		num--;
	}
	printf("售票点#%d售罄\n", p);
}

int main (int argc, char const **argv) {
	int n = 3;
	int x = 12;
	
	for (int i=1; i<=n; i++) {
		sell(i, x/n);
	}
	return 0;
}

并行式销售模式

#include 
#include 
#include 
#include 
#include 
#include 

typedef struct SellInfo {
	int id;
	int num;
}sellInfo;

void* sell (void* v) {
	//申请p指针,指向v的内存
	sellInfo* p = (struct SellInfo *) v;
	while (p->num > 0) {
		printf("售票点#%d售出1张票\n", p->id);
		p->num--;
		usleep(rand()%120);
	}
	printf("售票点#%d售罄\n", p->id);
	free(p);
}

int main (int argc, char const **argv) {
	int n = 3;
	int x = 12;
	srand(time(NULL));
	pthread_t th[3] = {0};
	for (int i=1; i<=n; i++) {
		sellInfo* p = (struct SellInfo *)malloc(sizeof(struct SellInfo));
		p->id = i;
		p->num = x / n;
		pthread_create(&th[i-1], NULL, sell, p);
	}
	for (int i=1; i<=n; i++) {
		pthread_join(th[i], NULL);
	}
	return 0;
}

竞争式销售模式

#include 
#include 
#include 
#include 
#include 
#include 

typedef struct SellInfo {
	int id;
	int num;
}sellInfo;

int total = 120;
pthread_mutex_t mutex;

void* sell (void* v) {
	sellInfo* p = (struct SellInfo *) v;
	int cur;
	while (1) {
		pthread_mutex_lock(&mutex);
		cur = total;
		if (cur <= 0) {
			printf("售罄!!!");
			pthread_mutex_unlock(&mutex);
			break;
		}
		printf("当前票数为:%d  ", cur);
		usleep(rand()%90000);
		printf("售票点#%d售出一张票\n", p->id);
		cur--;
		total = cur;
		pthread_mutex_unlock(&mutex);
	}
	free(p);
}

int main (int argc, char const **argv) {
	int n = 20;
	srand(time(NULL));
	pthread_t th[20] = {0};
	pthread_mutex_init(&mutex, NULL);
	for (int i=0; iid = i + 1;
		p->num = 0;
		pthread_create(&th[i], NULL, sell, p);
	}
	for (int i=0; i

协调生产和消费进度

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define N 14

int buf[N] = {-1};

int putin = 0;
int takeout = 0;

sem_t unoccupied;
sem_t occupied;

pthread_mutex_t mutex;

void delay (int len) {
	int i = rand() % len;
	int x;
	while (i > 0) {
		x = rand() % len;
		while (x > 0) {
			x--;
		}
		i--;
	}
}

void producer () {
	while (1) {
		int d = 1 + rand()%100;
		delay(50000);
		sem_wait(&unoccupied);
		pthread_mutex_lock(&mutex);
		buf[putin] = d;
		printf("Put %d to the buffer at %d.\n", d, putin);
		putin++;
		if (putin == N) {
			putin = 0;
		}
		sem_post(&occupied);
		pthread_mutex_unlock(&mutex);
	}
}

void consumer () {
	while (1) {
		delay (50000);
		sem_wait(&occupied);
		pthread_mutex_lock(&mutex);
		printf("Take out %d from the buffer at %d.\n", buf[takeout], takeout);
		buf[takeout] = -1;
		takeout++;
		if (takeout == N) {
			takeout = 0;
		}
		sem_post(&unoccupied);
		pthread_mutex_unlock(&mutex);
	}
}

int main (int argc, char const** argv) {
	pthread_t manufacture;
	pthread_t customer;
	
	pthread_mutex_init(&mutex, NULL);
	
	pthread_create(&manufacture, NULL, (void*)producer, NULL);
	pthread_create(&customer, NULL, (void*)consumer, NULL);
	
	sem_init(&unoccupied, 0, N);
	sem_init(&occupied, 0, 0);
	
	pthread_join(customer, NULL);
	pthread_join(manufacture, NULL);
	
	
	sem_destroy(&unoccupied);
	sem_destroy(&occupied);
	
	pthread_mutex_destroy(&mutex);
	
	return 0;
}

哲学家就餐问题

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define N 5

sem_t chopsticks[5];
sem_t r;

int philosophers[5] = {0, 1, 2, 3, 4};

void delay (int len) {
	int i = rand() % len;
	int x;
	while (i > 0) {
		x = rand() % len;
		while (x > 0) {
			x--;
		}
		i--;
	}
}

void philosopher (void* arg) {
	int i = *(int *)arg;
	int left = i;
	int right = (i + 1) % N;
	while (1) {
		printf("哲学家%d正在思考问题\n", i);
		delay(50000);
		
		printf("哲学家%d饿了\n", i);
		sem_wait(&r);
		sem_wait(&chopsticks[left]);
		printf("哲学家%d拿起了%d号筷子,现在只有一支筷子,不能进餐\n", i, left);
		sem_wait(&chopsticks[right]);
		printf("哲学家%d拿起了%d号筷子, 现在有两支筷子,开始进餐\n", i, right);
		delay(50000);
		sem_post(&r);
		sem_post(&chopsticks[left]);
		printf("哲学家%d放下了%d号筷子\n", i, left);
		sem_post(&chopsticks[right]);
		printf("哲学家%d放下了%d号筷子\n", i, right);
	}
}

int main (int argc, char **argv) {
	srand(time(NULL));
	pthread_t PHD[N];
	
	
	for (int i=0; i

你可能感兴趣的:(计算机系统基础)