创建三个线程,子线程1拷贝文件前一半,子线程2拷贝文件后一半,主线程回收子线程资源
#include <myhead.h>
#include <pthread.h>
// 线程函数声明
void* thread_function1(void* arg);
void* thread_function2(void* arg);
int main()
{
pthread_t thread1, thread2;
int ret;
// 创建线程1
ret = pthread_create(&thread1, NULL, thread_function1, NULL);
if (ret != 0)
{
perror("pthread1");
return 1;
}
// 创建线程2
ret = pthread_create(&thread2, NULL, thread_function2, NULL);
if (ret != 0)
{
perror("pthread2");
return 1;
}
// 等待线程结束
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("所有线程已完成\n");
return 0;
}
// 线程1的函数
void* thread_function1(void* arg)
{
printf("线程1开始执行\n");
int fd1,fd2;
fd1 = open("./1.txt",O_RDONLY);
fd2 = open("./2.txt",O_WRONLY|O_CREAT|O_TRUNC,0664);;
if(fd1==-1)
{
perror("open");
pthread_exit(NULL);
}
if(fd2==-1)
{
perror("open");
pthread_exit(NULL);
}
char buff[4096];
int len;
len = lseek(fd1,0,SEEK_END);
lseek(fd1,0,SEEK_SET);
read(fd1,buff,len/2);
write(fd2,buff,len/2);
close(fd1);
close(fd2);
sleep(1); // 模拟一些工作
printf("线程1执行完毕\n");
return NULL;
}
// 线程2的函数
void* thread_function2(void* arg)
{
printf("线程2开始执行\n");
int fd1,fd2;
fd1 = open("./1.txt",O_RDONLY);
fd2 = open("./2.txt",O_WRONLY|O_CREAT,0664);;
if(fd1==-1)
{
perror("open");
pthread_exit(NULL);
}
if(fd2==-1)
{
perror("open");
pthread_exit(NULL);
}
char buff[4096];
int len;
len = lseek(fd1,0,SEEK_END);
lseek(fd1,len/2,SEEK_SET);
lseek(fd2,len/2,SEEK_SET);
read(fd1,buff,len-len/2);
write(fd2,buff,len-len/2);
close(fd1);
close(fd2);
sleep(2); // 模拟一些工作
printf("线程2执行完毕\n");
return NULL;
}
使用无名信号量实现循环输出”春“”夏“”秋“”冬“
#include <myhead.h>
#include <pthread.h>
#define MAX 100 // 循环输出的次数
sem_t *sem_spring, *sem_summer, *sem_autumn, *sem_winter;
void* print_spring(void* arg)
{
for (int i = 0; i < MAX; i++)
{
sem_wait(sem_spring);
printf("春\t");
fflush(stdout);
sem_post(sem_summer);
}
return NULL;
}
void* print_summer(void* arg)
{
for (int i = 0; i < MAX; i++)
{
sem_wait(sem_summer);
printf("夏\t");
fflush(stdout);
sem_post(sem_autumn);
}
return NULL;
}
void* print_autumn(void* arg)
{
for (int i = 0; i < MAX; i++)
{
sem_wait(sem_autumn);
printf("秋\t");
fflush(stdout);
sem_post(sem_winter);
}
return NULL;
}
void* print_winter(void* arg)
{
for (int i = 0; i < MAX; i++)
{
sem_wait(sem_winter);
printf("冬\n");
fflush(stdout);
sem_post(sem_spring);
}
return NULL;
}
int main()
{
pthread_t t_spring, t_summer, t_autumn, t_winter;
// 创建信号量
sem_spring = sem_open("/sem_spring", O_CREAT, 0644, 1);
sem_summer = sem_open("/sem_summer", O_CREAT, 0644, 0);
sem_autumn = sem_open("/sem_autumn", O_CREAT, 0644, 0);
sem_winter = sem_open("/sem_winter", O_CREAT, 0644, 0);
if (sem_spring == SEM_FAILED || sem_summer == SEM_FAILED ||
sem_autumn == SEM_FAILED || sem_winter == SEM_FAILED)
{
perror("sem_open");
return 1;
}
// 创建线程
pthread_create(&t_spring, NULL, print_spring, NULL);
pthread_create(&t_summer, NULL, print_summer, NULL);
pthread_create(&t_autumn, NULL, print_autumn, NULL);
pthread_create(&t_winter, NULL, print_winter, NULL);
// 回收线程
pthread_join(t_spring, NULL);
pthread_join(t_summer, NULL);
pthread_join(t_autumn, NULL);
pthread_join(t_winter, NULL);
// 关闭信号量
sem_close(sem_spring);
sem_close(sem_summer);
sem_close(sem_autumn);
sem_close(sem_winter);
printf("\n");
return 0;
}