1、要求AB进程做通信
1) A进程发送一句话,B进程接收打印
2) 然后B进程发送给A进程一句话,A进程接收打印
3) 重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程
进程一
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
umask(0);
//创建有名管道
if(mkfifo("./myfifo1",0777)<0)
{
if(errno!=EEXIST)
{
perror("mkfifo");
return -1;
}
}
printf("myfifo1 success\n");
//创建有名管道
if(mkfifo("./myfifo2",0777)<0)
{
if(errno!=EEXIST)
{
perror("mkfifo");
return -1;
}
}
printf("myfifo2 success\n");
//已写方式打开管道文件
int fd_w=open("./myfifo1",O_WRONLY);
if(fd_w<0)
{
perror("open");
return -1;
}
printf("open_wronly success\n");
//已读方式打开管道文件
int fd_r=open("./myfifo2",O_RDONLY);
if(fd_r<0)
{
perror("open");
return -1;
}
printf("open_rdonly success\n");
char arr[128]="";
ssize_t res;
while(1)
{
printf("请输入要传输数据:");
fgets(arr,sizeof(arr),stdin);
arr[strlen(arr)-1]='\0';
if(write(fd_w,arr,sizeof(arr))<0)
{
perror("write");
return -1;
}
if(strcmp(arr,"quit")==0)
break;
//printf("write success\n");
bzero(arr,sizeof(arr));
res=read(fd_r,arr,sizeof(arr));
if(res<0)
{
perror("read");
return -1;
}else if(0==res)
{
printf("对方进程退出\n");
break;
}
if(strcmp(arr,"quit")==0)
break;
printf("读取到对方传输的数据:%s\n",arr);
}
//关闭文件
close(fd_r);
close(fd_w);
return 0;
}
进程二
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
umask(0);
//创建有名管道
if(mkfifo("./myfifo1",0777)<0)
{
if(errno!=EEXIST)
{
perror("mkfifo");
return -1;
}
}
printf("myfifo1 success\n");
//创建有名管道
if(mkfifo("./myfifo2",0777)<0)
{
if(errno!=EEXIST)
{
perror("mkfifo");
return -1;
}
}
printf("myfifo2 success\n");
//已读方式打开管道文件
int fd_r=open("./myfifo1",O_RDONLY);
if(fd_r<0)
{
perror("open");
return -1;
}
printf("open_rdonly success\n");
//已写方式打开管道文件
int fd_w=open("./myfifo2",O_WRONLY);
if(fd_w<0)
{
perror("open");
return -1;
}
printf("open_wronly success\n");
ssize_t res;
char arr[128];
while(1)
{
bzero(arr,sizeof(arr));
res=read(fd_r,arr,sizeof(arr));
if(res<0)
{
perror("read");
return -1;
}else if(0==res)
{
printf("对方进程退出\n");
break;
}
if(strcmp(arr,"quit")==0)
break;
printf("读取到对方传输的数据 :%s\n",arr);
printf("请输入传输数据:");
fgets(arr,sizeof(arr),stdin);
arr[strlen(arr)-1]='\0';
if(write(fd_w,arr,sizeof(arr))<0)
{
perror("write");
return -1;
}
if(strcmp(arr,"quit")==0)
break;
//printf("write success\n");
}
//关闭文件
close(fd_r);
close(fd_w);
return 0;
}
2 在第一题的基础上实现,AB进程能够随时收发数据(附加题)
思想:在两个进程中各创建两个线程,一个用来读,一个用来写
进程1
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
pthread_t ped;
void *callBack(void *arg)
{
pthread_detach(pthread_self());
int fd_r=*((int*)arg);
char arr[128]="";
ssize_t res;
while(1)
{
bzero(arr,sizeof(arr));
res=read(fd_r,arr,sizeof(arr));
if(res<0)
{
perror("read");
return NULL;
}else if(0==res)
{
printf("对方进程退出\n");
break;
}
if(strcmp(arr,"quit")==0)
{
printf("对方进程退出\n");
exit(0);
}
printf("读取到对方传输的数据:%s\n",arr);
}
}
int main(int argc, const char *argv[])
{
umask(0);
//创建有名管道
if(mkfifo("./myfifo1",0777)<0)
{
if(errno!=EEXIST)
{
perror("mkfifo");
return -1;
}
}
printf("myfifo1 success\n");
//创建有名管道
if(mkfifo("./myfifo2",0777)<0)
{
if(errno!=EEXIST)
{
perror("mkfifo");
return -1;
}
}
printf("myfifo2 success\n");
//已读方式打开管道文件
int fd_r=open("./myfifo1",O_RDONLY);
if(fd_r<0)
{
perror("open");
return -1;
}
printf("open_rdonly success\n");
//已写方式打开管道文件
int fd_w=open("./myfifo2",O_WRONLY);
if(fd_w<0)
{
perror("open");
return -1;
}
printf("open_wronly success\n");
if(pthread_create(&ped,NULL,callBack,(void*)&fd_r)!=0)
{
perror("pthread_create");
return -1;
}
ssize_t res;
char arr[128];
while(1)
{
printf("请输入传输数据:");
fgets(arr,sizeof(arr),stdin);
arr[strlen(arr)-1]='\0';
if(write(fd_w,arr,sizeof(arr))<0)
{
perror("write");
return -1;
}
if(strcmp(arr,"quit")==0)
break;
//printf("write success\n");
}
//关闭文件
close(fd_r);
close(fd_w);
return 0;
}
进程2
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
pthread_t ped;
void *callBack(void *arg)
{
pthread_detach(pthread_self());
int fd_r=*((int*)arg);
char arr[128]="";
ssize_t res;
while(1)
{
bzero(arr,sizeof(arr));
res=read(fd_r,arr,sizeof(arr));
if(res<0)
{
perror("read");
return NULL;
}else if(0==res)
{
printf("对方进程退出\n");
break;
}
if(strcmp(arr,"quit")==0)
{
printf("对方进程退出\n");
exit(0);
}
printf("读取到对方传输的数据:%s\n",arr);
}
}
int main(int argc, const char *argv[])
{
umask(0);
//创建有名管道
if(mkfifo("./myfifo1",0777)<0)
{
if(errno!=EEXIST)
{
perror("mkfifo");
return -1;
}
}
printf("myfifo1 success\n");
//创建有名管道
if(mkfifo("./myfifo2",0777)<0)
{
if(errno!=EEXIST)
{
perror("mkfifo");
return -1;
}
}
printf("myfifo2 success\n");
//已写方式打开管道文件
int fd_w=open("./myfifo1",O_WRONLY);
if(fd_w<0)
{
perror("open");
return -1;
}
printf("open_wronly success\n");
//已读方式打开管道文件
int fd_r=open("./myfifo2",O_RDONLY);
if(fd_r<0)
{
perror("open");
return -1;
}
printf("open_rdonly success\n");
//创建线程获取读权限
if(pthread_create(&ped,NULL,callBack,(void*)&fd_r)!=0)
{
perror("pthread_create");
return -1;
}
char arr[128]="";
ssize_t res;
while(1)
{
printf("请输入要传输数据:");
fgets(arr,sizeof(arr),stdin);
arr[strlen(arr)-1]='\0';
if(write(fd_w,arr,sizeof(arr))<0)
{
perror("write");
return -1;
}
if(strcmp(arr,"quit")==0)
break;
//printf("write success\n");
}
//关闭文件
close(fd_r);
close(fd_w);
return 0;
}
效果
3,捕获2)3)20)号信号
此时使用ctrl+c、ctrl+z、ctrl+\ 均不能关闭进程
只会调用对应的程序
使用killall -9 a.out杀死进程
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
typedef void (*sighandler_t)(int);
void sign(int temp)
{
printf("this is sign\n");
}
void sign1(int temp)
{
printf("this is sign1\n");
}
void sign2(int temp)
{
printf("this is sign2\n");
}
int main(int argc, const char *argv[])
{
printf("sign %p\nsign1 %p\n",sign,sign1);
sighandler_t s1,s2,s3;
s1=signal(2,sign);
if(SIG_ERR==s1)
{
perror("signal");
return -1;
}
printf("s1 %p\n",s1);
s2=signal(3,sign1);
if(SIG_ERR==s2)
{
perror("signal");
return -1;
}
printf("s2 %p\n",s2);
s3=signal(20,sign2);
if(SIG_ERR==s3)
{
perror("signal");
return -1;
}
printf("s3 %p\n",s3);
while(1)
{
printf("this is main\n");
sleep(1);
}
return 0;
}