Linux中多线程,同步将一个文件内容复制到另一个文件里面

时间:2021-07-18
本文章向大家介绍Linux中多线程,同步将一个文件内容复制到另一个文件里面,主要包括Linux中多线程,同步将一个文件内容复制到另一个文件里面使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void *(*func) (void *), void *arg);
int pthread_join (pthread_t tid, void ** status);

pthread_create用于创建一个线程,成功返回0,否则返回Exxx(为正数)。

  • pthread_t *tid:线程id的类型为pthread_t,通常为无符号整型,当调用pthread_create成功时,通过*tid指针返回。
  • const pthread_attr_t *attr:指定创建线程的属性,如线程优先级、初始栈大小、是否为守护进程等。可以使用NULL来使用默认值,通常情况下我们都是使用默认值。
  • void *(*func) (void *):函数指针func,指定当新的线程创建之后,将执行的函数。
  • void *arg:线程将执行的函数的参数。如果想传递多个参数,请将它们封装在一个结构体中。

pthread_join用于等待某个线程退出,成功返回0,否则返回Exxx(为正数)。

  • pthread_t tid:指定要等待的线程ID
  • void ** status:如果不为NULL,那么线程的返回值存储在status指向的空间中(这就是为什么status是二级指针的原因!这种才参数也称为“值-结果”参数)。

 

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

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define N 3 //每次读取的字符数

/*
按顺序输出我是1我是2不会乱序。
*/


sem_t rt, dt;//定义两个信号量
char buf[N] = {0};
int flag = 0;
int fd_1,fd_2;


void *fun_1(void *arg)//线程1
{
    char *p = (char *)arg;
    while(1)
    {
        //申请资源,如果没有资源,则程序阻塞休眠,如果有资源,资源数-1,程序继续执行
        sem_wait(&rt);

        //
        int red = read(fd_1,buf,sizeof(buf));
        if(-1 == red)
        {
            perror("read");
            break;
        }
        if(N > red)
        {
            flag = 1;
            sem_post(&dt);
            break;
        }
        puts(p);


        //释放资源,对应资源+1
        sem_post(&dt);
    } 
}


void *fun_2(void *arg)//线程2
{
    char *p = (char *)arg;
    while(1){
        //申请资源,如果没有资源,则程序阻塞休眠,如果有资源,资源数-1,程序继续执行
        sem_wait(&dt);

        //
        int wre = write(fd_2,buf,strlen(buf));
        if(-1 == wre)
        {
            perror("write");
            break;
        }
        if(1 == flag)
        {
            break;
        }
        puts(p);


        //释放资源,对应资源+1
        sem_post(&rt);
    }
}

int main()//main函数内为主线程,主线程在运行子线程才能正常运行。
{
    
    fd_1 = open("one.c",O_RDONLY);
    fd_2 = open("two.c",O_RDWR | O_CREAT | O_TRUNC,0777);
    if(-1 == fd_1)
    {
        perror("open one");
        return -1;
    }
    if(-1 == fd_2)
    {
        perror("open two");
        return -1;
    }



    pthread_t tid_1;
    pthread_t tid_2;
    if(0 != pthread_create(&tid_1, NULL, fun_1, "I am one!")){
        perror("pthread_create1");
        return -1;
    }
    if(0 != pthread_create(&tid_2, NULL, fun_2, "I am two!")){
        perror("pthread_create1");
        return -1;
    }


    //初始化信号量
    if(0 != sem_init(&rt, 0, 1))//给rt分配1个资源
    {
        perror("sem_init");
        return -1;
    }
    if(0 != sem_init(&dt, 0, 0))//给dt分配0个资源
    {
        perror("sem_init");
        return -1;
    }


    //回收读线程
    pthread_join(tid_1, NULL);//阻塞直到tid_1线程结束
    //回收写线程
    pthread_join(tid_2, NULL);//阻塞直到tid_2线程结束
    printf("复制完成!");
    close(fd_1);
    close(fd_2);

}

原文地址:https://www.cnblogs.com/JinShanCheShen/p/15026064.html