作业一:创建3个线程,一个子线程拷贝文件的前一半,一个子线程拷贝后一半文件,主线程回收子线程资源。
#include <myhead.h>
sem_t sem1,sem2,sem3;
void *fun1(void *ggg)
{
sem_wait(&sem3);
//
int fd, fd1;
fd = open("./1.txt", O_RDONLY);
fd1 = open("./2.txt", O_WRONLY | O_CREAT | O_TRUNC, 0664);
if (fd == -1)
{
perror("open");
return NULL;
}
if (fd1 == -1)
{
perror("open");
close(fd); // 关闭已打开的文件描述符
return NULL;
}
int len = lseek(fd, 0, SEEK_END);
int t = len / 2;
if (t % 2 != 0)
{
t++;
}
lseek(fd, 0, SEEK_SET);
char s[t]; // 动态调整缓冲区大小
read(fd, s, t);
write(fd1, s, t);
close(fd);
close(fd1);
//
fflush(stdout);
sem_post(&sem2);
return NULL;
}
void *fun2(void *ggg)
{
sem_wait(&sem2);
//
int fd, fd1;
fd = open("./1.txt", O_RDONLY);
fd1 = open("./2.txt", O_WRONLY | O_CREAT | O_APPEND, 0664);
if (fd == -1)
{
perror("open");
return NULL;
}
if (fd1 == -1)
{
perror("open");
close(fd); // 关闭已打开的文件描述符
return NULL;
}
int len = lseek(fd, 0, SEEK_END);
int t = len / 2;
if (t % 2 != 0)
{
t++;
}
lseek(fd, t, SEEK_SET);
lseek(fd1, 0, SEEK_END);
char s[t]; // 动态调整缓冲区大小
read(fd, s, t);
write(fd1, s, t);
close(fd);
close(fd1);
//
fflush(stdout);
sem_post(&sem1);
return NULL;
}
void *fun3(void *ggg)
{
sem_wait(&sem1);
//
int fd, fd1;
fd = open("./1.txt", O_RDONLY);
fd1 = open("./2.txt", O_RDONLY);
int len = lseek(fd, 0, SEEK_END);
if (fd1 == -1)
{
perror("open");
close(fd); // 关闭已打开的文件描述符
return NULL;
}
char buf[2*len]; // 动态调整缓冲区大小
lseek(fd1,0,SEEK_SET);
read(fd1,buf,len);
printf("%s\n",buf);
close(fd);
close(fd1);
fflush(stdout);
sem_post(&sem3);
return NULL;
}
int main(int argc, const char *argv[])
{
/*
创建3个线程,子进1程拷贝文件的前一半,子进程2拷贝后一半文件,父进程回收两个子进程资源。
*/
pthread_t tid1, tid2, tid3;
sem_init(&sem1, 0, 0);
sem_init(&sem2, 0, 0);
sem_init(&sem3, 0, 1);
if (pthread_create(&tid1, NULL, fun1, NULL) != 0)
{
perror("ptcreat1");
return -1;
}
if (pthread_create(&tid2, NULL, fun2, NULL) != 0)
{
perror("ptcreat2");
return -1;
}
if (pthread_create(&tid3, NULL, fun3, NULL) != 0)
{
perror("ptcreat3");
return -1;
}
usleep(2);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_join(tid3, NULL);
sem_destroy(&sem1);
sem_destroy(&sem2);
sem_destroy(&sem3);
return 0;
}
加1.txt及空文件2.txt
作业二:使用无名信号量实现循环输出 春、夏、秋、冬。
#include <myhead.h>
sem_t sem1,sem2,sem3,sem4;
void *fun1(void *ggg)
{
while(1)
{
sem_wait(&sem4);
printf("春天\t");
fflush(stdout);
sem_post(&sem3);
}
pthread_exit(NULL);
}
void *fun2(void *ggg)
{
while(1)
{
sem_wait(&sem3);
printf("夏天\t");
fflush(stdout);
sem_post(&sem2);
}
pthread_exit(NULL);
}
void *fun3(void *ggg)
{
while(1)
{
sem_wait(&sem2);
printf("秋天\t");
fflush(stdout);
sem_post(&sem1);
}
pthread_exit(NULL);
}
void *fun4(void *ggg)
{
while(1)
{
sem_wait(&sem1);
printf("冬天\t");
fflush(stdout);
sem_post(&sem4);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
/*春 夏 秋 冬
*/
pthread_t tid1,tid2,tid3,tid4;
sem_init(&sem1,0,0);
sem_init(&sem2,0,0);
sem_init(&sem3,0,0);
sem_init(&sem4,0,1);
if(pthread_create(&tid1,NULL,fun1,NULL) != 0)
{
perror("ptcreat1");
return -1;
}
if(pthread_create(&tid2,NULL,fun2,NULL) != 0)
{
perror("ptcreat2");
return -1;
}
if(pthread_create(&tid3,NULL,fun3,NULL) != 0)
{
perror("ptcreat3");
return -1;
}
if(pthread_create(&tid3,NULL,fun4,NULL) != 0)
{
perror("ptcreat4");
return -1;
}
sleep(2);
while(1);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
pthread_join(tid4,NULL);
sem_destroy(&sem1);
sem_destroy(&sem2);
sem_destroy(&sem3);
sem_destroy(&sem4);
return 0;
}