线程相关作业
1.创建两个线程,分支线程1拷贝文件的前一部分,分支线程2拷贝文件的后一部分
#include "head.h"
#define BUFFER_SIZE 1024
// 线程参数结构体,包含文件名和文件偏移量
typedef struct {
FILE *src_file;
FILE *dest_file;
long start_offset;
long length;
} ThreadArgs;
// 线程1:拷贝文件的前一部分
void *copyFirstPart(void *args) {
ThreadArgs *thread_args = (ThreadArgs *)args;
FILE *src = thread_args->src_file;
FILE *dest = thread_args->dest_file;
long start_offset = thread_args->start_offset;
long length = thread_args->length;
// 分配缓冲区
char buffer[BUFFER_SIZE];
fseek(src, start_offset, SEEK_SET);
long bytes_read = 0;
while (bytes_read < length) {
size_t read_size = (length - bytes_read < BUFFER_SIZE) ? (length - bytes_read) : BUFFER_SIZE;
size_t read_count = fread(buffer, 1, read_size, src);
fwrite(buffer, 1, read_count, dest);
bytes_read += read_count;
}
printf("Thread 1 finished copying the first part.\n");
return NULL;
}
// 线程2:拷贝文件的后一部分
void *copySecondPart(void *args) {
ThreadArgs *thread_args = (ThreadArgs *)args;
FILE *src = thread_args->src_file;
FILE *dest = thread_args->dest_file;
long start_offset = thread_args->start_offset;
long length = thread_args->length;
// 分配缓冲区
char buffer[BUFFER_SIZE];
fseek(src, start_offset, SEEK_SET);
long bytes_read = 0;
while (bytes_read < length) {
size_t read_size = (length - bytes_read < BUFFER_SIZE) ? (length - bytes_read) : BUFFER_SIZE;
size_t read_count = fread(buffer, 1, read_size, src);
fwrite(buffer, 1, read_count, dest);
bytes_read += read_count;
}
printf("Thread 2 finished copying the second part.\n");
return NULL;
}
int main() {
FILE *src_file = fopen("head.h", "rb");
if (src_file == NULL) {
perror("Error opening source file");
return 1;
}
// 获取源文件的大小
fseek(src_file, 0, SEEK_END);
long file_size = ftell(src_file);
fseek(src_file, 0, SEEK_SET);
// 创建两个目标文件
FILE *dest_file1 = fopen("part1.h", "wb");
FILE *dest_file2 = fopen("part2.h", "wb");
if (dest_file1 == NULL || dest_file2 == NULL) {
perror("Error opening destination file");
fclose(src_file);
return 1;
}
// 创建线程
pthread_t thread1, thread2;
// 设置线程参数
ThreadArgs args1 = {src_file, dest_file1, 0, file_size / 2};
ThreadArgs args2 = {src_file, dest_file2, file_size / 2, file_size - file_size / 2};
// 创建两个线程,分别拷贝前后部分
pthread_create(&thread1, NULL, copyFirstPart, &args1);
pthread_create(&thread2, NULL, copySecondPart, &args2);
// 等待线程完成
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
// 关闭文件
fclose(src_file);
fclose(dest_file1);
fclose(dest_file2);
printf("File copy completed.\n");
return 0;
}
2.创建3个线程,线程A打印A,线程B打印B,线程C打印C,要求重复打印顺序ABC (分别使用信号量和条件变量实现)
#include "head.h"
sem_t semA, semB, semC; // 信号量A, B, C
// 线程A打印A
void* printA(void* arg) {
while (1) {
sem_wait(&semA); // 等待信号量A
printf("A\n");
sleep(1);
sem_post(&semB); // 释放信号量B,通知线程B执行
}
return NULL;
}
// 线程B打印B
void* printB(void* arg) {
while (1) {
sem_wait(&semB); // 等待信号量B
printf("B\n");
sleep(1);
sem_post(&semC); // 释放信号量C,通知线程C执行
}
return NULL;
}
// 线程C打印C
void* printC(void* arg) {
while (1) {
sem_wait(&semC); // 等待信号量C
printf("C\n");
sleep(1);
sem_post(&semA); // 释放信号量A,通知线程A执行
}
return NULL;
}
int main() {
// 初始化信号量
sem_init(&semA, 0, 1); // 初始化信号量A为1,线程A先执行
sem_init(&semB, 0, 0); // 初始化信号量B为0,线程B等待线程A
sem_init(&semC, 0, 0); // 初始化信号量C为0,线程C等待线程B
// 创建线程
pthread_t threadA, threadB, threadC;
pthread_create(&threadA, NULL, printA, NULL);
pthread_create(&threadB, NULL, printB, NULL);
pthread_create(&threadC, NULL, printC, NULL);
// 等待线程结束(实际上永远不会结束,因为是无限循环)
pthread_join(threadA, NULL);
pthread_join(threadB, NULL);
pthread_join(threadC, NULL);
// 销毁信号量
sem_destroy(&semA);
sem_destroy(&semB);
sem_destroy(&semC);
return 0;
}
#include "head.h"
// 定义条件变量和互斥锁
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
// 用于控制线程执行的变量
int current_thread = 0; // 0: A, 1: B, 2: C
// 线程 A
void* print_A(void* arg) {
while (1) {
pthread_mutex_lock(&mutex);
while (current_thread != 0) {
pthread_cond_wait(&cond, &mutex); // 等待轮到线程 A
}
sleep(1);
printf("A\n"); // 打印 A
fflush(stdout); // 刷新输出缓冲区
current_thread = 1; // 切换到线程 B
pthread_cond_broadcast(&cond); // 唤醒其他线程
pthread_mutex_unlock(&mutex);
}
return NULL;
}
// 线程 B
void* print_B(void* arg) {
while (1) {
pthread_mutex_lock(&mutex);
while (current_thread != 1) {
pthread_cond_wait(&cond, &mutex); // 等待轮到线程 B
}
sleep(1);
printf("B\n"); // 打印 B
fflush(stdout); // 刷新输出缓冲区
current_thread = 2; // 切换到线程 C
pthread_cond_broadcast(&cond); // 唤醒其他线程
pthread_mutex_unlock(&mutex);
}
return NULL;
}
// 线程 C
void* print_C(void* arg) {
while (1) {
pthread_mutex_lock(&mutex);
while (current_thread != 2) {
pthread_cond_wait(&cond, &mutex); // 等待轮到线程 C
}
sleep(1);
printf("C\n"); // 打印 C
fflush(stdout); // 刷新输出缓冲区
current_thread = 0; // 切换到线程 A
pthread_cond_broadcast(&cond); // 唤醒其他线程
pthread_mutex_unlock(&mutex);
}
return NULL;
}
int main() {
pthread_t thread_A, thread_B, thread_C;
// 创建线程
pthread_create(&thread_A, NULL, print_A, NULL);
pthread_create(&thread_B, NULL, print_B, NULL);
pthread_create(&thread_C, NULL, print_C, NULL);
// 等待线程结束
pthread_join(thread_A, NULL);
pthread_join(thread_B, NULL);
pthread_join(thread_C, NULL);
return 0;
}