FIFO problem[solved]


 
Thread Tools Search this Thread
Top Forums Programming FIFO problem[solved]
# 1  
Old 01-04-2012
FIFO problem[solved]

I wrote a simple program to have cat pipe its output into less through a FIFO. The write works fine, but the child won't exit after writing to the FIFO. Another program needs to read from the FIFO for it to exit, but the parent (less) must wait for the data to become available to read the FIFO. So essentially, I have a deadlock.

Here's the code:

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

int main( int argc, char **argv ){
        //char path[1024];
        //sprintf( path, "~/Desktop/doc/%s.nr", argv[1] );
        pid_t pid;
        char *file = argv[1];
        mkfifo( "named_pipe", 0777 );
        if( (pid = fork()) < 0 ){
                printf( "Fork error.\n" );
        }   
        else if( pid == 0 ){
                int fifo = open( "named_pipe", O_WRONLY );
                dup2( fifo, 1 ); // Attach stdout to the FIFO
                execlp( "cat", "cat", file );
                // Run cat and have it write to the FIFO
        }   
        else{
                wait( NULL ); // Wait for child to write to the FIFO
                int fifo = open( "named_pipe", O_RDONLY );
                dup2( fifo, 0 ); // Attach stdin to the FIFO
                execlp( "less", "less" );
                // Run less and have it read from the FIFO
        }   
        return 0;
}

I just want to know how I can have one program write to a FIFO through its standard output and the other program read from the FIFO through its standard input.

Also, I can't use anonymous pipes, due to losing control over I/O at the exec.

---------- Post updated at 06:11 PM ---------- Previous update was at 06:06 PM ----------

Problem solved. I changed the parent to call wait() after opening the FIFO on standard input.

Code:
int fifo = open( "named_pipe", O_RDONLY );
dup2( fifo, 0 ); // Attach stdin to the FIFO
wait( NULL ); // Wait for child to write to the FIFO
execlp( "less", "less" );
 // Run less and have it read from the FIFO

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Copy Problem

I facing a problem : I have a Source and Destination directory. The Source directory is linked to SVN and is updated by the Script to Head Revision. After that i copy the Source to Destination by this command: cp -r /SOURCE PATH /DESTIUNATION PATH Now if a delete a file in source and... (5 Replies)
Discussion started by: ankur328
5 Replies

2. Shell Programming and Scripting

[Solved] Problem with if-then-else loop

Hi, i have a problem with this script: for i in $(cat list_ip_switch) do if if ; then echo "found ip" else echo "not found ip" fi done cat list_ip_switch 10.155.249.171 10.155.249.172 (3 Replies)
Discussion started by: elilmal
3 Replies

3. Shell Programming and Scripting

[Solved] SFTP problem

Hi, I'm new to sh scripting and have the following problem. I have a script to sftp that is now working with ssh key. But i need to change this, i wan't to sftp with a password. I have the following script, tried a lot to make a variabel for the password. But i can't make it work ... (2 Replies)
Discussion started by: kvhindex
2 Replies

4. Shell Programming and Scripting

[solved] Problem with find

hi All, I am using the command find /home/mqm/check/ -mtime +1|wc -l to get a count of the files older than a day under the check directory. But instead of getting the count of the files under check directory I m getting a count including the check directory itself. Can someone please tell... (0 Replies)
Discussion started by: jayii
0 Replies

5. UNIX for Dummies Questions & Answers

[Solved] execution problem

Hi I want to make a script . In this script i want to use input file and this input file consist of three numbers in a line for example input file is as below: 919876543210 09876543234567876 98764534245678 aircelmms","aircelweb","aircelwap" 096574235625... (2 Replies)
Discussion started by: esumiba
2 Replies

6. Shell Programming and Scripting

[Solved] problem assigning value

Hi, This is the script that am trying to execute. a= sar 1 5 | grep ^A | awk '{print $5}' echo $a i am getting output. 99 i get a blank space for echo $a. Why is the value not getting assigned to a?? Thanks in Advance. How to use code tags (6 Replies)
Discussion started by: aksijain
6 Replies

7. UNIX for Dummies Questions & Answers

SOLVED: Join problem

Hello, Going through book, "Guide to UNIX Using Linux". I am doing one of the projects that has me writing scripts to join files. Here is my pnumname script and I am extracting the programmers names and numbers from the program file and redirecting the output to the file pnn. I then created a... (0 Replies)
Discussion started by: thebeav
0 Replies

8. Programming

Help With FIFO problem...

=server.c= #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <errno.h> #include <fcntl.h> #include <string.h> #include <signal.h> #define MSGSIZE 50 (1 Reply)
Discussion started by: sponge
1 Replies

9. AIX

Problem Solved

Generally, most people, I guess, go from 5.3 ML4 Directly to TL 7. So they may never run into this issue. For the rest of us, here is the resolution of my problem in going from ML6 to TL7. Apparently with the change from ML to TL IBM added a "BuildDate Verification" routine into... (1 Reply)
Discussion started by: mrmurdock
1 Replies

10. Filesystems, Disks and Memory

fifo deletion problem..

I have unix sco server. I have created one application for client server communication. On this I have creted some fifos/pipes. The reader.123 fifo is used by one process for reading and writing. I haven't deleted that fifo. But ls or find command doesn't show it. It is giving error as file or... (1 Reply)
Discussion started by: yogeshdimble
1 Replies
Login or Register to Ask a Question