1.pipe
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(int argc,char *argv[]){
pid_t pid1;
int pipefd[2];
char buf;
if(pipe(pipefd)==-1){
perror("pipe");
exit(-1);
}
if((pid1=fork())<0){
perror("fork");
exit(-1);
}
if(pid1==0){
sleep(1);
close(pipefd[1]);
while(read(pipefd[0],&buf,1)>0)
write(STDOUT_FILENO,&buf,1);
write(STDOUT_FILENO,"\n",1);
close(pipefd[0]);
exit(0);
}
else {
close(pipefd[0]);
write(pipefd[1],argv[1],sizeof(argv[1]));
close(pipefd[1]);
wait(NULL);
exit(0);
}
}
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(int argc,char *argv[]){
pid_t pid1;
int pipefd[2];
char buf;
if(pipe(pipefd)==-1){
perror("pipe");
exit(-1);
}
if((pid1=fork())<0){
perror("fork");
exit(-1);
}
if(pid1==0){
sleep(1);
close(pipefd[1]);
while(read(pipefd[0],&buf,1)>0)
write(STDOUT_FILENO,&buf,1);
write(STDOUT_FILENO,"\n",1);
close(pipefd[0]);
exit(0);
}
else {
close(pipefd[0]);
write(pipefd[1],argv[1],sizeof(argv[1]));
close(pipefd[1]);
wait(NULL);
exit(0);
}
}
2.fifo
2.1 read
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char *argv[]){
int fd1;
char var[10];
remove("fifo1");
if(mkfifo("fifo1",0666)==-1){
perror("fifo");
exit(-1);
}
if((fd1 = open("fifo1",O_RDONLY))<0){
perror("open");
exit(-1);
}
if(read(fd1,var,10)<0){
perror("read");
exit(-1);
}
/*
if(read(fd1,var,10)==0){
printf("read ooo");
exit(0);
}
*/
printf("%s\n",var);
close(fd1);
return 0;
}
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char *argv[]){
int fd1;
char var[10];
remove("fifo1");
if(mkfifo("fifo1",0666)==-1){
perror("fifo");
exit(-1);
}
if((fd1 = open("fifo1",O_RDONLY))<0){
perror("open");
exit(-1);
}
if(read(fd1,var,10)<0){
perror("read");
exit(-1);
}
/*
if(read(fd1,var,10)==0){
printf("read ooo");
exit(0);
}
*/
printf("%s\n",var);
close(fd1);
return 0;
}
2.2 write
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char *argv[]){
int fd1;
char var[10];
if((fd1=open("fifo1",O_WRONLY))<0){
perror("openw");
exit(-1);
}
if(write(fd1,"abc\n",4)==-1){
perror("write");
exit(-1);
}
close(fd1);
return 0;
}
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char *argv[]){
int fd1;
char var[10];
if((fd1=open("fifo1",O_WRONLY))<0){
perror("openw");
exit(-1);
}
if(write(fd1,"abc\n",4)==-1){
perror("write");
exit(-1);
}
close(fd1);
return 0;
}