线程有规律循环打印输出,线程拷贝图片运用

1:线程打印循环(保证循环顺序输出)

使用互斥锁和条件变量实现:

 #include <stdio.h>
 #include <pthread.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
 #include <semaphore.h>
 
 pthread_t tid1,tid2,tid3;//创建三个线程
 pthread_cond_t cond1,cond2,cond3;//创建三个条件变量
 pthread_mutex_t mutex1;//创建三个互斥锁
 int flag = 0;
 int len =0;
 void* callBack1(void* arg)//子线程的执行体
 {
     while(1)
     {
         pthread_mutex_lock(&mutex1);//上锁
     if(flag != 0)
     {
         pthread_cond_wait(&cond1,&mutex1);
     }
     printf("A ");
     flag = 1;
 
     pthread_cond_signal(&cond2);
     pthread_mutex_unlock(&mutex1);//解锁
     }
     return NULL;
 }
 void* callBack2(void* arg)
 {
     while(1)
     {
         pthread_mutex_lock(&mutex1);//上锁
     if(flag != 1)
     {
         pthread_cond_wait(&cond2,&mutex1);
     }
     printf("B ");
     flag = 2;
 
     pthread_cond_signal(&cond3);
     pthread_mutex_unlock(&mutex1);//解锁   
     }
     return NULL;
 
 }
 void* callBack3(void* arg)
 {
     while(1)
     {
         pthread_mutex_lock(&mutex1);//上锁
     if(flag != 2)
     {
         pthread_cond_wait(&cond3,&mutex1);
     }
     printf("C ");
     flag = 0;
 
     pthread_cond_signal(&cond1);
     pthread_mutex_unlock(&mutex1);//解锁  
     }
     return NULL;
 }
 
 
 int main(int argc, const char *argv[])
 {
     pthread_mutex_init(&mutex1,NULL);//创建一个互斥锁
 
     pthread_cond_init(&cond1,NULL);//创建条件变量
     pthread_cond_init(&cond2,NULL);
     pthread_cond_init(&cond3,NULL);
 
     if(pthread_create(&tid1,NULL,callBack1,&mutex1)!=0)
     {
         perror("pthread_creadte");
         return -1;
     }
     printf("线程1创建成功\n");
     if(pthread_create(&tid2,NULL,callBack2,&mutex1)!=0)
     {
         perror("pthread_creat");\
             return -1;
     }
     printf("线程2创建成功\n");
     if(pthread_create(&tid3,NULL,callBack3,&mutex1)!=0)
     {
         perror("pthread_creat");
             return -1;
     }
     printf("线程3创建成功\n");
 
 
     pthread_join(tid1,NULL);
 
     pthread_mutex_destroy(&mutex1);//销毁互斥锁
 
     return 0;
 }                                                                 
                                                                   
                                                                   

结果图:

2:线程分开拷贝图片

#include <stdio.h>
  2 #include <pthread.h>
  3 #include <sys/types.h>
  4 #include <unistd.h>
  5 #include <sys/stat.h>
  6 #include <semaphore.h>
  7 #include <fcntl.h>
  8 #include <errno.h>
  9 pthread_t tid,tid1;
 10 sem_t sem;
 11 off_t len;
 12 void* one_pthread(void* arg)
 13 {
 14     printf("前部分\n");
 15     sem_wait(&sem);
 16 
 17     int fp = open("./photo.jpg",O_RDONLY);
 18     if(fp < 0)
 19     {
 20         perror("open");
 21     }
 22     int fp1 = open("copy.jpg",O_WRONLY);
 23     if(fp1 <0 )
 24     {
 25         perror("open");
 26     }
 27     off_t len = lseek(fp,0,SEEK_END);//计算图片总大小并返回字节数
 28     char n;
 29     lseek(fp,0,SEEK_SET);
 30     lseek(fp1,0,SEEK_SET);
 31     for(int i=0;i<len/2;i++)
 32     {
 33         read(fp,&n,1);
 34         write(fp1,&n,1);
 35     }
 36     printf("前半部分拷贝完成\n");
 37 
 38     sem_post(&sem);
 39     pthread_exit(NULL);
 40 }
 41 
 42 void* two_pthread(void* arg)
 43 {
 44     sleep(1);
 45     printf("后半部分\n");
 46     sem_wait(&sem);
 47     //打开拷贝的图片
 48         int fp = open("./photo.jpg",O_RDONLY);
 49     if(fp < 0)
 50     {
 51         perror("open");
 52     }
 53     //打开拷贝到的文件
 54     int fp1 = open("copy.jpg",O_WRONLY);
 55     if(fp1 < 0)
 56     {
 57         perror("open");
 58     }
 59     len = lseek(fp,0,SEEK_END);//计算图片总大小并返回字
 60     char n;
 61     lseek(fp,len/2,SEEK_SET);
 62     lseek(fp1,len/2,SEEK_SET);
 63     //进行拷贝                                                                                             
 64     for(int i=len/2;i<len;i++)
 65     {
 66         read(fp,&n,1);
 67         write(fp1,&n,1);
 68     }
 69     printf("后半部分拷贝完成\n");
 70     sem_post(&sem);
 71     pthread_exit(NULL);
 72 }
 73 int main(int argc, const char *argv[])
 74 {
 75     //打开一个用于拷贝的空文件
 76     int fp = open("copy.jpg",O_WRONLY|O_CREAT|O_TRUNC,0665);
 77     if(fp <0)
 78     {
 79         perror("openn");
 80         return -1;
 81     }
 82     //创建一个信号灯
 83     sem_init(&sem,0,1);
 84 
 85     if(pthread_create(&tid,NULL,one_pthread,NULL) !=0)
 86     {
 87         perror("pthread_create");
 88         return -1;
 89     }
 90     if(pthread_create(&tid1,NULL,two_pthread,NULL) !=0)
 91     {
 92         perror("pthread_create");
 93         return -1;
 94     }
 95 
 96     pthread_join(tid,NULL);//阻塞线程,防止直接退出
 97     pthread_join(tid1,NULL);
 98     sem_destroy(&sem);
 99     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;
    }
    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(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;
}   

结果图

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值