实现AB进程对话,要求AB进程能够随时收发。
使用多进程
//A process
#include "/home/ubuntu/li/IO/head.h"
void zom(int sig)
{
while(waitpid(-1,NULL,WNOHANG));
//返回值为正数,继续回收
//返回值为0,说明还有子进程,子进程未结束
//返回值为负数,没有子进程
}
int main(int argc, const char *argv[])
{
//回收僵尸进程
sighandler_t s = signal(17,zom);
if(SIG_ERR == s)
{
ERR_MSG("signal");
return -1;
}
if(mkfifo("./fifo1",0775)<0)
{
printf("line = %d\n",__LINE__);
if(errno!=17){
ERR_MSG("mkfifo");
return -1;
}
}
if(mkfifo("./fifo2",0775)<0)
{
printf("line = %d\n",__LINE__);
if(errno!=17){
ERR_MSG("mkfifo");
return -1;
}
}
pid_t cpid = fork();
int fd_r = open("./fifo1",O_RDONLY);
int fd_w = open("./fifo2",O_WRONLY);
if(fd_r<0||fd_w<0)
{
ERR_MSG("open");
return -1;
}
if(cpid>0)
{
//写
char buf[20];
while(1){
bzero(buf,sizeof(buf));
printf("请输入:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
if(write(fd_w,buf,sizeof(buf))<0)
{
ERR_MSG("write");
return -1;
}
printf("成功写入\n");
if(!strcmp(buf,"quit"))
break;
}
kill(cpid,9);
}
else if(0 == cpid)
{
//读
char buf[20];
while(1)
{
ssize_t res;
bzero(buf,sizeof(buf));
res = read(fd_r,buf,sizeof(buf));
if(res<0)
{
ERR_MSG("read");
return -1;
}
else if(0 == res)
{
printf("对端结束\n");
break;
}
printf("\nres = %ld,buf:%s",res,buf);
fflush(stdout);
if(strcmp(buf,"quit")==0)
break;
}
kill(getppid(),9);
exit(0);
}
else
{
ERR_MSG("fork");
return -1;
}
close(fd_r);
close(fd_w);
return 0;
}
//B process
#include "/home/ubuntu/li/IO/head.h"
void zom(int sig)
{
while(waitpid(-1,NULL,WNOHANG));
//返回值为正数,继续回收
//返回值为0,说明还有子进程,子进程未结束
//返回值为负数,没有子进程
}
int main(int argc, const char *argv[])
{
//回收僵尸进程
sighandler_t s = signal(17,zom);
if(SIG_ERR == s)
{
ERR_MSG("signal");
return -1;
}
if(mkfifo("./fifo1",0775)<0)
{
printf("line = %d\n",__LINE__);
if(errno!=17){
ERR_MSG("mkfifo");
return -1;
}
}
if(mkfifo("./fifo2",0775)<0)
{
printf("line = %d\n",__LINE__);
if(errno!=17){
ERR_MSG("mkfifo");
return -1;
}
}
pid_t cpid = fork();
int fd_w = open("./fifo1",O_WRONLY);
int fd_r = open("./fifo2",O_RDONLY);
if(fd_r<0||fd_w<0)
{
ERR_MSG("open");
return -1;
}
if(cpid>0)
{
//写
char buf[20];
while(1){
bzero(buf,sizeof(buf));
printf("请输入:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
if(write(fd_w,buf,sizeof(buf))<0)
{
ERR_MSG("write");
return -1;
}
printf("成功写入\n");
if(!strcmp(buf,"quit"))
break;
}
kill(cpid,9);//杀死子进程
}
else if(0 == cpid)
{
//读
char buf[20];
while(1)
{
ssize_t res;
bzero(buf,sizeof(buf));
res = read(fd_r,buf,sizeof(buf));
if(res<0)
{
ERR_MSG("read");
return -1;
}
else if(0 == res)
{
printf("对端结束\n");
break;
}
printf("\nres = %ld,buf:%s",res,buf);
fflush(stdout);
int a = strcmp(buf,"quit");
if(strcmp(buf,"quit")==0)
break;
}
kill(getppid(),9);//通知父进程结束
exit(0);
}
else
{
ERR_MSG("fork");
return -1;
}
close(fd_r);
close(fd_w);
return 0;
}
//head.h
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <dirent.h>
#include <errno.h>
#include <sys/wait.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
typedef void (*sighandler_t)(int);
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d\n",__LINE__);\
perror(msg);\
}while(0)