[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.

Question
[x]
Attachment Details

Named Pipes

Asked by FIsh in C Programming Language

Tags: named pipes, grep, ps -ef

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);
	}
}
[+][-]11/02/09 11:58 PM, ID: 25726805Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]11/03/09 06:23 AM, ID: 25729178Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/03/09 04:52 PM, ID: 25735497Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92 - Hierarchy / EE_QW_3_20080625