IOday6
1.创建两个线程,分支线程1拷贝文件的前一部分,分支线程2拷贝文件的后一部分
#include <head.h>
void* frist(void* arg)
{
//打开文件
int fd=open("../4.c",O_RDONLY);
//创建拷贝文件
int fd1=open("./2.c",O_RDWR|O_CREAT,0664);
//读取文件大小
off_t size=lseek(fd,0,SEEK_END);
//重置光标位置
lseek(fd,0,SEEK_SET);
//前半段内容
char* buf=malloc(size/2+1);
size_t res=read(fd,buf,size/2);
write(fd1,buf,size/2);
printf("%ld\n",size);
printf("%ld\n",res);
//关闭文件
free(buf);
close(fd);
close(fd1);
pthread_exit(NULL);
}
void* last(void* arg)
{
//打开文件
int fd=open("../4.c",O_RDONLY);
//创建拷贝文件
int fd1=open("./2.c",O_RDWR|O_CREAT,0664);
//读取文件大小
off_t size=lseek(fd,0,SEEK_END);
//获得剩余要写入的数据
off_t m=size-size/2;
//重置光标位置
lseek(fd,size/2,SEEK_SET);
lseek(fd1,size/2,SEEK_SET);
//读取后半段内容
char* buf=malloc(size/2+1);
size_t res=read(fd,buf,size/2);
write(fd1,buf,size/2-1);
printf("%ld\n",size);
printf("%ld\n",res);
//关闭文件
free(buf);
close(fd);
close(fd1);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//创建两个分支线程
pthread_t pth1,pth2;
if((errno=pthread_create(&pth1,NULL,frist,NULL))!=0)
PRINT_ERROR("pthread_create1 error");
if((errno=pthread_create(&pth2,NULL,last,NULL))!=0)
PRINT_ERROR("pthread_create2 error");
pthread_join(pth1,NULL);
pthread_join(pth2,NULL);
return 0;
}
2.创建3个线程,线程A打印A,线程B打印B,线程C打印C,要求重复打印顺序ABC (分别使用信号量和条件变量实现)
信号量:
#include <head.h>
sem_t sem1,sem2,sem3;
void* funA(void* arg)
{
while(1)
{
//P操作 -1
sem_wait(&sem1);
printf("A\n");
//V操作
sem_post(&sem2);
}
pthread_exit(NULL);
}
void* funB(void* arg)
{
while(1)
{
sleep(1);
//P操作 -1
sem_wait(&sem2);
printf("B\n");
//V操作
sem_post(&sem3);
}
pthread_exit(NULL);
}
void* funC(void* arg)
{
while(1)
{
sleep(1);
//P操作 -1
sem_wait(&sem3);
printf("C\n");
//V操作
sem_post(&sem1);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//信号量初始化
if(sem_init(&sem1,0,1))
PRINT_ERROR("sem_init1 error");
if(sem_init(&sem2,0,1))
PRINT_ERROR("sem_init2 error");
if(sem_init(&sem1,0,1))
PRINT_ERROR("sem_init3 error");
//创建分支线程
pthread_t pth1,pth2,pth3;
if((errno=pthread_create(&pth1,NULL,funA,NULL))!=0)
PRINT_ERROR("pthread_create1 error");
if((errno=pthread_create(&pth2,NULL,funB,NULL))!=0)
PRINT_ERROR("pthread_create2 error");
if((errno=pthread_create(&pth3,NULL,funC,NULL))!=0)
PRINT_ERROR("pthread_create3 error");
//回收分支
pthread_join(pth1,NULL);
pthread_join(pth2,NULL);
pthread_join(pth3,NULL);
//销毁信号量
sem_destroy(&sem1);
sem_destroy(&sem2);
sem_destroy(&sem3);
return 0;
}
条件变量:
#include <head.h>
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond3 = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex= PTHREAD_MUTEX_INITIALIZER;
int flag=0;
void* funA(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
while(flag!=0)
pthread_cond_wait(&cond1,&mutex);
flag=1;
printf("A\n");
pthread_cond_signal(&cond2);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* funB(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
while(flag!=1)
pthread_cond_wait(&cond2,&mutex);
flag=2;
printf("B\n");
pthread_cond_signal(&cond3);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* funC(void* arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
while(flag!=2)
pthread_cond_wait(&cond3,&mutex);
flag=0;
printf("C\n");
pthread_cond_signal(&cond1);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//创建分支线程
pthread_t pth1,pth2,pth3;
if((errno=pthread_create(&pth1,NULL,funA,NULL))!=0)
PRINT_ERROR("pthread_create1 error");
if((errno=pthread_create(&pth2,NULL,funB,NULL))!=0)
PRINT_ERROR("pthread_create2 error");
if((errno=pthread_create(&pth3,NULL,funC,NULL))!=0)
PRINT_ERROR("pthread_create3 error");
//回收分支
pthread_join(pth1,NULL);
pthread_join(pth2,NULL);
pthread_join(pth3,NULL);
//销毁条件变量
pthread_cond_destroy(&cond1);
pthread_cond_destroy(&cond2);
pthread_cond_destroy(&cond3);
return 0;
}