3/2作业

1.将一个文件中的数据打印到终端上,类似cat一个文件。要求如下

   a.A线程读取文件中的数据

   b.B线程将A线程读取到的数据打印到终端上

   c.C文件打印完毕后,结束进程

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
sem_t sem1;
sem_t sem2;
ssize_t res=0;
char str[20]="";
void* readCallback(void* arg)
{
	int fd=open("./01_cond.c",O_RDONLY);
	if(fd<0)
	{
		perror("open");
		pthread_exit(NULL);
	}
	while(1)
	{
		sem_wait(&sem1);
		bzero(str,sizeof(str));
		res=read(fd,str,sizeof(str));
		if(0==res)
		{
			sem_post(&sem2);
			pthread_exit(NULL);
		}
		if(res<0)
		{
			perror("read");
			pthread_exit(NULL);
		}
		sem_post(&sem2);
	}
	pthread_exit(NULL);
}
void* writeCallback(void* arg)
{
	while(1)
	{
		sem_wait(&sem2);
		if(write(1,str,res)<=0)
		{
			break;
		}
		sem_post(&sem1);
	}
	pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
	if(sem_init(&sem1,0,1)!=0)
	{
		perror("sem_init");
		return -1;
	}	
	if(sem_init(&sem2,0,1)!=0)
	{
		perror("sem_init");
		return -1;
	}
	pthread_t tid1,tid2;
	if(pthread_create(&tid1,NULL,readCallback,NULL)!=0)
	{
		perror("pthread_create");
		return -1;
	}	
	if(pthread_create(&tid1,NULL,writeCallback,NULL)!=0)
	{
		perror("pthread_create");
		return -1;
	}
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	sem_destroy(&sem1);
	sem_destroy(&sem2);
	return 0;
}

 

2.用条件变量实现,有编号为ABC的三个线程,线程内分别打印自己的线程编号,要求打印的顺序为ABC

#include<stdio.h>
#include <pthread.h>
 
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond3 = PTHREAD_COND_INITIALIZER;
int flag = 0;
 
void* callBackA(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if(flag != 0)
        {
            pthread_cond_wait(&cond1, &mutex);
        }
        printf("A");
        flag = 2;
        pthread_cond_signal(&cond2);
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}
 
void* callBackB(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if(flag != 1)
        {
            pthread_cond_wait(&cond2, &mutex);
        }
        printf("B");
        flag = 0;
        pthread_cond_signal(&cond3);
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}
 
void* callBackC(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if(flag != 2)
        {
            pthread_cond_wait(&cond3, &mutex);
        }    
        printf("C");
        flag = 0;
        pthread_cond_signal(&cond1);
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	pthread_t tid,tid1,tid2;
	if(pthread_create(&tid, NULL, callBackA, NULL) != 0)
	{
		fprintf(stderr, "pthread_create failed\n");
		return -1;
	}
	if(pthread_create(&tid1, NULL, callBackB, NULL) != 0)
	{
		fprintf(stderr, "pthread_create failed\n");
		return -1;
	}
	if(pthread_create(&tid2, NULL, callBackC, NULL) != 0)
	{
		fprintf(stderr, "pthread_create failed\n");
		return -1;
	}
	pthread_join(tid, NULL);
	pthread_join(tid1, NULL);
	pthread_join(tid2, NULL);

	pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond1);
    pthread_cond_destroy(&cond2);
    pthread_cond_destroy(&cond3);
    return 0;
}

3.要求用信号量的方式实现,打印一次倒置一次。不允许使用flag

#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
char str[]="1234567";
sem_t sem1;
sem_t sem2;
void* callBack_print(void* arg)
{
	while(1)
	{
		sem_wait(&sem1);
		printf("%s\n", str);
		sem_post(&sem2);
	}
	pthread_exit(NULL);
}
void* callBack_swap(void* arg)
{
	int len = strlen(str);
	char temp;
	int i = 0;
	while(1)
	{
		sem_wait(&sem2);
		for(i=0; i<len/2; i++)
		{
			temp = str[i];
			str[i] = str[len-1-i];
			str[len-1-i] = temp;
		}
		sem_post(&sem1);
	}
	pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
	if(sem_init(&sem1,0,1)<0)
	{
		perror("sem_init");
		return -1;
	}	
	if(sem_init(&sem2,0,1)<0)
	{
		perror("sem_init");
		return -1;
	}
	pthread_t tid1,tid2;
	if(pthread_create(&tid1,NULL,callBack_print,NULL)!=0)
	{
		return -1;
	}
	if(pthread_create(&tid2,NULL,callBack_swap,NULL)!=0)
	{
		return -1;
	}
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	sem_destroy(&sem1);
	sem_destroy(&sem2);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/k_weihgl/article/details/129330188