LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-16-2007, 02:03 PM   #1
ineya
Member
 
Registered: Jul 2007
Posts: 39

Rep: Reputation: 16
Question Reading from FIFO file


I have a fifo file, where another process wrote some data. I want to read this data from this file in bash script I'm trying to write.

However, all calls like "cat", "more", "tail" or "cp" are blocking and waiting forever. (because as I understand it, FIFO file "has no end").

Any suggestion, how to solve this? (It is clear to me, that I could write some piece of code myself, but I'm wondering if there is already some useful utility I could use)

Thank you.
 
Old 07-16-2007, 03:05 PM   #2
Tux-Slack
Member
 
Registered: Nov 2006
Location: Slovenia
Distribution: Slackware 13.37
Posts: 511

Rep: Reputation: 37
FIFO does have and end of file. The problem would be if you would be reading data from that file and the other process would be still writing it. Then the end of file is "on the move". And as the other process is writing it it moves the end of file line by line down and therefor you can't reach the end.
This would be the same as if you would run
cat /dev/urandom > /some/file
and try to cat /some/file to the screen.
You would never reach the end untill you stoped cat /dev/urandom > /some/file
 
Old 07-17-2007, 12:11 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
The read process will block until the writing process closes its descriptor. That indicates the end of the data, or "EOF". When piping in a script without using "|" you generally have to set up the read operation in the background (e.g. cat mypipe &,) execute the write operation (e.g. ls -l > mypipe,) and then wait to make sure the background process is done before continuing.

IO blocking is a state set at the file descriptor level. In a C program you would change the default blocking to non-blocking with fcntl(file_descriptor, F_SETFL, O_NONBLOCK);. That's necessary for a lot of server-type programs. If you really need a version of cat that doesn't block, you could write one in just a few lines. Is it a time limit you are concerned with? Do you want it to only wait X number of seconds when no input is given?
Code:
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <string.h>


void read_loop(int fFile, double wWait)
{
	if (fFile < 0) return;

	double max_time = wWait, total_time = 0;
	struct timespec cycle_time = { 0, 50 * 1000 * 1000 };
	double add_time = (double) cycle_time.tv_sec + (double) cycle_time.tv_nsec / 1000000000.;

	char next_line[1024];

	FILE *input_file = fdopen(fFile, "r");

	while (total_time < max_time)
	{
	while (fgets(next_line, 1024, input_file))
	 {
	write(STDOUT_FILENO, next_line, strlen(next_line));
	total_time = 0;
	 }

	nanosleep(&cycle_time, NULL);
	total_time += add_time;
	}

	fclose(input_file);
}


int main(int argc, char *argv[])
{
	if (argc < 2)
	{
	fprintf(stderr, "%s [max time] (files...)\n", argv[0]);
	return 1;
	}

	int max_wait = strtoul(argv[1],0, 10);

	if (argc == 2)
	{
	fprintf(stderr, "%s: using standard input\n", argv[0]);
	fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
	read_loop(STDIN_FILENO, max_wait);
	return 0;
	}

	int current = 2;

	while (current < argc)
	{
	fprintf(stderr, "%s: switch to file '%s'\n", argv[0], argv[current]);
	int next_file = open(argv[current++], O_RDONLY | O_NONBLOCK);
	read_loop(next_file, max_wait);
	close(next_file);
	}

	return 0;
}
Here is my non-blocking version of cat. You give it a time in seconds to wait between valid inputs and if it times out then it will switch files until it runs out of them. Yes, I think I can solve all problems by throwing C code at them
ta0kira

Last edited by ta0kira; 07-17-2007 at 12:19 AM.
 
Old 07-17-2007, 01:22 AM   #4
ineya
Member
 
Registered: Jul 2007
Posts: 39

Original Poster
Rep: Reputation: 16
Thumbs up

I was prepared to write it by myself if necessary :-), but you saved me a lot of work.
Thank you.

I just had to add
Code:
#include <stdlib.h>
to compile this. My debian machine had trouble with "strtoul".

Quote:
Yes, I think I can solve all problems by throwing C code at them
You surely solved mine. ;-)
 
Old 07-17-2007, 11:19 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I did have that header in when I added the function, but then I looked at it when I was done and thought, "why is THAT in there?" After a quick look, I didn't see anything that needed it so I took it out, but it still managed to compile alright.
ta0kira
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
reading a number from fifo help gabyc4t Programming 5 05-15-2007 06:11 AM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM
Reading text file-writting binary file cdog Programming 5 06-13-2006 11:56 AM
awk: fatal:cannot open file for reading (no such file or Directory) in Linux sangati vishwanath Linux - Software 4 07-06-2005 12:59 AM
Script, Reading a file, When end of file? elibm Programming 2 07-16-2001 11:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration