6.13 线程作业

1.线程拷贝

 #include<stdio.h>
 #include<pthread.h>
 #include<unistd.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include<sys/wait.h>
 #include<sys/types.h>
 
 int fp1,fp2;
 off_t size;
 pthread_mutex_t mutex;
 
 void*qian(void*arg){
     pthread_mutex_lock(&mutex);
     lseek(fp1,0,SEEK_SET);
     lseek(fp2,0,SEEK_SET);
 
     char c;
     for(int i=0;i<size/2;i++){
         read(fp1,&c,1);
         write(fp2,&c,1);
     }                                                
     printf("前半段拷贝完毕\n");
     pthread_mutex_unlock(&mutex);
     pthread_exit(NULL);
 
 }
 void*huo(void*vrg){
     pthread_mutex_lock(&mutex);
     lseek(fp1,size/2,SEEK_SET);
     lseek(fp2,size/2,SEEK_SET);
     char c;
     for(int i=size/2;i<size;i++){
         read(fp1,&c,1);
         write(fp2,&c,1);
     }
     printf("后半段拷贝完毕\n");
     pthread_mutex_unlock(&mutex);
     pthread_exit(NULL);
 }
                                                           
int main(int argc, const char *argv[])
{

    pthread_mutex_init(&mutex,NULL);
    fp1=open("./saber.png",O_RDONLY);
    if(fp1<0){
        perror("open");
        return -1;
    }

    fp2=open("./fsaber.png",O_RDWR|O_CREAT|O_TRUNC,0777);
    if(fp2<0){
        perror("open");
        return -1;
    }
    size=lseek(fp1,0,SEEK_END);

        pthread_t tid;
        pthread_create(&tid,NULL,qian,NULL);
        pthread_t tod;
        pthread_create(&tod,NULL,huo,NULL);

        pthread_join(tid,NULL);
        pthread_join(tod,NULL);
        pthread_mutex_destroy(&mutex);
        close(fp1);
        close(fp2);
    return 0;
}

                


实现

2.三个线程打印ABC

#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<string.h>

pthread_mutex_t mutex;
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*aaa(void*arg){
    while(1){
        //临界区
        //上锁
        pthread_mutex_lock(&mutex);
        if(flag!=0){
            pthread_cond_wait(&cond1,&mutex);
        }
        printf("A");
        flag=1;
        //解锁
        pthread_cond_signal(&cond2);
        pthread_mutex_unlock(&mutex);
    }                                                    
    pthread_exit(NULL);
}

void*bbb(void*arg){
    while(1){
        pthread_mutex_lock(&mutex);
        if(flag!=1){
            pthread_cond_wait(&cond2,&mutex);
        }
        printf("B");
        flag=2;
        pthread_cond_signal(&cond3);
         pthread_mutex_unlock(&mutex);
     }   
     pthread_exit(NULL);
 }
 void*ccc(void*arg){                                 
     while(1){
         //临界区
         //上锁
         pthread_mutex_lock(&mutex);
         if(flag!=2){
             pthread_cond_wait(&cond3,&mutex);
         }
         printf("C\n");
         flag=0;
         pthread_cond_signal(&cond1);
         //解锁
         pthread_mutex_unlock(&mutex);
     }
     pthread_exit(NULL);
 }
 
 int main(int argc, const char *argv[])
 {
 //  pthread_mutex_t mutex;
     pthread_mutex_init(&mutex,NULL);
 
     pthread_t tid1,tid2,tid3;
     pthread_create(&tid1,NULL,aaa,NULL);
     pthread_create(&tid2,NULL,bbb,NULL);
     pthread_create(&tid3,NULL,ccc,NULL);
 
     pthread_join(tid1,NULL);
     pthread_mutex_destroy(&mutex);
     pthread_cond_destroy(&cond1);
     pthread_cond_destroy(&cond2);
     pthread_cond_destroy(&cond3);
    return 0;
}
                   
                   
                   

实现

3.管道实现

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<string.h>

int main(int argc, const char *argv[])
{
    int pfd[2];                                                        
    if(pipe(pfd)<0){
        perror("pipe");
        return -1;
    }
    char buf[128]="";
    pid_t pid=fork();
    if(pid>0){
        while(1){
            fgets(buf,sizeof(buf),stdin);
            buf[strlen(buf)-1]='\0';
            if((write(pfd[1],buf,sizeof(buf)))<0){
                perror("write");
                return -1;
            }
            printf("写入成功:%s __%d__\n",buf,__LINE__);
        }
    }
    else if(pid==0){
        ssize_t res;
        while(1){
            res=read(pfd[0],buf,sizeof(buf));
            if(res<0){
                perror("read");
                return -1;
            }
            printf("res=%ld %s__%d__\n",res,buf,__LINE__);
        }
    }
    else{
        perror("fork");
         return -1;
     }
 
     return 0;
 }
 
                        
                        

实现

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值