Linux多线程实现文件读写操作—copy

一、思路:

  1. 多线程是在一个进程中有多个线程在运行,为了实现问价读写操作,这里需要定义两个子线程,一个子线程进行读取操作,另外一个进行写入操作。

pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg) //创建线程
/*参数:
    thread: &tid  线程号的地址
    attr: NULL 线程函数默认的属性  
    start_routine:  线程函数的函数名  
    arg: 给线程函数传递的参数 NULL

返回值:
    成功: 0
    失败: -1 */

int pthread_join(pthread_t thread, void **retval)//回收线程资源
/*参数:
    thread: tid
返回值:
    成功:0
    失败: -1 */
  1. 定义一个buf用于接受读取的数据,在将buf中的数据写入待写入的文件中。

  1. 创建两个线程函数fun1,fun2;

fun1里面使用文件IO函数open,打开一个需要读取其内容的文件,赋予可读权限。使用read函数读取内容到buf中。

fun2里面使用文件IO函数open,创建一个需要写入内容的文件,赋予读写权限,文件不存在则创建该文件。使用write函数从buf中写入内容到文件中。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//打开文件,文件已经存在
int open(const char *pathname, int flags);
//创建文件,文件不存在
int open(const char *pathname, int flags, mode_t mode);
/*参数:
    pathname: 文件的路径名
    flags: 打开方式 O_RDONLY:只读
                   O_WRONLY:可写
                   O_RDWR:读写
                   O_APPEND:追加  
                   O_TRUNC:清零  
                   O_CREAT:创建  
    mode: 文件的权限 0666
返回指:    
    成功:文件描述符fd
    失败:-1 */


#include <unistd.h>
int close(int fd);//关闭打开的文件


#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);//读取内容到buf
/*参数:
      fd: 文件描述符
      buf: 内存地址
      count: 读取的字节数 100个字节
返回值:
    成功: 
        >0  返回实际读取到的字节数
        == 0 读到文件的末尾
    失败:
         <0 读取失败*/

#include <unistd.h>
ssize_t  write(int  fd,  const void *buf, size_t count);//从buf中写入数据到文件
/*参数:
      fd: 文件描述符
      buf: 内存地址
      count: 写入的字节数 100个字节
返回值:
    成功:返回实际写入的字节数
    失败:-1 */
  1. 线程都在同时运行,由于读写有先后顺序,先读取数据,在写入数据,因此需要线程的同步或者互斥,才能保证读写的正常进行。(这里我使用的是同步)

#include <semaphore.h>
//线程的同步
sem_t sem; //信号量的变量

int sem_init(sem_t *sem, int pshared, unsigned int value);//初始化信号量
/*参数:
    sem: &sem
    pshared: 0:线程    1:进程
    value: 初值 0 or 1
返回值:
    成功:0
    失败:-1 */
int sem_wait(sem_t *sem);  //-1
//如果初值为0,会阻塞

int sem_post(sem_t *sem);  //+1
#include <pthread.h>
//线程的互斥
pthread_mutex_t mutex; //互斥锁的变量

int pthread_mutex_init(pthread_mutex_t   *mutex, const pthread_mutexattr_t *mutexattr);//初始化互斥锁
/*参数:
     mutex: &mutex
     mutexattr:  NULL (默认属性)
返回值:
    成功:0
    失败:-1 */
      
int pthread_mutex_lock(pthread_mutex_t *mutex);//加锁

int pthread_mutex_unlock(pthread_mutex_t *mutex);//解锁

二、代码

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


pthread_t tid1,tid2; //线程号
pthread_mutex_t mutex; //互斥锁
sem_t sem1,sem2; //信号量

char buf[20] = {0}; //接受读取文件的内容

int count = 0; //记录读取的字节数
int n = 0; //记录每次读取的字节数


/*读取子线程函数*/
void *fun1(void *arg)
{
    /*以可读方式打开1.txt*/

    int fd1 = open("./1.txt",O_RDONLY,0666);
    if(fd1 < 0)
    {
        perror("open");
        return (void *)-1;
    }

    while(1)
    {
        sem_wait(&sem2); // -1
        //pthread_mutex_lock(&mutex); //加锁

        printf("reading......!\n");
        sleep(1); //延迟1s

        n = read(fd1,buf,sizeof(buf)); //从1.txt中读取字符串到buf中

        if(n == 0) //读到末尾结束循环
        {
            printf("read over!\n");
            close(fd1);
            //break;  /*使用同步时,break后,write会阻塞,导致不能打印write over*/
                    /*使用互斥是需要break,结束循环*/
        }
        
        //pthread_mutex_unlock(&mutex); //解锁
        sem_post(&sem1); //+1
    }

}

/*写入子线程函数*/
void *fun2(void *arg)
{
    /*以读写方式打开2.txt,如果文件不存在则创建2.txt*/
    
    int fd2 = open("./2.txt",O_RDWR | O_CREAT,0666);
    if(fd2 < 0)
    {
        perror("open");
        return (void *)-1;
    }
    while(1)
    {
        sem_wait(&sem1); //-1 阻塞

        printf("writing......!\n");
        sleep(1);

        int m =write(fd2,buf,n); // 从buf中写入字符串到fd2
        if(0 == m) //写入的字节数为0时结束
        {
            printf("write over!\n");
            close(fd2);
            break;
        }
        count += m; //记录写入的字节数
        sem_post(&sem2); //+1   
    }
    printf("write byte:%d\n",count); //打印最终读取字节数

}

int main(int argc, char *argv[])
{
    //pthread_mutex_init(&mutex,NULL); //初始化互斥锁

    sem_init(&sem1,0,0); //初始化信号量
    sem_init(&sem2,0,1);

    /*创建子线程*/

    /*fun1,读取数据的子线程*/
    int ret = pthread_create(&tid1,NULL,fun1,NULL);
    if(ret == -1)
    {
        perror("pthread_create");
        return -1;
    }
    pthread_detach(tid1);

    /*fun2,写入数据的子线程*/
    ret = pthread_create(&tid2,NULL,fun2,NULL);
    if(ret == -1)
    {
        perror("pthread_create");
        return -1;
    }
    pthread_detach(tid2);

    while(1);

    return 0;
}

三、运行结果

  1. 1.txt为需要读取的文件

  1. 2.txt为写入数据前的文件(内容为空)

  1. 运行中(先读取,在写入)

  1. 读取完,写入完后的2.txt


四、完结

谢谢大家浏览!感谢!😁

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个基于多线程的串口读写操作的Demo,该程序启动了两个线程,一个线程用于读取串口数据,另一个线程用于将读取到的数据写入文件中。 ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <fcntl.h> #include <termios.h> #include <unistd.h> #define MAX_BUF_SIZE 1024 char buf[MAX_BUF_SIZE]; int buf_pos = 0; void *read_thread(void *arg) { int fd = *(int *)arg; while (1) { char ch; int n = read(fd, &ch, 1); if (n > 0) { if (ch == '\n') { buf[buf_pos++] = '\0'; buf_pos = 0; } else { buf[buf_pos++] = ch; } } } } void *write_thread(void *arg) { int fd = *(int *)arg; FILE *fp = fopen("output.txt", "w"); while (1) { if (buf_pos > 0) { fprintf(fp, "%s\n", buf); fflush(fp); buf_pos = 0; } } fclose(fp); } int main() { int fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY); struct termios options; tcgetattr(fd, &options); cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); options.c_cflag |= (CLOCAL | CREAD); options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; tcsetattr(fd, TCSANOW, &options); pthread_t t1, t2; pthread_create(&t1, NULL, read_thread, &fd); pthread_create(&t2, NULL, write_thread, &fd); pthread_join(t1, NULL); pthread_join(t2, NULL); close(fd); return 0; } ``` 以上程序启动了两个线程,一个线程用于读取串口数据,另一个线程用于将读取到的数据写入文件中。其中,read_thread函数中使用read函数读取串口数据,并将读取到的数据存储在buf数组中;write_thread函数中则将buf数组中的数据写入到文件output.txt中。在主线程中,首先使用open函数打开串口设备文件,然后使用tcgetattr和tcsetattr函数配置串口参数,接着使用pthread_create函数创建两个线程,使用pthread_join函数等待两个线程的结束。注意,在使用pthread_create函数创建线程时,需要指定线程函数的类型为void *(*)(void *),表示函数返回值为void *,参数为void *。在每个线程中使用的文件描述符都是主线程中打开的串口设备文件描述符。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值