读写锁练习题

练习:设计一个程序,程序中有3个线程,主线程A创建一个文本,每隔5s获取一次系统时间并写入到该文本中,另外两个线程B和C分别从文本中读取当前的时间和日期,子线程B输出系统时间"hh:mm:ss",子线程c输出系统日期"2024年05月31日”,要求使用读写锁实现互斥。 提示:主线程A获取写操作的锁,另外的线程分别获取读操作的锁。

/***************************************************************************************
 *
 *	file name:  pthread_rwlock.c
 *	author	 :  Dazz
 *	date	 :  2024/05/31
 *	function :  练习:设计一个程序,程序中有3个线程,主线程A创建一个文本,每隔5s获取一次系统时
 *              间并写入到该文本中,另外两个线程B和C分别从文本中读取当前的时间和日期,子线程B输
 *              出系统时间"hh:mm:ss",子线程c输出系统日期"2024年05月31日”,要求使用读写锁实现
 *              互斥。 提示:主线程A获取写操作的锁,另外的线程分别获取读操作的锁。
 *
 * 	note	 :  link with -pthread.
 *
 *	CopyRight (c)  2024  Dazz_24@163.com   All Right Reseverd
 *
 * ***************************************************************************************/

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>

// 定义读写锁
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;

// 定义文件流指针
FILE *txt = NULL;

// 线程B用来输出系统时间"hh:mm:ss"
void *funcB(void *)
{
    // 读缓存区
    char buf[30] = "0";
    char *p;
    while (1)
    {
        // 上锁
        pthread_rwlock_rdlock(&rwlock);

        // 读取缓存区里的系统时间
        fread(buf, sizeof(buf), 1, txt);

        // 文件光标移到开头
        fseek(txt, 0, SEEK_SET);

        // 解锁
        pthread_rwlock_unlock(&rwlock);

        // 用strstr处理字符串
        p = strstr(buf, ",");
        p = strtok(buf, ",");
        p = strtok(NULL, ",");

        printf("this is funcB,this is what I read:%s\n", p);

        sleep(2);
    }
}

//
// 线程C用来输出系统时间"2024年05月31日”
void *funcC(void *)
{
    // 读缓存区
    char buf[30] = "0";
    char *p;
    while (1)
    {
        // 上锁
        pthread_rwlock_rdlock(&rwlock);

        // 读取缓存区里的系统时间
        fread(buf, sizeof(buf), 1, txt);

        // 文件光标移到开头
        fseek(txt, 0, SEEK_SET);

        // 解锁
        pthread_rwlock_unlock(&rwlock);

        // 用strtok处理字符串
        p = strtok(buf, ",");

        printf("this is funcC,this is what I read:%s\n", p);

        sleep(2);
    }
}

int main()
{
    // 创建一个文本
    txt = fopen("./txt.log", "wb+");
    if (NULL == txt)
    {
        fprintf(stderr, "fopen fail!,error: %d, %s\n", errno, strerror(errno));
        exit(1);
    }

    // 初始化读写锁
    pthread_rwlock_init(&rwlock, NULL);

    // 创建线程B和C
    pthread_t threadB;
    pthread_t threadC;

    // 执行线程B和C
    pthread_create(&threadB, NULL, funcB, NULL);
    pthread_create(&threadC, NULL, funcC, NULL);

    // 定义一个时间变量
    time_t timep;
    struct tm *timerow;

    while (1)
    {
        // 获取当前当前的时间戳
        timep = time(NULL);

        // 将时间戳的地址作为参数传递给函数localtime
        timerow = localtime(&timep);

        // 上锁
        pthread_rwlock_wrlock(&rwlock);

        // 将读取到的系统时间写入txt.log文本中
        fprintf(txt, "%d年%d月%d日,%d:%d:%d",
                timerow->tm_year + 1900,
                timerow->tm_mon + 1,
                timerow->tm_mday,
                timerow->tm_hour,
                timerow->tm_min,
                timerow->tm_sec);

        // 将光标重新指向文件头
        fseek(txt, 0, SEEK_SET);

        // 解锁
        pthread_rwlock_unlock(&rwlock);
    }

    return 0;
}

posted @ 2024-05-31 12:03  Dazz_24  阅读(27)  评论(0)    收藏  举报