2 Replies - 6996 Views - Last Post: 27 March 2010 - 03:25 PM Rate Topic: -----

#1 DamienCurr  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 69
  • Joined: 03-May 09

Unnamed vs. named pipes...

Posted 26 March 2010 - 07:53 AM

Good morning all,

So. This morning I am trying to implement two examples of pipe-based communication between two UNIX commands; one example using an unnamed pipe and the other a named pipe. For example, "$ ps -ef | grep ct321j". Of course, UNIX does this itself from the command line; the goal is to do the same thing in a C program.

I will use fork and exec and then a pipe to connect the two processes. I will redirect the standard output and input to the pipe, in each example.

I am looking for some really good tutorials or examples that I can reference in order to assist me in this endeavor. Any and all suggestions are welcome!

Thank you

Damien

Is This A Good Question/Topic? 0
  • +

Replies To: Unnamed vs. named pipes...

#2 DamienCurr  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 69
  • Joined: 03-May 09

Re: Unnamed vs. named pipes...

Posted 27 March 2010 - 12:57 PM

Okay,

I've read everything I could get my hands on about pipes (named and unnamed), forking and using the exec system calls. I've searched the Internet for two days on anything pertaining to the above. I have no code to post, because I do not know where to begin. I know that I need to use: fork(), pipe(), execl(), and dup2(). I'm not supposed to use "system" and "popen" or similar calls.

I am not sure which to start with, the pipe and then fork or vice versa. Am I suppose to start the exec system call during the child or parent process? Are there any C program templates out there that can be used for reference?

Some direction would be appreciated.

Thanks,

Damien
Was This Post Helpful? 0
  • +
  • -

#3 DamienCurr  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 69
  • Joined: 03-May 09

Re: Unnamed vs. named pipes...

Posted 27 March 2010 - 03:25 PM

Okay,

After continuing to work on this all day (reading, looking things up on the Internet, etc.) I have the following code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

int main(int argc, char *argv[]){

int fd[2];
pid_t pid;

pipe(fd);						/* create a pipe */

pid = fork();					/* fork */
	if(pid < 0){
		printf("Cannot fork");
		exit(1);
	}
	if(pid > 0){
		/* parent process */
		dup2(fd[1], 1);			/* Replace stdout with out side of the pipe */
		close(fd[0]);			/* Close unused side of pipe (in side) */
		execlp("ps", "ps", "-ef", (char *)0);
	}
	else if(pid == 0){
		/* child process */
		dup2(fd[0], 0);			/* Replace stdin with the in side of the pipe */
		close(fd[1]);			/* Close unused side of pipe (out side) */
		execlp("grep", "grep", "ct321j", (char *)0);
	}

}



Does this make sense to anyone, based off of what I am being asked to do? Suggestions are welcome!

Thanks,
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1