创建两个线程,其中一个线程读取文件中的数据,另外一个线程将读取到的内容打印到终端上,类似实现cat一个文件。 cat数据完毕后,要结束两个线程。

#include <stdio.h>
#include <pthread.h>

#define BUFFER_SIZE 99999   //足够大

// 全局共享的数据缓冲区
char buffer[BUFFER_SIZE];
int buffer_length = 0;

// 锁和条件变量用于线程同步
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

// 读取文件的线程函数
void *readFileThread(void *filename) {
    // 打开文件
    FILE *file = fopen((char *)filename, "r");
    if (file == NULL) {
        printf("无法打开文件\n");
        pthread_exit(NULL);
    }

    // 读取文件数据到缓冲区
    while (!feof(file)) {
        // 加锁
        pthread_mutex_lock(&mutex);

        // 等待打印线程打印完毕
        while (buffer_length != 0) {
            pthread_cond_wait(&cond, &mutex);
        }

        // 读取数据到缓冲区
        buffer_length = fread(buffer, sizeof(char), BUFFER_SIZE, file);

        // 通知打印线程打印数据
        pthread_cond_signal(&cond);

        // 解锁
        pthread_mutex_unlock(&mutex);
    }

    // 关闭文件
    fclose(file);
    pthread_exit(NULL);
}

// 打印数据的线程函数
void *printDataThread(void *arg) {
    while (1) {
        // 加锁
        pthread_mutex_lock(&mutex);

        // 等待读取线程读取数据
        while (buffer_length == 0) {
            pthread_cond_wait(&cond, &mutex);
        }

        // 打印数据
        fwrite(buffer, sizeof(char), buffer_length, stdout);
        fflush(stdout);

        // 通知读取线程可以继续读取文件
        buffer_length = 0;
        pthread_cond_signal(&cond);

        // 解锁
        pthread_mutex_unlock(&mutex);

        // 判断是否读取完毕
        if (buffer_length == 0) {
            break;
        }
    }

    pthread_exit(NULL);
}

int main(int argc, char *argv[]) {
    pthread_t readThread, printThread;

    if (argc != 2) {
        printf("使用方法:%s <文件名>\n", argv[0]);
        return 0;
    }

    // 创建读取文件的线程
    if (pthread_create(&readThread, NULL, readFileThread, argv[1]) != 0) {
        printf("无法创建读取文件的线程\n");
        return 0;
    }

    // 创建打印数据的线程
    if (pthread_create(&printThread, NULL, printDataThread, NULL) != 0) {
        printf("无法创建打印数据的线程\n");
        return 0;
    }

    // 等待线程结束
    pthread_join(readThread, NULL);
    pthread_join(printThread, NULL);

    return 0;
}

 倒置1234567(循环输出)

#include <head.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
char buf[]="1234567";
sem_t sem1;

void *callBack1(void *arg)
{
	while(1)
	{	
		if(sem_wait(&sem1)<0)
		{
			perror("sem_post");
			return NULL;
		}		
		printf("%s\n",buf);	
		if(sem_post((sem_t*)arg)<0)
		{
			perror("sem_post");
			return NULL;
		}
	}
}
void *callBack2(void *arg)
{
	int sz=sizeof(buf);
	while(1)
	{
		if(sem_wait((sem_t*)arg)<0)
		{
			perror("sem_post");
			return NULL;
		}	
		int lift =0;
		int right =sz-2;
		while(lift<right)
		{
			char t=buf[lift];
			buf[lift]=buf[right];
			buf[right]=t;
			lift++;
			right--;
		}	
		if(sem_post(&sem1)<0)
		{
			perror("sem_post");
			return NULL;
		}		
			
	}
}
int main(int argc, const char *argv[])
{
	sem_t sem;
	if(sem_init(&sem,0,1)<0)
	{
		perror("sem_init");
		return -1;
	}
	if(sem_init(&sem1,0,1)<0)
	{
		perror("sem_init");
		return -1;
	}
	if(sem_wait(&sem)<0)
	{
		perror("sem_post");
		return -1;
	}	
	pthread_t tid1 ,tid2;
	if(pthread_create(&tid1,NULL,callBack1,(void*)&sem)!=0)
	{
		fprintf(stderr,"pthread fail __%d__\n",__LINE__);
		return -1;
	}	
	pthread_detach(tid1); //分离线程1
	if(pthread_create(&tid2,NULL,callBack2,(void*)&sem)!=0)
	{
		fprintf(stderr,"pthread fail __%d__\n",__LINE__);
		return -1;
	}
	
	pthread_join(tid2,NULL);
	sem_destroy(&sem);
	
	return 0;
}

线程整理O_ck钉钉钉的博客-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个可以实现该功能的C程序: ``` #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define MAX_LINE_LENGTH 1000 void *read_csv(void *arg); int main(int argc, char **argv) { if (argc < 2) { printf("Please specify the csv file to read.\n"); exit(1); } FILE *csv_file = fopen(argv[1], "r"); if (csv_file == NULL) { printf("Unable to open file %s.\n", argv[1]); exit(1); } pthread_t threads[10]; // 创建10个线程 int thread_args[10]; // 每个线程读取的行号 for (int i = 0; i < 10; i++) { thread_args[i] = i + 1; pthread_create(&threads[i], NULL, read_csv, &thread_args[i]); } for (int i = 0; i < 10; i++) { pthread_join(threads[i], NULL); } fclose(csv_file); return 0; } void *read_csv(void *arg) { int row = *(int *) arg; char line[MAX_LINE_LENGTH]; FILE *csv_file = fopen("data.csv", "r"); if (csv_file == NULL) { printf("Unable to open file.\n"); pthread_exit(NULL); } for (int i = 0; i < row; i++) { if (fgets(line, MAX_LINE_LENGTH, csv_file) == NULL) { printf("Reached end of file.\n"); break; } } printf("Thread %ld read row %d: %s", pthread_self(), row, line); fclose(csv_file); pthread_exit(NULL); } ``` 该程序读取一个名为"data.csv"的csv文件创建了10个线程,每个线程读取不同的行号。read_csv()函数是每个线程执行的函数,它接收一个整数参数,代表要读取的行号。每个线程打开文件读取指定行号的内容,然后输出到控制台。最后,主线程等待所有线程完成后关闭文件并退出程序。 需要注意的是,该程序没有对文件进行任何的并发控制,因此可能会出现多个线程同时读取同一行的情况。如果需要保证每个线程读取不同的行内容,可以使用互斥锁或其他并发控制机制来避免竞争条件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ck钉钉钉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值