⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 read_write.txt

📁 linux下读者写者程序
💻 TXT
字号:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
#include <signal.h>   

#define in 0
#define out 1
#define waiting 2

static void printids(const char *s);
static void* thr_read(void *arg);
static void* thr_write(void *arg);
sem_t mutex_r, mutex_w, mutex_p, db;
int rc = 0, wc = 0;
int readP[4], writeP[4];
//readP = (int *)malloc(4*sizeof(int));
//writeP = (int *)malloc(4*sizeof(int));
static void sig_int(int);
int main(void)
{
	
	pthread_t n1, n2, n3, n4, n5, n6, n7, n8;
	signal(SIGINT, sig_int);
	
	int i, err;
	for(i = 0; i < 4; i++) {
		readP[i] = out;
		writeP[i] = out;
	}

        sem_init(&mutex_r, 0, 1);
	sem_init(&mutex_w, 0, 1);
        sem_init(&db, 0, 1);
        
	err = pthread_create(&n1, NULL, thr_read, NULL);
	err = pthread_create(&n5, NULL, thr_read, NULL);
	err = pthread_create(&n3, NULL, thr_read, NULL);
        err = pthread_create(&n2, NULL, thr_write, NULL);
	
        sleep(1);
        err = pthread_create(&n4, NULL, thr_write, NULL);

        sleep(1);
        err = pthread_create(&n6, NULL, thr_write, NULL);
	err = pthread_create(&n7, NULL, thr_read, NULL);
        sleep(1);
        err = pthread_create(&n8, NULL, thr_write, NULL);
	
	if (err != 0)
		printf("can't create thread: %s\n", strerror(err));
	pthread_join(n1,NULL);
	pthread_join(n2,NULL);
	pthread_join(n3,NULL);
	pthread_join(n4,NULL);
	pthread_join(n5,NULL);
	pthread_join(n6,NULL);
	pthread_join(n7,NULL);
	pthread_join(n8,NULL);
        
	sem_destroy(&mutex_r);
	sem_destroy(&mutex_w);
	sem_destroy(&db);
}


static void printids(const char *s)
{
	pid_t		pid;
	pthread_t	tid;

	pid = getpid();
	tid = pthread_self();
	printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid,
	  (unsigned int)tid, (unsigned int)tid);
}

static void *thr_read(void *arg)
{
        sem_wait(&mutex_r);
        rc = rc + 1;
	readP[rc-1] = waiting;
        if(rc == 1)
              sem_wait(&db);
        sleep(1);
        readP[rc-1] = in;
        //printf("rc:%d, db:%d", rc, db);
	printids("read thread: ");
	sem_post(&mutex_r);
        sleep(1);
        sem_wait(&mutex_r);
        rc = rc - 1;
 	
        if(rc == 0)
              sem_post(&db);
	//printf("rc:%d, db:%d", rc, db);
	printids("read out: ");
	readP[rc] = out;
        sem_post(&mutex_r);
	return((void *)0);
}

static void *thr_write(void *arg)
{
        sem_wait(&mutex_w);
        wc = wc + 1;
	writeP[wc-1] = waiting;
        if(wc == 1)
              sem_wait(&db);
        sleep(1);
        writeP[rc-1] = in;
        //printf("wc:%d, db:%d", wc, db);
	printids("write thread: ");
	sem_post(&mutex_w);
        sleep(1);
        sem_wait(&mutex_w);
        wc = wc - 1;
 	
        if(wc == 0)
              sem_post(&db);
	//printf("wc:%d, db:%d", wc, db);
	printids("write out: ");
	writeP[wc] = out;
        sem_post(&mutex_w);
	return((void *)0);
}

static void sig_int(int signo) 
{
	if(rc == 1 && readP[rc-1] == waiting) {
		printf("%d people are waiting for reading database!\n", rc);
		printf("%d people are writing database!\n", wc);
	}
	if(wc == 1 && writeP[rc-1] == waiting) {
		printf("%d people are waiting for writing database!\n", wc);
		printf("%d people are reading database!\n", rc);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -