作业一:创建3个线程,一个子线程拷贝文件的前一半,一个子线程拷贝后一半文件,主线程回收子线程资源。
#include<stdio.h>
#include<string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
pthread_t tid_main,tid1,tid2;
int copy_front_half(int star,int len)
{
int fd1,fd2,fd3;
fd1=open("./1.txt",O_RDONLY);
fd2=open("./2.txt",O_WRONLY|O_CREAT|O_TRUNC,0665);
// fd3=open("./3.txt",O_WRONLY|O_CREAT|O_TRUNC,0665);
char buff[1024];
int sum=0;
lseek(fd1,star,SEEK_SET);
while(1)
{
int res=read(fd1,buff,sizeof(buff));
sum+=res;
if(res==0||sum>=len)
{
write(fd2,buff,res-(res-len));
break;
}
write(fd2,buff,res);
}
close(fd1);
close(fd2);
}
int copy_rear_half(int star,int len)
{
int fd1,fd2,fd3;
fd1=open("./1.txt",O_RDONLY);
fd3=open("./3.txt",O_WRONLY|O_CREAT|O_TRUNC,0665);
char buff[1024];
int sum=0;
lseek(fd1,star,SEEK_SET);
while(1)
{
int res=read(fd1,buff,sizeof(buff));
sum+=res;
if(res==0||sum>=len)
{
write(fd3,buff,res-(sum-len));
break;
}
write(fd3,buff,res);
}
close(fd1);
close(fd3);
}
void *fun1(void *a)
{
int star=0;
int len=*(int *)a;
copy_front_half(0,len/2);
pthread_exit(NULL);
}
void *fun2(void *a)
{
int star=0;
int len=*(int *)a;
copy_rear_half(len/2,len-len/2);
pthread_exit(NULL);
}
void *fun_main(void *a)
{
int len=*(int *)a;
if(pthread_create(&tid1,NULL,fun1,&len)!=0)
{
perror("ptcreate");
return NULL;
}
if(pthread_create(&tid2,NULL,fun2,&len)!=0)
{
perror("ptcreate");
return NULL;
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
int fd;
fd=open("./1.txt",O_RDONLY);
int len=lseek(fd,0,SEEK_END);
if(pthread_create(&tid_main,NULL,fun_main,&len)!=0)
{
perror("ptcreate");
return -1;
}
close(fd);
pthread_join(tid_main,NULL);
return 0;
}
作业二(使用无名信号量实现春夏秋冬的循环输出)
#include<stdio.h>
#include<string.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
sem_t sem1,sem2,sem3,sem4;
void *fun1(void *a)
{
while(1)
{
sleep(1);
sem_wait(&sem4);
printf("春\t");
fflush(stdout);
sem_post(&sem1);
}
pthread_exit(NULL);
}
void *fun2(void *a)
{
while(1)
{
sem_wait(&sem1);
printf("夏\t");
fflush(stdout);
sem_post(&sem2);
}
pthread_exit(NULL);
}
void *fun3(void *a)
{
while(1)
{
sem_wait(&sem2);
printf("秋\t");
fflush(stdout);
sem_post(&sem3);
}
pthread_exit(NULL);
}
void *fun4(void *a)
{
while(1)
{
sem_wait(&sem3);
printf("冬\n");
fflush(stdout);
sem_post(&sem4);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
sem_init(&sem1,0,0);
sem_init(&sem2,0,0);
sem_init(&sem3,0,0);
sem_init(&sem4,0,1);
pthread_t tid1,tid2,tid3,tid4;
if(pthread_create(&tid1,NULL,fun1,NULL)!=0)
{
perror("ptcreate1");
return -1;
}
if(pthread_create(&tid2,NULL,fun2,NULL)!=0)
{
perror("ptcreate2");
return -1;
}
if(pthread_create(&tid3,NULL,fun3,NULL)!=0)
{
perror("ptcreate3");
return -1;
}
if(pthread_create(&tid4,NULL,fun4,NULL)!=0)
{
perror("ptcreate4");
return -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;
}