|
|
1.1 root 1:
2:
3: pipe() COHERENT System Call pipe()
4:
5:
6:
7:
8: Open a pipe
9:
10: iinntt ppiippee(_f_d)
11: iinntt _f_d[22];
12:
13: A pipe is an interprocess communication mechanism. pipe creates
14: a pipe, typically to construct pipelines in the shell sh.
15:
16: pipe fills in fd[0] and fd[1] with read and write file descrip-
17: tors, respectively. The file descriptors allow the transfer of
18: data from one or more writers to one or more readers. Pipes are
19: buffered to 5,120 bytes. If more than 5,120 bytes are written
20: into the pipe, the write call will not return until the reader
21: has removed sufficient data for the write to complete. If a read
22: occurs on an empty pipe, its completion awaits the writing of
23: data.
24:
25: When all writing processe close their write file descriptors, the
26: reader receives an end of file indication. A write on a pipe
27: with no remaining readers generates a SIGPIPE signal to the
28: caller.
29:
30: pipe is generally called just before fork. Once the parent and
31: child processes are created, the unused file descriptors should
32: be closed in each process.
33:
34: ***** Example *****
35:
36: The following example prints the word Waiting until a line of
37: data is entered. It illustrates how to use pipe, fstat, and
38: fork.
39:
40:
41: #include <stdio.h>
42: #include <sys/stat.h>
43:
44:
45:
46: main()
47: {
48: struct stat s;
49: char buff[10];
50: int fd[2];
51:
52:
53:
54: if(pipe(fd) == -1) {
55: fprintf(stderr, "Cannot open pipe");
56: exit(1);
57: }
58:
59:
60:
61:
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
67:
68:
69: pipe() COHERENT System Call pipe()
70:
71:
72:
73: if(!fork()) { /* child process */
74: buff[0] = getchar(); /* wait for the keyboard */
75: write(fd[1], buff, 1); /* childs copy of buff */
76: exit(0);
77: }
78:
79:
80:
81: for(;;) { /* parent process. */
82: fstat(fd[0], &s);
83: if(s.st_size) { /* char in the pipe */
84: read(fd[0], buff, 1); /* parents copy */
85: printf("Got data\n");
86: exit(0);
87: }
88:
89:
90:
91: printf("Waiting\n");
92: }
93: }
94:
95:
96: ***** See Also *****
97:
98: close(), COHERENT system calls, mknod(), read(), sh, signal(),
99: write()
100:
101: ***** Diagnostics *****
102:
103: pipe returns zero on successful calls, or -1 if it could not
104: create the pipe.
105:
106: If it is necessary to create a pipe between tasks that are not
107: parent and child, use /etc/mknod to create a named pipe. These
108: named pipes can be opened and used by different programs for com-
109: munication. Remember to give them the correct owner and permis-
110: sions.
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130: COHERENT Lexicon Page 2
131:
132:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.