当前位置: 首页 > news >正文

线程管理操作

1.创建两个线程,,分支线程1拷贝文件的前一部分,分支线程2拷贝文件的后一部分


#include <head.h>

#define SRC_FILE "./1.txt"
#define DST_FILE "./2.txt"
#define BUFFER_SIZE 4096

struct copy_args {
    long start;
    long end;
};

void* copy_thread(void* arg) {
    struct copy_args* str= (struct copy_args*)arg;
    
    // 打开源文件和目标文件
    FILE* src = fopen(SRC_FILE, "rb");
    if (!src) {
        perror("Source file open failed");
        pthread_exit(NULL);
    }
    
    FILE* dst = fopen(DST_FILE, "wb+"); 
    if (!dst) {
        perror("Target file open failed");
        fclose(src);
        pthread_exit(NULL);
    }
    
    
    fseek(src, str->start, SEEK_SET);
    fseek(dst, str->start, SEEK_SET);
    
    char buffer[BUFFER_SIZE];
    long remain = str->end - str->start;
    
    while (remain > 0) {
        size_t read_size = fread(buffer, 1, 
            remain > sizeof(buffer) ? sizeof(buffer) : remain, src);
        if (read_size <= 0) break;
        
        size_t write_size = fwrite(buffer, 1, read_size, dst);
        if (write_size != read_size) {
            fprintf(stderr, "Write error\n");
            break;
        }
        remain -= read_size;
    }
    
    fclose(src);
    fclose(dst);
    return NULL;
}

int main() {
    //检查文件状态获取结果
    struct stat st;
    if (stat(SRC_FILE, &st) != 0) {
        perror("Source file not found");
        exit(EXIT_FAILURE);
    }
    
    // 正确处理奇数长度文件
    long half = (st.st_size % 2 == 0) ? 
                st.st_size / 2 : 
                (st.st_size + 1) / 2;

    pthread_t t1, t2;
    
    // 动态分配参数内存(避免栈内存失效)
    struct copy_args* str1 = malloc(sizeof(struct copy_args));
    struct copy_args* str2 = malloc(sizeof(struct copy_args));
    *str1 = (struct copy_args){0, half};
    *str2 = (struct copy_args){half, st.st_size};

    pthread_create(&t1, NULL, copy_thread, str1);
    pthread_create(&t2, NULL, copy_thread, str2);
    
    pthread_join(t1, NULL);
    pthread_join(t2, NULL);
    
    // 释放动态分配的内存
    free(str1);
    free(str2);
    
    printf("File copy completed\n");
    return 0;
}

2.创建3个线程,线程A打印A,线程B打印B,线程C打印C,要求重复打印顺序ABC(分别使用信号量和条件变量实现)


#include <head.h>

sem_t semA, semB, semC;

void* threadA(void* arg) {
    while(1) {
        sem_wait(&semA);
        printf("FIA ");
        sem_post(&semB);
    }
}

void* threadB(void* arg) {
    while(1) {
        sem_wait(&semB);
        printf("FIB ");
        sem_post(&semC);
    }
}

void* threadC(void* arg) {
    while(1) {
        sem_wait(&semC);
        printf("FIC\n");
        sem_post(&semA);
    }
}

int main() {
    sem_init(&semA, 0, 1);
    sem_init(&semB, 0, 0);
    sem_init(&semC, 0, 0);

    pthread_t tA, tB, tC;
    pthread_create(&tA, NULL, threadA, NULL);
    pthread_create(&tB, NULL, threadB, NULL);
    pthread_create(&tC, NULL, threadC, NULL);

    pthread_join(tA, NULL);
    pthread_join(tB, NULL);
    pthread_join(tC, NULL);
}


#include <head.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int state = 0;

void* threadA(void* arg) {
    while(1) {
        pthread_mutex_lock(&mutex);
        while(state != 0) 
            pthread_cond_wait(&cond, &mutex);
        printf("FIA ");
        state = 1;
        pthread_cond_broadcast(&cond);
        pthread_mutex_unlock(&mutex);
    }
}

void* threadB(void* arg) {
    while(1) {
        pthread_mutex_lock(&mutex);
        while(state != 1)
            pthread_cond_wait(&cond, &mutex);
        printf("FIB ");
        state = 2;
        pthread_cond_broadcast(&cond);
        pthread_mutex_unlock(&mutex);
    }
}

void* threadC(void* arg) {
    while(1) {
        pthread_mutex_lock(&mutex);
        while(state != 2)
            pthread_cond_wait(&cond, &mutex);
        printf("FIC\n");
        state = 0;
        pthread_cond_broadcast(&cond);
        pthread_mutex_unlock(&mutex);
    }
}

int main() {
    pthread_t tA, tB, tC;
    pthread_create(&tA, NULL, threadA, NULL);
    pthread_create(&tB, NULL, threadB, NULL);
    pthread_create(&tC, NULL, threadC, NULL);

    pthread_join(tA, NULL);
    pthread_join(tB, NULL);
    pthread_join(tC, NULL);
}

相关文章:

  • 【CSS3】金丹篇
  • 3.3.5 VO-O语法- 高级语法
  • 大语言模型中Token的输出过程
  • vue+dhtmlx-gantt 实现甘特图-快速入门【甘特图】
  • Python 编程题 第八节:字符串变形、压缩字符串、三个数的最大乘积、判定字符是否唯一、IP地址转换
  • KL散度详解与应用
  • FTP 与 TFTP 的详细异同点
  • 信号与系统笔记——第二章 连续系统的时域分析(三)
  • 复现无人机的项目,项目名称为Evidential Detection and Tracking Collaboration
  • 【面试】Kafka
  • OpenCV实现图像分割与无缝合并
  • Jenkins实现自动化构建与部署:上手攻略
  • 机器学习 Day03 Numpy基本使用
  • 2025 ubuntu24系统宿主机上在线安装mysql数据库完整演示
  • Python连接SQL SEVER数据库全流程
  • 基于大模型的结节性甲状腺肿诊疗全流程预测与方案研究报告
  • 【原理理解】图像SNR信噪比理解
  • 关于JSONArray转换为JSONObject的问题解决
  • 第四章:表单与交互:打造你的「数据捕手」
  • 攻防世界 file_include【php://filter详解】
  • 国家能源局:成立核电工程定额专家委员会
  • 文学如何遭遇世界:日本“世界文学”的半个世纪
  • 中方敦促美国停止将溯源问题政治化
  • 上海徐汇 “家 + 书屋”,创新服务广大家庭
  • “当代阿炳”甘柏林逝世,创办了国内第一所残疾人高等学府
  • 减负举措如何助力基层干部轻装上阵?记者一线调查