1.2管道的創建
解釋如下 :
-
#include <stdio.h>
-
#include <unistd.h>
-
#include <stdlib.h>
-
-
int main()
-
{
-
int n;
-
int fd[2];
-
int count = 0;
-
char buf[100] = {0};
-
-
if(pipe(fd) < 0)
-
{
-
perror("Fail to create pipe");
-
exit(EXIT_FAILURE);
-
}
-
-
close(fd[1]);
-
-
if((n = read(fd[0],buf,sizeof(buf))) < 0)
-
{
-
perror("Fail to read pipe");
-
exit(EXIT_FAILURE);
-
}
-
-
printf("Rread %d bytes : %s.\n",n,buf);
-
-
return 0;
- }
運行結果:
點擊(此處)摺疊或打開
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <errno.h>
-
#include <string.h>
-
-
#define N 10
-
#define MAX 100
-
-
int child_read_pipe(int fd)
-
{
-
char buf[N];
-
int n = 0;
-
-
while(1)
-
{
-
n = read(fd,buf,sizeof(buf));
-
buf[n] = '\0';
-
-
printf("Read %d bytes : %s.\n",n,buf);
-
-
if(strncmp(buf,"quit",4) == 0)
-
break;
-
}
-
-
return 0;
-
}
-
-
int father_write_pipe(int fd)
-
{
-
char buf[MAX] = {0};
-
-
while(1)
-
{
-
printf(">");
-
fgets(buf,sizeof(buf),stdin);
-
buf[strlen(buf)-1] = '\0';
-
write(fd,buf,strlen(buf));
-
usleep(500);
-
if(strncmp(buf,"quit",4) == 0)
-
break;
-
}
-
-
return 0;
-
}
-
-
int main()
-
{
-
int pid;
-
int fd[2];
-
-
if(pipe(fd) < 0)
-
{
-
perror("Fail to pipe");
-
exit(EXIT_FAILURE);
-
}
-
-
if((pid = fork()) < 0)
-
{
-
perror("Fail to fork");
-
exit(EXIT_FAILURE);
-
-
}else if(pid == 0){
-
-
close(fd[1]);
-
child_read_pipe(fd[0]);
-
-
}else{
-
-
close(fd[0]);
-
father_write_pipe(fd[1]);
-
}
-
-
exit(EXIT_SUCCESS);
- }
運行結果:
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <errno.h>
-
#include <string.h>
-
-
int main()
-
{
-
int pid;
-
int n;
-
int fd[2];
-
char buf[1000 * 6] = {0};
-
-
if(pipe(fd) < 0)
-
{
-
perror("Fail to pipe");
-
exit(EXIT_FAILURE);
-
}
-
-
if((pid = fork()) < 0)
-
{
-
perror("Fail to fork");
-
exit(EXIT_FAILURE);
-
-
}else if(pid == 0){
-
-
close(fd[1]);
-
sleep(5);
-
close(fd[0]);
-
printf("Read port close.\n");
-
sleep(3);
-
-
}else{
-
-
close(fd[0]);
-
-
while(1)
-
{
-
n = write(fd[1],buf,sizeof(buf));
-
printf("Write %d bytes to pipe.\n",n);
-
}
-
-
}
-
-
exit(EXIT_SUCCESS);
- }
運行結果:
點擊(此處)摺疊或打開
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <errno.h>
-
#include <sys/stat.h>
-
#include <sys/types.h>
-
#include <fcntl.h>
-
-
#define MAX 100
-
-
int child_work(int pfd,char *fname)
-
{
-
int n,fd;
-
char buf[MAX];
-
-
if((fd = open(fname,O_WRONLY | O_CREAT | O_TRUNC,0666)) < 0)
-
{
-
fprintf(stderr,"Fail to open %s : %s.\n",fname,strerror(errno));
-
return -1;
-
}
-
-
while( n = read(pfd,buf,sizeof(buf)) )
-
{
-
write(fd,buf,n);
-
}
-
-
close(pfd);
-
-
return 0;
-
}
-
-
int father_work(int pfd,char *fname)
-
{
-
int fd,n;
-
char buf[MAX];
-
-
if((fd = open(fname,O_RDONLY)) < 0)
-
{
-
fprintf(stderr,"Fail to open %s : %s.\n",fname,strerror(errno));
-
return -1;
-
}
-
-
while(n = read(fd,buf,sizeof(buf)))
-
{
-
write(pfd,buf,n);
-
}
-
-
close(pfd);
-
-
return 0;
-
}
-
-
int main(int argc,char *argv[])
-
{
-
int pid;
-
int fd[2];
-
-
if(argc < 3)
-
{
-
fprintf(stderr,"usage %s argv[1] argv[2].\n",argv[0]);
-
exit(EXIT_FAILURE);
-
}
-
-
if(pipe(fd) < 0)
-
{
-
perror("Fail to pipe");
-
exit(EXIT_FAILURE);
-
}
-
-
if((pid = fork()) < 0)
-
{
-
perror("Fail to fork");
-
exit(EXIT_FAILURE);
-
-
}else if(pid == 0){
-
-
close(fd[1]);
-
child_work(fd[0],argv[2]);
-
-
}else{
-
-
close(fd[0]);
-
father_work(fd[1],argv[1]);
-
wait(NULL);
-
}
-
-
exit(EXIT_SUCCESS);
- }
點擊(此處)摺疊或打開
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <errno.h>
-
#include <sys/types.h>
-
#include <sys/stat.h>
-
#include <fcntl.h>
-
-
int main(int argc,char *argv[])
-
{
-
int fd;
-
-
if(argc < 2)
-
{
-
fprintf(stderr,"usage : %s argv[1].\n",argv[0]);
-
exit(EXIT_FAILURE);
-
}
-
-
if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
-
{
-
fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno));
-
exit(EXIT_FAILURE);
-
}
-
-
if((fd = open(argv[1],O_WRONLY)) < 0)
-
{
-
fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno));
-
exit(EXIT_FAILURE);
-
}
-
-
printf("open for write success.\n");
-
-
return 0;
- }
B.open for read
點擊(此處)摺疊或打開
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <errno.h>
-
#include <sys/types.h>
-
#include <sys/stat.h>
-
#include <fcntl.h>
-
-
int main(int argc,char *argv[])
-
{
-
int fd;
-
-
if(argc < 2)
-
{
-
fprintf(stderr,"usage : %s argv[1].\n",argv[0]);
-
exit(EXIT_FAILURE);
-
}
-
-
if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
-
{
-
fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno));
-
exit(EXIT_FAILURE);
-
}
-
-
if((fd = open(argv[1],O_RDONLY)) < 0)
-
{
-
fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno));
-
exit(EXIT_FAILURE);
-
}
-
-
printf("open for read success.\n");
-
-
return 0;
- }
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <errno.h>
-
#include <sys/types.h>
-
#include <sys/stat.h>
-
#include <fcntl.h>
-
-
#define MAX 655360
-
-
int main(int argc,char *argv[])
-
{
-
int n,fd;
-
char buf[MAX];
-
-
if(argc < 2)
-
{
-
fprintf(stderr,"usage : %s argv[1].\n",argv[0]);
-
exit(EXIT_FAILURE);
-
}
-
-
if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
-
{
-
fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno));
-
exit(EXIT_FAILURE);
-
}
-
-
if((fd = open(argv[1],O_WRONLY )) < 0)
-
{
-
fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno));
-
exit(EXIT_FAILURE);
-
}
-
-
printf("open for write success.\n");
-
-
while(1)
-
{
-
printf(">");
-
scanf("%d",&n);
-
-
n = write(fd,buf,n);
-
printf("write %d bytes.\n",n);
-
}
-
-
exit(EXIT_SUCCESS);
- }
點擊(此處)摺疊或打開
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <errno.h>
-
#include <sys/types.h>
-
#include <sys/stat.h>
-
#include <fcntl.h>
-
-
#define MAX 655360
-
-
int main(int argc,char *argv[])
-
{
-
int fd,n;
-
char buf[MAX];
-
-
if(argc < 2)
-
{
-
fprintf(stderr,"usage : %s argv[1].\n",argv[0]);
-
exit(EXIT_FAILURE);
-
}
-
-
if(mkfifo(argv[1],0666) < 0 && errno != EEXIST)
-
{
-
fprintf(stderr,"Fail to mkfifo %s : %s.\n",argv[1],strerror(errno));
-
exit(EXIT_FAILURE);
-
}
-
-
if((fd = open(argv[1],O_RDONLY )) < 0)
-
{
-
fprintf(stderr,"Fail to open %s : %s.\n",argv[1],strerror(errno));
-
exit(EXIT_FAILURE);
-
}
-
-
printf("open for read success.\n");
-
-
while(1)
-
{
-
printf(">");
-
scanf("%d",&n);
-
-
n = read(fd,buf,n);
-
-
printf("Read %d bytes.\n",n);
-
-
}
-
-
exit(EXIT_SUCCESS);
- }
讀者可以將這兩個程序運行,然後輸入read和write FIFO大小就可以看到效果。