プログラミング・モデル:同時プログラミング関連コード


シリアル販売モデル
#include 

void sell (int p, int num) {
	while (num > 0) {
		printf(" #%d 1 
", p); num--; } printf(" #%d
", 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 
", p->id); p->num--; usleep(rand()%120); } printf(" #%d
", 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 
", 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.
", 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.
", 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 
", i); delay(50000); printf(" %d
", i); sem_wait(&r); sem_wait(&chopsticks[left]); printf(" %d %d , ,
", i, left); sem_wait(&chopsticks[right]); printf(" %d %d , ,
", i, right); delay(50000); sem_post(&r); sem_post(&chopsticks[left]); printf(" %d %d
", i, left); sem_post(&chopsticks[right]); printf(" %d %d
", i, right); } } int main (int argc, char **argv) { srand(time(NULL)); pthread_t PHD[N]; for (int i=0; i