240401 6.13 作业

1、用线程完成图片拷贝,要求一个线程拷贝一半,另一个线程拷贝另一半。

 #include <stdio.h>                                             
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <pthread.h>
 
 void* copy1(void* arg)  //拷贝前半部分
 {
 
     //以读的方式打开1.jpg文件
     int fd = open("1.jpg",O_RDONLY);
     if(fd < 0)
     {
         perror("open");
         return NULL;
     }
     printf("文件打开成功\n");
 
     //以写的方式打开文件2.jpg,通过两个线程写进去
     int fd1 = open("2.jpg",O_WRONLY|O_CREAT|O_TRUNC,0777);
     if(fd1 <0)
     {
         perror("open");
         return NULL;
     }
 
     off_t size = lseek(fd,0,SEEK_END);
 
     pthread_mutex_lock((pthread_mutex_t*)arg);
 
     lseek(fd,0,SEEK_SET);
     lseek(fd1,0,SEEK_SET);
     char c;
     for(int i=0;i<size/2;i++)
     {
         read(fd,&c,1);
         write(fd1,&c,1);
     }
     printf("前半部分拷贝完毕\n");
 
     pthread_mutex_unlock((pthread_mutex_t*)arg);
 
     pthread_exit(NULL);
 }
 
 void* copy2(void* arg)  //拷贝后半部分
 {
     //以读的方式打开1.jpg文件
     int fd = open("1.jpg",O_RDONLY);
     if(fd < 0)
     {
         perror("open");
         return NULL;
     }
     printf("文件打开成功\n");
 
     //以写的方式打开文件2.jpg,通过两个线程写进去
     int fd1 = open("2.jpg",O_WRONLY,0777);
     if(fd1 <0)
     {
         perror("open");
         return NULL;
     }
     off_t size = lseek(fd,0,SEEK_END);
 
     pthread_mutex_lock((pthread_mutex_t*)arg);//锁
 
     lseek(fd,size/2,SEEK_SET);
     lseek(fd1,size/2,SEEK_SET);
     char c;
     for(int i=0;i<size/2;i++)
     {
         read(fd,&c,1);
         write(fd1,&c,1);
     }
 
     pthread_mutex_unlock((pthread_mutex_t*)arg);
 
     printf("后半部分拷贝完毕\n");
 
     pthread_exit(NULL);
 }
 
 int main(int argc, const char *argv[])
 {
     //创建互斥锁
     pthread_mutex_t mutex;
     pthread_mutex_init(&mutex,NULL);
 
 
     //创建两个线程
     pthread_t pid1;
     if(pthread_create(&pid1,NULL,copy1,(void*)&mutex) !=0)
     {
         perror("pthread_create");
         return -1;
     }
     pthread_join(pid1,NULL);
 
     pthread_t pid2;
     if(pthread_create(&pid2,NULL,copy2,(void*)&mutex) !=0)
     {
         perror("pthread_create");
         return -1;
     }
 
     pthread_join(pid1,NULL);
     pthread_mutex_destroy(&mutex);
 
 
     return 0;
 }

运行结果:

2、创建编号为ABC三个线程,三个线程循环打印自己的编号,要求打印出来的结果必须是ABC;

 #include <stdio.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 //#include <fcutl.h>
 #include <stdio.h>
 #include <pthread.h>
 
 //创建互斥锁
 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 
 int flag = 0;
 
 void* A(void* arg)
 {
     if(flag == 0)
     {
 
 
             //上锁
             pthread_mutex_lock(&mutex);
 
             printf("A\n");
 
             //解锁
             pthread_mutex_unlock(&mutex);
 
             flag = 1;
     }
 }
 
 void* B(void* arg)
 {
     if(flag == 1)
     {
         //上锁
         pthread_mutex_lock(&mutex);
 
         printf("B\n");
 
         //解锁
         pthread_mutex_unlock(&mutex);
 
         flag = 2;
     }
 }
 
 void* C(void* arg)
 {
     if(flag == 2)
     {
         //上锁
         pthread_mutex_lock(&mutex);
 
         printf("C\n");
 
         //解锁
         pthread_mutex_unlock(&mutex);
 
         flag = 0;
     }
 }
 int main(int argc, const char *argv[])
 {
     //创建线程
     pthread_t tid1,tid2,tid3;
     if(pthread_create(&tid1,NULL,A,NULL) !=0)
     {
         printf("pthread_create");
         return -1;
     }
     sleep(1);
 
     if(pthread_create(&tid2,NULL,B,NULL) !=0)
     {
         printf("pthread_create");
         return -1;
     }
     sleep(2);
 
     if(pthread_create(&tid3,NULL,C,NULL) !=0)
     {
         printf("pthread_create");
         return -1;
     }
 
 //  pthread_join(tid3,NULL);
     sleep(5);
 
     return 0;
 }
                                                                       

3、pipe管道

#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;
    }
    printf("创建管道成功 %d %d\n",pfd[0],pfd[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(0 == pid)
    {
        //子进程运行 --->子进程读取
        ssize_t res;
        while(1)
        {
            //若没有数据,则read函数阻塞
            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;
}

运行结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值