线程管理操作
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);
}