|
[x]
Posted via EE Mobile
|
|
| Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again. |
|
|
|
|
Asked by FIsh in C Programming Language
I am working with unnamed and named pipes. I have already figured out the unnamed pipe, but have worked up a named pipe program, but it does not work. Could someone please tell me if I am even close.
I need the code to be able to use a named pipe function to have the output of the "ps -ef" command sent through the pipe to the "grep cm415b"command.
so: nova> ps -ef | grep cm415b
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:
|
/******************************************
* Class: CMIS 415
* Assignment: P3
* Author:
*Date created: 02 Nov 2009
*Date last modified:
*Comments: Implement an example of pipe-based communication between two UNIX
commands. The commands will be hardcoded inside the program to avoid parsing the
command line.
********************************************/
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
int main() {
int fd;
if (mkfifo("/tmp/fifo", cm415a) == -1) {
perror("error FIFO");
exit(1);
}
switch(fork()) {
case -1:
perror("creating process");
exit(1);
case 0:
close(1);
fd = open("/tmp/fifo", O_WRONLY);
if (fd == -1) {
perror("Opening FIFO for writing");
exit(1);
}
else if (fd != 1) {
fprintf(stderr, "Write descriptor failed");
exit(1);
}
execl("/usr/bin/grep", "grep", "cm415a", (char*) NULL);
perror("Running grep command");
exit(1);
default:
close(0);
fd = open("tmp/fifo", O_RDONLY);
if (fd == -1) {
perror("Opening FIFO for reading");
exit(1);
}
else if (fd != 0) {
fprintf(stderr, "Write descriptor failed");
exit(1);
}
execl("/bin/ps", "ps", "-ef", (char*) NULL);
perror("Running ps command");
exit(1);
}
}
|
20091111-EE-VQP-92 - Hierarchy / EE_QW_3_20080625