就是不写 readme !
涉及知识点:
- 有名管道
- 进程通信
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main() {
int fd, fd1, fd2;
char buf[1024];
if (mkfifo("./hello.txt", 0777) < 0) {
perror("failed to open the FIFO mkfifo");
return 1;
}
if ((fd1 = open("./hello.txt", O_CREAT|O_RDWR)) == -1) {
perror("failed to open the FIFO hello.txt");
return 1;
}
if ((fd = open("./19407010204.txt", O_RDWR)) == -1) {
perror("failed to open the FIFO 1911");
return 1;
}
while (1) {
read(fd, buf, sizeof(buf)); // buf
printf("read content is : %s\n", buf);
write(fd1, buf, sizeof(buf));
printf("write pipe: %s\n", buf);
sleep(10);
}
close(fd);
close(fd1);
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main() {
int fd, fd1, fd2;
char buf[1024], buf2[1024];
fd = open("./hello.txt", O_RDONLY);
if (fd < 0) {
printf("err!!");
return -1;
}
while (1) {
read(fd, buf, sizeof(buf)); // buf
printf("read content is : %s\n", buf);
sleep(10);
}
close(fd);
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main() {
int fd;
char s[] = "This is a test programmer of process & IPC";
if ((fd = open("./19407010204.txt", O_CREAT|O_RDWR,0666)) == -1) {
perror("failed to open the FIFO");
return 1;
}
while (1) {
write(fd, s, sizeof(s));
printf("Write: %s\n", s);
sleep(3);
}
close(fd);
return 0;
}