复制代码
1
2管道和进程的创建
(1)管道的创建
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include<stdlib.h> #include<sys/stat.h> #include<string.h> #include<errno.h> int main()(int argc,char **argv) if(argc<2) { fprintf(stdout,"Usage:%s<filename>n",argv[0]; exit(1); } //int mkfifo(const char *path,mode_t mode); if(mkfifo(argv[1],0644)<0) { fprintf(stderr,"mkfifo() failed:%sn",strerror(errno)); exit(1); } return 0; }
复制代码
1
2
3这几行代码可以创建一个管道 **(2)写进程的创建**
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/stat.h> #include<fcntl.h> #include<string.h> #include<errno.h> int main(int argc,char *argv[]) { int fd; char str[1024]; ssize_t n; if(argc<2){ printf("Please input 'fifo name' after %sn",argv[0]; exit(1); } mkfifo(argv[1],0644); if((fd=open(argv[1],O_WRONLY))<0){ printf("Opening Write_fifo Failed.n"); exit(1); } printf("Write_fifo is ready.n"); printf("Waitting message....nn"); while(1){ printf("Please input:n"); fgets(str,sizeof(str),stdin); write(fd,dtr,strlrn(str)+1); if(!strcmp(str,":qn")){ printf("Write_fifo closed.n"); break; } } close(fd); return 0; }
复制代码
1
2此为写进程的创建。
(3)读进程的创建。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include <errno.h> int search(char *str1, char *str2); int count = 0; int main(int argc, char *argv[]) { int fd1, fd2; char str1[1024], str2[1024]; ssize_t n; if (argc < 2) { printf("Please input 'fifo name' after %sn", argv[0]); exit(1); } if ((fd1 = open(argv[1], O_RDONLY)) < 0) { printf("Opening Read_fifo failed.n"); exit(1); } if ((fd2 = open("./target.txt", O_RDONLY)) < 0) { printf("Opening Search_file failed.n"); exit(1); } lseek(fd2, 0, SEEK_SET); printf("Write_fifo is ready.n"); printf("Waiting message......nn"); while (read(fd1, str1, sizeof(str1)) > 0) { if (!strcmp(str1, ":qn")) { printf("Read_fifo closed.n"); break; } str1[strlen(str1) - 1] = ''; printf("Your message is: %sn", str1); printf("Now searching '%s' in 'target.txt' ....n", str1); while(1) { n = read(fd2, str2, sizeof(str2)); if (n < 1024) { search(str2, str1); lseek(fd2, 0, SEEK_SET); break; } if (n == sizeof(str2)) search(str2, str1); } printf("Appeared %d times!n", count); count = 0; } close(fd1); close(fd2); return 0; } int search(char *str1, char *str2) { char *temp; while (strlen(str1)) { temp = strstr(str1, str2); if (temp == NULL) break; count += 1; temp = &temp[strlen(str2)]; str1 = temp; } return 0; }
最后
以上就是欢喜猫咪最近收集整理的关于Linux管道的操作的全部内容,更多相关Linux管道内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复