The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
fifo or named pipe working? lvkchaitanya UNIX for Dummies Questions & Answers 1 02-06-2008 12:44 PM
Reading from blocking fifo pipe in shell script victorin Shell Programming and Scripting 4 05-08-2007 11:39 AM
how to use fifo atticus High Level Programming 3 06-05-2006 10:15 AM
PIPE and FIFO buffer size Jus UNIX for Advanced & Expert Users 1 08-20-2004 03:53 PM
FIFO over NFS saabir UNIX for Advanced & Expert Users 2 08-06-2003 08:03 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-17-2002
M3xican M3xican is offline
Registered User
  
 

Join Date: Jul 2002
Location: Italy - Naples
Posts: 6
Question Pipe & fifo....

Could someone Help me with this code please?

Code:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>

#define SIZE_B 256      /*buffer's size */
#define NUM_ARG 20      /* max number of args for any command  */

int main()
{
	char *arg1[NUM_ARG];
	char *arg2[NUM_ARG];
	int pippa;	
	pid_t pid1,pid2;
	
	arg1[0]="ls";	/*first command*/
	arg1[1]="-l";
	arg1[2]=0;
	
	arg2[0]="grep";	/*second command*/
	arg2[1]="mishell";
	arg2[2]=0;
	
	mkfifo("./mishell_pipa",S_IRWXU);       /*make fifo*/

        pippa=open("./mishell_pipa",O_RDWR|O_CREAT|O_TRUNC|O_SYNC);    /*get fd*/   

	unlink("./mishell_pipa");       /*unlink file*/

	if(!(pid1=vfork()))	/*first process*/
	{
		dup2(pippa,STDOUT_FILENO);
		execvp(arg1[0],arg1);
		_exit(0);
	}
	else
	{
		waitpid(pid1,NULL,0);	/*wait first process*/
			
		if(!(pid2=vfork()))	/*second process*/
		{
			dup2(pippa,STDIN_FILENO);
			execvp(arg2[0],arg2);
			_exit(0);
		}
		else
			waitpid(pid2,NULL,0);	/*wait second process*/
	return 0;
	}
	
}
I have to create a pipe between 2 process such as the output of the first process
is the input of the second.
In practice I have to do the same thing that the bash does using the follow command:

ls -l|grep mishell

The program works well, but after that grep gives me its output it doesn't come back to
the shell, but stays blocked.

I have tried also to use the mode O_NONBLOCK in the open function, but so the first problem leaves
its place to another. The programm works well, but at the end grep gives me this error:

grep: (standard input): Resource temporarily unavailable

I think that the problem is that grep attend a particular kind of input to terminate, and ls doesn't
give it this input. I've tried to write a EOF in the fifo after the execution of ls, but there
aren't changes.

What can I do????
Help me please!
  #2 (permalink)  
Old 07-18-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
If you're going to use a named pipe you would need to open it twice. But you should just use a pipe. Your first process will not create your third process until after the second process finishes. But the second process would be waiting for the third process to read its data. This is a deadlock. You only need two processes anyway. And don't use vfork() anymore, use fork().

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);
      }
      else
      {
              dup2(pipefd[0],STDIN_FILENO);
              close(pipefd[1]);
              execvp(arg2[0],arg2);
      }
}
  #3 (permalink)  
Old 07-19-2002
M3xican M3xican is offline
Registered User
  
 

Join Date: Jul 2002
Location: Italy - Naples
Posts: 6
Thumbs up Thanx

Thanx Perderabo, I have solved my problem using as pipe as fifo thanks your suggestion of open
the pipe twice.

I have modifyed your source because your line:
Code:
if(pid1=fork())
was incorrect.
I hope that you don't judge me presumptuos, but the lack of the ! was harmful for the program. ;P
  #4 (permalink)  
Old 07-19-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
I tested it before I posted. I just retested and cannot get my code to fail. What problem did you see? What version of unix are you using?
  #5 (permalink)  
Old 07-20-2002
M3xican M3xican is offline
Registered User
  
 

Join Date: Jul 2002
Location: Italy - Naples
Posts: 6
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);
      }
}
Sponsored Links
Closed Thread

Bookmarks

Tags
linux mandrake

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0