read from file using forks


 
Thread Tools Search this Thread
Top Forums Programming read from file using forks
# 1  
Old 03-18-2007
read from file using forks

i'm tring to make 2 processes each read from the same file but only one of them read the file.

Code:
FILE * fileptr1;
fileptr1 = fopen("file1.txt","rt");
pid2=fork();
while(1)		
{
	fscanf(fileptr1,"%s",temp1);
	if(feof(fileptr1)==0)
	{
		printf("%i",getpid());	//id of current process 
		printf(" Read: ");
		printf("%s",temp1);	//what was just read
		printf("\n");		
	}
	else
		break;
}
if(pid2==0)
fclose(fileptr1);			//close reading file

# 2  
Old 03-19-2007
# 3  
Old 03-19-2007
Try with read ( Unbuffered I/O )

The file is available to both the processes after the fork call.
So most likely one of the process reads the file & the file offset is
updated , and the other process gets an EOF .
So only one process prints the contents .
May get the desired result with read system call since you can control the
number of bytes to be read .
Or may try with setting the buffering options with setbuf function.
# 4  
Old 03-19-2007
i really can't use read do to the fact that i want it to read a whole line and all the lines aren't the same length. i got it to work using read but it didn't complete my objective to read line by line.

And i don't understand why one process gets an EOF. I thought they shared the same pointer?
# 5  
Old 03-20-2007
this is what i have now still no idea why is doesn't work

Code:
#include <stdio.h>
#include <fcntl.h>
main( )
{
	FILE *fileptr;
	char temp1[100];
	char buff2[20];
	int pid2;
	FILE * fileptr1;
	fileptr1 = fopen("file1.txt","rt");
	pid2=fork();
	printf("%i",getpid());
	printf("just created \n");
	while(1)		
	{
		printf("%i",getpid());
		printf("in the while loop\n");
		fscanf(fileptr1,"%s",temp1);
		if(feof(fileptr1)==0)
		{
			printf("%i",getpid());	//id of current process 
			printf(" Read: ");
			printf("%s",temp1);	//what was just read
			printf("\n");
			sleep(5);		
		}
	else
		break;
	}
}

this was the output
Code:
26017just created 
26017in the while loop
26017 Read: m+n
26016just created 
26016in the while loop
26017in the while loop
26017 Read: o+p
26017in the while loop
26017 Read: q+r+s
26017in the while loop
26017 Read: a+b+c+d
26017in the while loop

# 6  
Old 03-20-2007
I don't really have the foggiest idea of what you're actually trying to do here. But try this... open the file, then shut off buffering like this:
Code:
        fileptr1 = fopen("file1.txt","r");
        setvbuf(fileptr1, (char *)NULL, _IONBF, 0);

# 7  
Old 03-21-2007
omg it works... !!!! wow thanks. all i want was for 2 processes to read from the same file one line at a time
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shells, forks, subprocesses... oh my

all, i've been reading to try and get an abstract idea of the process effeciency of commands , sed, bash, perl, awk, find, grep, etc which processes will spawn?, fork?, launch subshell?, etc and under what conditions? how do you know which commands have the faster and better stdio... (2 Replies)
Discussion started by: f77hack
2 Replies

2. Shell Programming and Scripting

Read from file and execute the read command

Hi, I am facing issues with the below: I have a lookup file say lookup.lkp.This lookup.lkp file contains strings delimited by comma(,). Now i want to read this command from file and execute it. So my code below is : Contents in the lookup.lkp file is : c_e,m,a,`cd $BOX | ls cef_*|tail... (7 Replies)
Discussion started by: vital_parsley
7 Replies

3. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

4. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

5. Programming

Cannot read a file with read(fd, buffer, buffersize) function

# include <stdio.h> # include <fcntl.h> # include <stdlib.h> # include <sys/stat.h> int main(int argc, char *argv) { int fRead, fPadded, padVal; int btRead; int BUFFSIZE = 512; char buff; if (argc != 4) { printf ("Please provide all of the... (3 Replies)
Discussion started by: naranja18she
3 Replies

6. Programming

multiple forks and printf question

Hello *NIX gurus, I have a slight perplexing problem with multiple forks giving different results... Here is the deal. From what I undestand, a fork() call starts executing from the next instruction that follows the fork() call. That means it inherits the PC counter register value of the... (4 Replies)
Discussion started by: navigator
4 Replies

7. UNIX for Advanced & Expert Users

Question on forks and pipes

I am trying to figure out why when i have the following code int main( { printf("0\n"); fork(); printf("1\n"); exit(0);} and type in the shell a.out | cat the output of this program is 0 1 0 1 instead of 0 1 1 does anyone know? (3 Replies)
Discussion started by: Phantom12345
3 Replies

8. Programming

forks, ipc, fifos, update issues...

Hi, so I've got this program("main") that fork executes another ("user"). These programs communicate through fifos. One communication is a spawn call, where user passes an executable, main forks and executes it. So, I'm keeping track of all my processes using a task table. After the fork (for... (6 Replies)
Discussion started by: Funktar
6 Replies

9. UNIX for Advanced & Expert Users

forks....HELP!!! someone anyone?

Hey guys, I'm given this bit of code, but, I'm having some problems executing it with the functions I've defined so far. I'm suppose to define the funtions "parse" and "execute." Parse splits the command in buf into individual arguments. It strips whitespace, replacing those it finds with NULLS... (3 Replies)
Discussion started by: richardspence2
3 Replies
Login or Register to Ask a Question