1. 使用有名管道,完成两个进程的相互通信
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <myhead.h>
int main(int argc, const char *argv[])
{
//创建有名管道fifo文件
if(mkfifo("fifoWork",0664) == -1){
perror("fifoWork error");
return -1;
}
if(mkfifo("fifo2Work",0664) == -1){
perror("fifoWork error");
return -1;
}
//阻塞
getchar();
system("rm fifoWork");
system("rm fifo2Work");
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <myhead.h>
int main(int argc, const char *argv[])
{
//往有名管道写数据
int pid,pid2 =-1;
//创建子进程
int tid = -1;
tid = fork();
//打开文件
if((pid = open("./fifoWork",O_WRONLY)) ==-1){
perror("write fifoWork error");
return -1;
}
if((pid2 = open("./fifo2Work",O_RDONLY)) ==-1){
perror("read fifo2Work error");
return -1;
}
//主线程
if(tid > 0){
char buf[100]="";
while(1){
//终端输入
printf("%d请输入>>>",getpid());
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
//写入
write(pid,buf,sizeof(buf));
if(strcmp(buf,"tuichu") == 0){
break;
}
}
//子线程
}else if(tid ==0){
char buf[100] = "";
while(1){
bzero(buf,sizeof(buf));
read(pid2,buf,sizeof(buf));
printf("%d收到的数据 %s \n",getpid(),buf);
if(strcmp(buf,"tuichu") == 0 ){
break;
}
}
}else{
perror("fork error");
return -1;
}
//关闭文件
close(pid);
close(pid2);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <myhead.h>
int main(int argc, const char *argv[])
{
//从有名管道中读数据
int pid,pid2 = -1;
//创建子进程
int tid2 = -1;
tid2 = fork();
if(( pid = open("./fifoWork",O_RDONLY)) == -1){
perror("read fifoWork error");
return -1;
}
if(( pid2 = open("./fifo2Work",O_WRONLY)) == -1){
perror("read fifo2Work error");
return -1;
}
//主线程
if(tid2 > 0 ){
char buf[100] = "";
while(1){
bzero(buf,sizeof(buf));
read(pid,buf,sizeof(buf));
printf("%d收到的数据 %s\n",getpid(),buf);
if(strcmp(buf,"tuichu") == 0 ){
break;
}
}
}else if(tid2 == 0){
char buf[100] ="";
while(1){
//终端输入
printf("%d请输入>>>",getpid());
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
//写入
write(pid2,buf,sizeof(buf));
if(strcmp(buf,"tuichu") == 0){
break;
}
}
}else{
perror("accept error");
return -1;
}
close(pid);
close(pid2);
return 0;
}
2. 再次重写两个线程文件拷贝
还是会出现不少小错误,1.什么时候需要关闭文件流 2. 什么时候线程退出,什么时候回收资源
3.初始化文件坐标,循环读文件什么时候退出
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <myhead.h>
typedef struct fileInfo{
const char * srcFile;
const char * descFile;
int start;
int len;
}fileInfo,*fileInfo_p;
#endif
#include "head.h"
int getLen(const char * srcFile);
int create_source(const char * descFile);
int thread_copy(const char * srcFile,const char * descFile,int start,int len);
void * copytest(void *arg);
int main(int argc, const char *argv[])
{
//实现多线程,两个线程文件拷贝
//一个线程拷贝一半
//文件从终端输入
if(argc != 3){
printf("please enter file ");
return -1;
}
int len = getLen(argv[1]);
create_source(argv[2]);
// 创建线程1
pthread_t prd;
//结构体初始化
fileInfo info ={argv[1],argv[2],0,len/2};
if(pthread_create(&prd,NULL,copytest,&info)!=0){
printf("pthread_create error\n");
return -1;
}
// 创建线程2
pthread_t prd2;
fileInfo info2 ={argv[1],argv[2],len/2,len-len/2};
if(pthread_create(&prd2,NULL,copytest,&info2)!=0){
printf("pthread_create error\n");
return -1;
}
//资源回收
pthread_join(prd,NULL);
pthread_join(prd2,NULL);
return 0;
}
#include "head.h"
//打开文件得到文件长度
int getLen(const char * srcFile){
//打开文件
int oid = -1;
if(( oid = open(srcFile,O_RDONLY)) == -1){
perror("srcFile error");
return -1;
}
int len = lseek(oid,0,SEEK_END);
close(len);
return len;
}
//创建目标文件
int create_source(const char * descFile){
int oid = -1;
if((oid = open(descFile,O_RDWR|O_CREAT|O_TRUNC,0664)) == -1){
perror("descFile error");
return -1;
}
close(oid);
return 0;
}
int thread_copy(const char * srcFile,const char * descFile,int start,int len){
//读写文件
int red=-1,wed=-1;
if(( red = open(srcFile,O_RDONLY)) == -1){
perror("copy srcFile open error");
return -1;
}
if((wed = open(descFile,O_WRONLY)) ==-1){
perror("copy descFile open error");
return -1;
}
//初始化读写文件坐标
lseek(red,start,SEEK_SET);
lseek(wed,start,SEEK_SET);
//开始写文件
char buf[100] = "";
int sum =0;
int size = 0;
while(1){
size = read(red,buf,sizeof(buf));
sum += size;
//读取数据什么时候结束
if(size == 0 || sum >= len){
write(wed,buf,size-(sum-len));
break;
}
write(wed,buf,size);
}
close(red);
close(wed);
return 0;
}
//线程调用的函数
void * copytest(void *arg){
fileInfo_p info = (fileInfo_p)arg;
thread_copy(info->srcFile,info->descFile,info->start,info->len);
//退出线程
pthread_exit(NULL);
}
3. 思维导图