IO day4

2023-12-18 06:02:06

1、用两个线程复印图片,一个线程拷贝一半?


//上半部分
void* pthreadA(void* arg){ //void* arg=&mutex
	
	//上锁
	pthread_mutex_lock(&mutex);
 
	lseek(pid_r,0,SEEK_SET);
	lseek(pid_w,0,SEEK_SET);
 
	
	char str;
	for(int i=0;i<statbuf.st_size/2;i++){
		read(pid_r,&str,sizeof(str));
		write(pid_w,&str,sizeof(str));
	}
 
	//解锁
	pthread_mutex_unlock(&mutex);
 
	pthread_exit(NULL);
}
 
 

void* pthreadB(void* arg){ //void* arg=&mutex
 
	//上锁
	pthread_mutex_lock(&mutex);
 
	
	char str;
	lseek(pid_r,statbuf.st_size/2,SEEK_SET);
	lseek(pid_w,statbuf.st_size/2,SEEK_SET);
 
	
	for(int i=statbuf.st_size/2;i<statbuf.st_size;i++)
	{
		read(pid_r,&str,sizeof(str));
		write(pid_w,&str,sizeof(str));
	}
 
	//解锁
	pthread_mutex_unlock(&mutex);
 
	pthread_exit(NULL);
}
 
int main(int argc, const char *argv[])
{
	pthread_t tid1,tid2;
 
	
	if((pid_r=open("./1.jpg",O_RDONLY))<0){
		perror("open");
		pthread_exit(NULL);
	}
	if((pid_w=open("./2.jpg",O_WRONLY|O_CREAT|O_TRUNC,0664))<0){
		perror("open");
		pthread_exit(NULL);
	}
	
	
	stat("./1.jpg",&statbuf);
 
	//创建锁
	pthread_mutex_init(&mutex,NULL);
	//创建线程
	if(pthread_create(&tid1,NULL,pthreadA,NULL)!=0){
		printf("create pthreadA file\n");
		return -1;
	}
 
	if(pthread_create(&tid2,NULL,pthreadB,NULL)!=0){
		printf("create pthreadB file\n");
		return -1;
	}
 
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);

	pthread_mutex_destroy(&mutex);

	close(pid_r);
	close(pid_w);
 
	return 0;

}

?

?

?2、创建两个线程,要求一个线程从文件中读取数据,另一个线程将读取到的数据打印到终端,类似cat一个文件文件cat完毕后,要结束进程
a.读到一次数据,打印一次数据

#include <stdio.h>
#include "/home/ubuntu/head.h"
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
char buf;

sem_t sem;
sem_t sem2;
void* callBack(void* arg)
{
    int res = 0;
    while(1)
    {
        //p操作
        if(sem_wait(&sem)<0)
        {
            perror("sem_wait");
            pthread_exit(NULL);
        }

        
        sem_wait(&sem);
        res = fscanf(fd, "%c", &buf);
        sem_post(&sem2);
        if(EOF == res)
        {
            break;

        }

        //v操作
        if(sem_post(&sem2)<0)
        {
            perror("sem_post");
            pthread_exit(NULL);
        }

    }

    pthread_exit(NULL);

}

void* callBack2(void* arg)
{
    int res = 0;
    while(1)
    {
        //p操作
        if(sem_wait(&sem2)<0)
        {
            perror("sem_wait");
            pthread_exit(NULL);
        }

        sem_wait(&sem2);
        res = fprintf(stdout, "%c", buf);
        sem_post(&sem1);

    
        //v操作
        if(sem_post(&sem)<0)
        {
            perror("sem_post");
            pthread_exit(NULL);

        }

    }

    pthread_exit(NULL);
}



int main(int argc, const char *argv[])
{
    
    fd = fopen("./lx.c", "r");
    if(fd == NULL)
    {
        perror("fd");
        return -1;
    } 
    
    //信号灯
     if(sem_init(&sem,0,1)<0)
    {
        perror("sem_init");
        return -1;
    }

    if(sem_init(&sem2,0,0)<0)
    {
        perror("sem_init");
        return -1;
    }

    //创建A线程
    pthread_t Apid;
    if(pthread_create(&Apid,NULL,callBack,NULL)!=0)
    {
        fprintf(stderr,"pthread_create _%d_\n", __LINE__);
        return -1;
    }

    //创建B线程
    pthread_t Bpid;
    if(pthread_create(&Bpid,NULL,callBack2,NULL)!=0)
    {
        fprintf(stderr,"pthread_create _%d_\n",__LINE__);
        return -1;
    }

   

    pthread_join(Apid,NULL);
    pthread_join(Bpid,NULL);

    sem_destroy(&sem);
    sem_destroy(&sem2);
    return 0;
}                                                             

文章来源:https://blog.csdn.net/hjj123adc/article/details/135052234
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。