Sponsored Content
Full Discussion: Pipe & fifo....
Top Forums Programming Pipe & fifo.... Post 24912 by M3xican on Saturday 20th of July 2002 07:22:11 AM
Old 07-20-2002
Lightbulb the new code

The prolems are 2.
1) the lack of the ! into the (pid1=fork())
2) the lack of a wait or waitpid function after the else.

The output of your program is correct, but sometimes the first output line is wrote on the shell line, and
always the program waits an enter to terminate.

I have tested your code with a Linux Mandrake 8.2 .

This is your code with my changes:

Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

#define NUM_ARG 20      /* max number of args for any command  */

int main()
{
      char *arg1[NUM_ARG];
      char *arg2[NUM_ARG];
      pid_t pid1,pid2;
      int pipefd[2];
      
      arg1[0]="ls";   /*first command*/
      arg1[1]="-l";
      arg1[2]=0;
      
      arg2[0]="grep"; /*second command*/
      arg2[1]="mishell";
      arg2[2]=0;

      pipe(pipefd);
      if(!(pid1=fork()))     
      {
              dup2(pipefd[1],STDOUT_FILENO);
              close(pipefd[0]);
              execvp(arg1[0],arg1);
	      exit(0);
      }
      else
      {
	      waitpid(pid1,NULL,0);    		
	      dup2(pipefd[0],STDIN_FILENO);
              close(pipefd[1]);
              execvp(arg2[0],arg2);
	      exit(0);
      }
}

 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

PIPE and FIFO buffer size

Hello! How I can increase (or decrease) the predefined pipe buffer size? Thanks! (1 Reply)
Discussion started by: Jus
1 Replies

2. Shell Programming and Scripting

Reading from blocking fifo pipe in shell script

Hi!! I have a problem reading from a fifo pipe in shell script. The idea is simple, I have a C program with two pipe files: An input pipe I use to send commands in shell script to the C program (echo "command" > input.pipe) An output pipe that I read the result of the command also in... (4 Replies)
Discussion started by: victorin
4 Replies

3. UNIX for Dummies Questions & Answers

fifo or named pipe working?

Can someone explain to me the working of fifo() system call using simple C programs so that I can implement them in the UNIX environement? (1 Reply)
Discussion started by: lvkchaitanya
1 Replies

4. Programming

C++ How to use pipe() & fork() with stdin and stdout to another program

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question:... (2 Replies)
Discussion started by: vvaidyan
2 Replies

5. UNIX for Dummies Questions & Answers

Search for & edit rows & columns in data file and pipe

Dear unix gurus, I have a data file with header information about a subject and also 3 columns of n rows of data on various items he owns. The data file looks something like this: adam peter blah blah blah blah blah blah car 01 30 200 02 31 400 03 57 121 .. .. .. .. .. .. n y... (8 Replies)
Discussion started by: tintin72
8 Replies

6. UNIX for Advanced & Expert Users

Why not SIGPIPE for readers of pipe/FIFO?

Hi This is a exercise question from Unix network programming vol2. Why the SIGPIPE signal is generated only for writers when readers disappear. why not it is generated for readers when writer disappears. I guess, if the writer didn't get any response like the reader gets EOF, it will... (4 Replies)
Discussion started by: kumaran_5555
4 Replies

7. Programming

Pipe & fifo size limit

Hi guys. 1. how much is the size of pipe?(i mean the buffer size) 2. is this size different in various UNIX derivations? 3. what happens if we write to a full pipe? does it block until get some free space(the other side receive data) or returns an error? 3. FIFO s are physical files on the... (2 Replies)
Discussion started by: majid.merkava
2 Replies

8. Shell Programming and Scripting

awk reading from named pipe (fifo)

I'm trying to read a fifo using awk and comming across some problems. I'm writing to the fifo from multiple processes invoked by GNU Parallel: mkfifo my_fifo awk '{ a = a + $2 } END { for (i in a) print i, a }' my_fifo | sort -nk1 > sorted_output grep -v '^@' massive_file | parallel... (3 Replies)
Discussion started by: nathanhaigh
3 Replies

9. Shell Programming and Scripting

Named Pipe & Oracle imp

Hi, I have a little knowledge about mkfifo, first-in-first-out, a special file, a named pipe and it involves inter-process communication, as multiple processes can write data into a single file. Here, I would like to know how this is helpful in executing the below Oracle 'imp' command... (1 Reply)
Discussion started by: Dev_Dev
1 Replies

10. Shell Programming and Scripting

UNIX fifo concurrent read from a named pipe

I have created a fifo named pipe in solaris, which writes the content of a file, line by line, into pipe as below: $ mkfifo namepipe $ cat books.txt "how to write unix code" "how to write oracle code" $ cat books.txt >> namepipe & I have a readpipe.sh script which reads the named... (2 Replies)
Discussion started by: naveen mani
2 Replies
fifo(n) 																   fifo(n)

__________________________________________________________________________________________________________________________________________________

NAME
fifo - Create and manipulate u-turn fifo channels SYNOPSIS
package require Tcl package require memchan fifo _________________________________________________________________ DESCRIPTION
fifo creates a stream-oriented in-memory channel and returns its handle. There is no restriction on the ultimate size of the channel, it will always grow as much as is necessary to accomodate the data written into it. In contrast to the channels generated by memchan a channel created here effectively represents an U-turn. All data written into it can be read out, but only in the same order. This also means that a fifo channel is not seekable. The channels created here can be transfered between interpreters in the same thread and between threads, but only as a whole. It is not possible to use them to create a bi- or unidirectional connection between two interpreters. Memory channels created by fifo provide two read-only options which can be queried via the standard fconfigure command. These are -length The value of this option is the number of bytes currently stored in the queried memory channel. -allocated The value of this option is the number of bytes currently allocated by the queried memory channel. This number is at least as big as the value of -length. As the channels generated by fifo grow as necessary they are always writable. This means that a writable fileevent-handler will fire con- tinuously. The channels are also readable if they contain more than zero bytes. Under this conditions a readable fileevent-handler will fire continu- ously. NOTES
One possible application of memory channels created by memchan or fifo is as temporay storage device to collect data coming in over a pipe or a socket. If part of the processing of the incoming data is to read and process header bytes or similar fifo are easier to use as they do not require seeking back and forth to switch between the assimilation of headers at the beginning and writing new data at the end. SEE ALSO
fifo2, memchan, null KEYWORDS
channel, fifo, i/o, in-memory channel, memchan, stream COPYRIGHT
Copyright (c) 1996-2003 Andreas Kupries <andreas_kupries@users.sourceforge.net> Memory channels 2.1 fifo(n)
All times are GMT -4. The time now is 10:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy