1.单次发送单次读
//fifo的创建
#include <head.h>
int main(int argc,const char * argv[])
{
if(mkfifo("./fifo1",0666) < 0){
perror("mkfifo");
return -1;
}
if(mkfifo("./fifo2",0666) < 0){
perror("mkfifo");
return -1;
}
getchar();
system("sudo rm fifo1");//关闭
system("sudo rm fifo2");
return 0;
return 0;
}
//read.c
#include <head.h>
int main(int argc,const char * argv[])
{
int fd = open("/home/ubuntu/xwf/IOday6/fifo1",O_RDWR);//打开管道1
if(fd < 0){
perror("open");
printf("__%d__",__LINE__);
return -1;
}
int fd1 = open("/home/ubuntu/xwf/IOday6/fifo2",O_RDWR);//打开管道2
if(fd1 < 0){
perror("open");
printf("__%d__",__LINE__);
return -1;
}
char buf[128] = "";
char str[128] = "";
while(1){//读write
memset(buf,0,sizeof(buf));
read(fd,buf,sizeof(buf));
printf("read.c read:%s\n",buf);
if(0 == strcmp(buf,"quit")){
break;
}
//read.c write:
printf("read.c write\n");//写入管道
scanf("%s",str);
write(fd1,str,sizeof(str));
if(0 == strcmp(str,"quit")){
break;
}
memset(str,0,sizeof(str));
}
return 0;
}
//write
#include <head.h>
char buf[128] = "";
char str[128] = "";
int main(int argc,const char * argv[])
{
int fd = open("/home/ubuntu/xwf/IOday6/fifo1",O_RDWR);
if(fd < 0){
perror("open");
printf("__%d__",__LINE__);
return -1;
}
int fd1 = open("/home/ubuntu/xwf/IOday6/fifo2",O_RDWR);
if(fd1 < 0){
perror("open");
printf("__%d__",__LINE__);
return -1;
}
while(1){
printf("write.c -->write:\n");//写入管道
scanf("%s",buf);
write(fd,buf,sizeof(buf));
if(0 == strcmp(buf,"quit")){
break;
}
memset(buf,0,sizeof(buf));
//读出read.c中输出的
memset(str,0,sizeof(str));//读管道
read(fd1,str,sizeof(str));
printf("write.c -->read:%s\n",str);
if(0 == strcmp(str,"quit")){
break;
}
}
return 0;
}
2.多次发送多次读
//管道创建
#include <head.h>
int main(int argc,const char * argv[])
{
if(mkfifo("./fifo1",0666) < 0){
perror("mkfifo");
return -1;
}
if(mkfifo("./fifo2",0666) < 0){
perror("mkfifo");
return -1;
}
getchar();
system("sudo rm fifo1");
system("sudo rm fifo2");
return 0;
return 0;
}
//read.c
#include <head.h>
char buf[128] = "";
char str[128] = "";
pthread_t tid,tid1;
void* READ(void* arg){//第一个线程打开管道1
int fd = open("/home/ubuntu/xwf/IOday6/fifo1",O_RDWR);
if(fd < 0){
perror("open");
printf("__%d__",__LINE__);
return NULL;
}
while(1){//读管道1
memset(str,0,sizeof(str));
read(fd,str,sizeof(str));
printf("01_read.c -->read:%s\n",str);
if(0 == strcmp(str,"quit")){
break;
}
}
close(fd);//退出后要请求第二个线程退出
pthread_cancel(tid1);
pthread_exit(NULL);
}
void* WRITE(void* arg){//第二个线程打开管道2
int fd1 = open("/home/ubuntu/xwf/IOday6/fifo2",O_RDWR);
if(fd1 < 0){
perror("open");
printf("__%d__",__LINE__);
return NULL;
}
while(1){//写入管道2
printf("01_read.c -->write:\n");
scanf("%s",buf);
write(fd1,buf,sizeof(buf));
if(0 == strcmp(buf,"quit")){
break;
}
memset(buf,0,sizeof(buf));
}
close(fd1);//退出后要请求第一个线程退出
pthread_cancel(tid);
pthread_exit(NULL);
}
int main(int argc,const char * argv[])
{
//创建两个线程
if(pthread_create(&tid,NULL,WRITE,NULL) != 0){
printf("error\n");
return -1;
}
if(pthread_create(&tid,NULL,READ,NULL) != 0){
printf("error\n");
return -1;
}
pthread_join(tid,NULL);
pthread_join(tid1,NULL);
return 0;
}
//write.c
#include <head.h>
char buf[128] = "";
char str[128] = "";
pthread_t tid,tid1;
void* WRITE(void* arg){
int fd = open("/home/ubuntu/xwf/IOday6/fifo1",O_RDWR);
if(fd < 0){
perror("open");
printf("__%d__",__LINE__);
return NULL;
}
while(1){
printf("01_write.c -->write:");
scanf("%s",buf);
write(fd,buf,sizeof(buf));
if(0 == strcmp(buf,"quit")){
break;
}
memset(buf,0,sizeof(buf));
}
close(fd);
pthread_cancel(tid1);
pthread_exit(NULL);
}
void* READ(void* arg){
int fd1 = open("/home/ubuntu/xwf/IOday6/fifo2",O_RDWR);
if(fd1 < 0){
perror("open");
printf("__%d__",__LINE__);
return NULL;
}
while(1){
memset(str,0,sizeof(str));
read(fd1,str,sizeof(str));
printf("01_write.c -->read:%s\n",str);
if(0 == strcmp(str,"quit")){
break;
}
}
close(fd1);
pthread_cancel(tid);
pthread_exit(NULL);
}
int main(int argc,const char * argv[])
{
//创建两个线程
if(pthread_create(&tid,NULL,WRITE,NULL) != 0){
printf("error\n");
return -1;
}
if(pthread_create(&tid1,NULL,READ,NULL) != 0){
printf("error\n");
return -1;
}
pthread_join(tid,NULL);
pthread_join(tid1,NULL);
return 0;
}
//第二种方法
//fifo文件一致
//read.c
#include <head.h>
int main(int argc,const char * argv[])
{
char str[128] = "";
char buf[128] = "";
pid_t pid;
pid = fork();
if(pid > 0){//实现读
int fd = open("/home/ubuntu/xwf/IOday6/fifo1",O_RDWR);
if(fd < 0){
perror("open");
printf("__%d__",__LINE__);
return -1;
}
while(1){
memset(str,0,sizeof(str));
read(fd,str,sizeof(str));
printf("02_read.c -->read:%s\n",str);
if(0 == strcmp(str,"quit")){
break;
}
}
close(fd);
kill(pid,9);
}else if(0 == pid){//子进程实现写
int fd1 = open("/home/ubuntu/xwf/IOday6/fifo2",O_RDWR);
if(fd1 < 0){
perror("open");
printf("__%d__",__LINE__);
return -1;
}
while(1){
printf("02_read.c -->write:\n");
scanf("%s",buf);
write(fd1,buf,sizeof(buf));
if(0 == strcmp(buf,"quit")){
break;
}
memset(buf,0,sizeof(buf));
}
close(fd1);
kill(getppid(),9);
}else{//创建错误
printf("error\n");
return -1;
}
wait(NULL);
return 0;
}
//write.c
#include <head.h>
int main(int argc,const char * argv[])
{
char str[128] = "";
char buf[128] = "";
pid_t pid;
pid = fork();
if(pid > 0){//实现读
int fd = open("/home/ubuntu/xwf/IOday6/fifo2",O_RDWR);
if(fd < 0){
perror("open");
printf("__%d__",__LINE__);
return -1;
}
while(1){
memset(str,0,sizeof(str));
read(fd,str,sizeof(str));
printf("02_write.c -->read:%s\n",str);
if(0 == strcmp(str,"quit")){
break;
}
}
kill(pid,9);
close(fd);
}else if(0 == pid){//子进程实现写
int fd1 = open("/home/ubuntu/xwf/IOday6/fifo1",O_RDWR);
if(fd1 < 0){
perror("open");
printf("__%d__",__LINE__);
return -1;
}
while(1){
printf("02_write.c -->write:\n");
scanf("%s",buf);
write(fd1,buf,sizeof(buf));
if(0 == strcmp(buf,"quit")){
break;
}
memset(buf,0,sizeof(buf));
}
kill(getppid(),9);
close(fd1);
}else{//创建错误
printf("error\n");
return -1;
}
wait(NULL);
return 0;
}
3.捕捉2,3,20号信号
#include <head.h>
void hander(int arg){
printf("信号2响应了一次\n");
return ;
}
void hander1(int arg){
printf("信号3响应了一次\n");
return ;
}
void hander2(int arg){
printf("信号20响应了一次\n");
return ;
}
int main(int argc,const char * argv[])
{
__sighandler_t s = signal(2,hander);
if(SIG_ERR == s){
printf("__%d__\n",__LINE__);
perror("signal");
return -1;
}
__sighandler_t s1 = signal(3,hander1);
if(SIG_ERR == s1){
printf("__%d__\n",__LINE__);
perror("signal");
return -1;
}
__sighandler_t s2 = signal(20,hander2);
if(SIG_ERR == s2){
printf("__%d__\n",__LINE__);
perror("signal");
return -1;
}
while(1){
printf("./sig....\n");
sleep(1);
}
return 0;
}