1.fifo a,b 实时聊天。可以连续收发。发送#quit,a,b都退出。
1.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
void * th1(void* arg)
{
int fd = *(int*)arg;
while(1)
{
char buf[128]={0};
int ret = read(fd,buf,sizeof(buf));
if(ret<=0)
{
exit(1);
}
if(0 == strcmp(buf,"#quit\n"))
{
exit(1);
}
printf("from A:%s",buf);
fflush(stdout);
}
return NULL;
}
void* th2(void* arg)
{
int fd = *(int *)arg;
while(1)
{
char buf[128]={0};
printf("To A:");
fgets(buf,sizeof(buf),stdin);
write(fd,buf,strlen(buf));
if(0 ==strcmp(buf,"#quit\n"))
{
exit(1);
}
}
return NULL;
}
int main(int argc, char *argv[])
{
int ret = mkfifo("myfifo1",0666);
if(-1 == ret)
{
//如果是管道文件已存在错误,让程序继续运行
if(EEXIST== errno)
{
}else
{
perror("mkfifo");
exit(1);
}
}
ret = mkfifo("myfifo2",0666);
if(-1 == ret)
{
//如果是管道文件已存在错误,让程序继续运行
if(EEXIST== errno)
{
}else
{
perror("mkfifo");
exit(1);
}
}
//open 会阻塞,等到另一端读段打开,解除阻塞
int fd_r = open("myfifo1",O_RDONLY);
if(-1 == fd_r)
{
perror("open");
exit(1);
}
int fd_w = open("myfifo2",O_WRONLY);
if(-1 == fd_w)
{
perror("open");
exit(1);
}
pthread_t tid1,tid2;
pthread_create(&tid1,NULL,th1,&fd_r);
pthread_create(&tid2,NULL,th2,&fd_w);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}
2.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
void * th1(void* arg)
{
int fd = *(int*)arg;
while(1)
{
char buf[128]={0};
int ret = read(fd,buf,sizeof(buf));
if(ret<=0)
{
exit(1);
}
if(0 == strcmp(buf,"#quit\n"))
{
exit(1);
}
printf("from B:%s",buf);
fflush(stdout);
}
return NULL;
}
void* th2(void* arg)
{
int fd = *(int *)arg;
while(1)
{
char buf[128]={0};
printf("To b:");
fgets(buf,sizeof(buf),stdin);
write(fd,buf,strlen(buf));
if(0 ==strcmp(buf,"#quit\n"))
{
exit(1);
}
}
return NULL;
}
int main(int argc, char *argv[])
{
int ret = mkfifo("myfifo1",0666);
if(-1 == ret)
{
//如果是管道文件已存在错误,让程序继续运行
if(EEXIST== errno)
{
}else
{
perror("mkfifo");
exit(1);
}
}
ret = mkfifo("myfifo2",0666);
if(-1 == ret)
{
//如果是管道文件已存在错误,让程序继续运行
if(EEXIST== errno)
{
}else
{
perror("mkfifo");
exit(1);
}
}
//open 会阻塞,等到另一端读段打开,解除阻塞
int fd_w = open("myfifo1",O_WRONLY);
if(-1 == fd_w)
{
perror("open");
exit(1);
}
int fd_r = open("myfifo2",O_RDONLY);
if(-1 == fd_w)
{
perror("open");
exit(1);
}
pthread_t tid1,tid2;
pthread_create(&tid1,NULL,th1,&fd_r);
pthread_create(&tid2,NULL,th2,&fd_w);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
}
2.pipe,父进程把dict.txt写入管道。
子进程从终端接收需要查找的单词。
找到后显示意思。如果没找到,显示单词输入错误。如果输入#quit,退出。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#define MAX 19661
int main(int argc, char *argv[])
{
int pipefd[2]={0};
int ret = pipe(pipefd);
if(-1 == ret)
{
perror("pipe");
exit(1);
}
pid_t pid = fork();
if(pid>0)
{
close(pipefd[0]);
int fd = open("/home/linux/dict.txt",O_RDONLY);
if(-1 == fd)
{
perror("open");
exit(1);
}
while(1)
{
while(1)
{
char buf[4096]={0};
int rd_ret = read(fd,buf,sizeof(buf));
if(0==rd_ret)
{
break;
}
write(pipefd[1],buf,rd_ret);
}
lseek(fd,0,SEEK_SET);
}
}
else if(0 == pid)
{
close(pipefd[1]);
FILE* fp = fdopen(pipefd[0],"r");
while(1)
{
char want_word[128]={0};
int num =1;
printf("input word");
fgets(want_word,sizeof(want_word),stdin);//zoo\n
want_word[strlen(want_word)-1]='\0';
if(0 == strcmp(want_word,"#quit"))
{
exit(0);
}
while(1)
{
char line_buf[512]={0};
fgets(line_buf,sizeof(line_buf),fp);//FILaE*
char *arg [2]={NULL};
arg[0]=strtok(line_buf," ");
arg[1]=strtok(NULL,"\r");
if(0==strcmp(arg[0],want_word))
{
printf("%s %s\n",arg[0],arg[1]);
break;
}
num++;
if(num>MAX)
{
printf("没找到这个单词 %s\n",want_word);
break;
}
}
}
}
else
{
perror("fork");
exit(1);
}
return 0;
}