//转自陈颢文(老师)的blog
fifo1.c
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//./a.out fifoname
int main(int argc, const char *argv[])
{
int fd = 0;
if(argc < 2)
{
fprintf(stderr,"Usage : %s argv[1]",argv[0]);
exit(EXIT_FAILURE);
}
//创建有名管道文件
if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
{
perror("Fail to create mkfifo");
exit(EXIT_FAILURE);
}
//打开有名管道文件
//fd = open(argv[1],O_RDONLY);
fd = open(argv[1],O_WRONLY | O_NONBLOCK);
// fd = open(argv[1],O_RDWR);
if(fd < 0)
{
perror("Fail to open");
exit(EXIT_FAILURE);
}
printf("open success!\n");
close(fd);
return 0;
}
fifo.c
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//./a.out fifoname
int main(int argc, const char *argv[])
{
int fd = 0;
if(argc < 2)
{
fprintf(stderr,"Usage : %s argv[1]",argv[0]);
exit(EXIT_FAILURE);
}
//创建有名管道文件
if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
{
perror("Fail to create mkfifo");
exit(EXIT_FAILURE);
}
//打开管道文件
//fd = open(argv[1],O_RDONLY);
fd = open(argv[1],O_WRONLY);
if(fd < 0)
{
perror("Fail to open");
exit(EXIT_FAILURE);
}
printf("open success!\n");
close(fd);
return 0;
}
fifo2.c
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
//./a.out fifoname
int main(int argc, const char *argv[])
{
int fd = 0;
int count = 0;
int n = 0;
char buf[1024] = {0};
char *string = "hello";
int i = 0;
if(argc < 2)
{
fprintf(stderr,"Usage : %s argv[1]",argv[0]);
exit(EXIT_FAILURE);
}
//创建有名管道文件
if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
{
perror("Fail to create mkfifo");
exit(EXIT_FAILURE);
}
//打开有名管道文件
//fd = open(argv[1],O_RDONLY);
//fd = open(argv[1],O_WRONLY);
fd = open(argv[1],O_RDWR);
if(fd < 0)
{
perror("Fail to open");
exit(EXIT_FAILURE);
}
printf("open success!\n");
//把数据写到管道文件中
write(fd,string,strlen(string));
n = read(fd,buf,sizeof(buf));
printf("Read %d bytes : %s\n",n,buf);
close(fd);
return 0;
}
==================================
read_fifo.c
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
void do_read(int fd)
{
char buf[1024] = {0};
int n = 0;
while(1)
{
memset(buf,0,sizeof(buf));
n = read(fd,buf,sizeof(buf));
if(n == 0)
break;
printf("read %d bytes : %s\n",n,buf);
}
return ;
}
//./a.out fifoname
int main(int argc, const char *argv[])
{
int fd = 0;
char buf[1024] = {0};
int n = 0;
if(argc < 2)
{
fprintf(stderr,"Usage : %s argv[1]",argv[0]);
exit(EXIT_FAILURE);
}
//创建有名管道文件
if((mkfifo(argv[1],0666)) < 0 && errno != EEXIST)
{
perror("Fail to create mkfifo");
exit(EXIT_FAILURE);
}
//打开有名管道文件
fd = open(argv[1],O_RDONLY);
if(fd < 0)
{
perror("Fail to open");
exit(EXIT_FAILURE);
}
printf("open for read success!\n");
do_read(fd);
close(fd);
return 0;
}
write_fifo.c
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
void do_write(int fd)
{
char buf[1024] = {0};
int n = 0;
while(1)
{
printf(">");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf) - 1] = '\0';
if(strncmp(buf,"quit",4) == 0)
break;
n = write(fd,buf,strlen(buf));
printf("write %d bytes is success\n",n);
}
return ;
}
//./a.out fifoname
int main(int argc, const char *argv[])
{
int fd = 0;
int count = 0;
int n = 0;
char buf[1024] = {0};
if(argc < 2)
{
fprintf(stderr,"Usage : %s argv[1]",argv[0]);
exit(EXIT_FAILURE);
}
//创建有名管道文件
if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
{
perror("Fail to create mkfifo");
exit(EXIT_FAILURE);
}
//打开有名管道文件
fd = open(argv[1],O_WRONLY);
if(fd < 0)
{
perror("Fail to open");
exit(EXIT_FAILURE);
}
printf("open for write success!\n");
do_write(fd);
close(fd);
exit(EXIT_SUCCESS);
}
==============================================
talk
A.c
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
void do_read(int fd)
{
char buf[1024] = {0};
int n = 0;
while(1)
{
memset(buf,0,sizeof(buf));
n = read(fd,buf,sizeof(buf));
if(n == 0)
break;
printf("read %d bytes : %s\n",n,buf);
}
exit(EXIT_SUCCESS);
}
void do_write(int fd)
{
char buf[1024] = {0};
int n = 0;
while(1)
{
printf(">");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf) - 1] = '\0';
if(strncmp(buf,"quit",4) == 0)
break;
n = write(fd,buf,strlen(buf));
printf("write %d bytes is success\n",n);
}
exit(EXIT_SUCCESS);
}
//./a.out fifoname
int main(int argc, const char *argv[])
{
int fd1 = 0,fd2 = 0;
char buf[1024] = {0};
int n = 0;
pid_t pid = 0;
if(argc < 3)
{
fprintf(stderr,"Usage : %s argv[1]",argv[0]);
exit(EXIT_FAILURE);
}
//创建有名管道文件
if((mkfifo(argv[1],0666)) < 0 && errno != EEXIST)
{
perror("Fail to create mkfifo");
exit(EXIT_FAILURE);
}
if((mkfifo(argv[2],0666)) < 0 && errno != EEXIST)
{
perror("Fail to create mkfifo");
exit(EXIT_FAILURE);
}
pid = fork();
if(pid < 0)
{
perror("Fail to fork");
exit(EXIT_FAILURE);
}else if(pid > 0){
//打开有名管道文件
fd1 = open(argv[1],O_WRONLY);
if(fd1 < 0)
{
perror("Fail to open");
exit(EXIT_FAILURE);
}
do_write(fd1);
wait(NULL);
}else if(pid == 0){
fd2 = open(argv[2],O_RDONLY);
if(fd2 < 0)
{
perror("Fail to open");
exit(EXIT_FAILURE);
}
do_read(fd2);
}
close(fd1);
close(fd2);
return 0;
}
b.c
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
void do_read(int fd)
{
char buf[1024] = {0};
int n = 0;
while(1)
{
memset(buf,0,sizeof(buf));
n = read(fd,buf,sizeof(buf));
if(n == 0)
break;
printf("read %d bytes : %s\n",n,buf);
}
exit(EXIT_SUCCESS);
}
void do_write(int fd)
{
char buf[1024] = {0};
int n = 0;
while(1)
{
printf(">");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf) - 1] = '\0';
if(strncmp(buf,"quit",4) == 0)
break;
n = write(fd,buf,strlen(buf));
printf("write %d bytes is success\n",n);
}
exit(EXIT_SUCCESS);
}
//./a.out fifoname
int main(int argc, const char *argv[])
{
int fd1 = 0,fd2 = 0;
char buf[1024] = {0};
int n = 0;
pid_t pid = 0;
if(argc < 3)
{
fprintf(stderr,"Usage : %s argv[1]",argv[0]);
exit(EXIT_FAILURE);
}
//创建有名管道文件
if((mkfifo(argv[1],0666)) < 0 && errno != EEXIST)
{
perror("Fail to create mkfifo");
exit(EXIT_FAILURE);
}
if((mkfifo(argv[2],0666)) < 0 && errno != EEXIST)
{
perror("Fail to create mkfifo");
exit(EXIT_FAILURE);
}
pid = fork();
if(pid < 0)
{
perror("Fail to fork");
exit(EXIT_FAILURE);
}else if(pid > 0){
//打开有名管道文件
fd1 = open(argv[2],O_WRONLY);
if(fd1 < 0)
{
perror("Fail to open");
exit(EXIT_FAILURE);
}
do_write(fd1);
wait(NULL);
}else if(pid == 0){
fd2 = open(argv[1],O_RDONLY);
if(fd2 < 0)
{
perror("Fail to open");
exit(EXIT_FAILURE);
}
do_read(fd2);
}
close(fd1);
close(fd2);
return 0;
}
test.c
#include <stdio.h>
int main(int argc, const char *argv[])
{
int *p = NULL;
*p = 10;
printf("*p : %d\n",*p);
return 0;
}
hello.c
#include <stdio.h>
int main(int argc, const char *argv[])
{
printf("hello world\n");
return 0;
}