954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

if number of write's are more than the read's or vice-versa

Hi ,

what is the behaviour of below pseudo code.

pipe(fd)

if(fork()){
	read(fd[0]);
} else {
	write(fd[1]);
	write(fd[1]);
}


I understand how system calls work , but could not understand the behaviour when used differently.

secondly if a process uses while(1); in side its block, will the entire program struck there or it struck there as long as it is scheduled.


Thanks,

Gaiety
Junior Poster
120 posts since Sep 2009
Reputation Points: 13
Solved Threads: 3
 

If there are more reads than writes, any reads beyond the number of writes will fail.
If there are more writes than reads, nothing happens aside from a risk of lost data for the writes that weren't consumed.

deceptikon
Indubitably
Administrator
632 posts since Jan 2012
Reputation Points: 119
Solved Threads: 105
 

Thanks deceptikon for your answer.

Actually i tried to debug program involving fork and read and write.

the control is always entering if part only.

is there any way by which we can debug both child and parent proccess just the normal program.

Gaiety
Junior Poster
120 posts since Sep 2009
Reputation Points: 13
Solved Threads: 3
 

fork returns two values ,0 for child process, and pid of child for parent process. Hence , your if is always true and it runs for parent. If you want some code to run for child you will have to check

if(fork() == 0){
// your code
}
DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 
is there any way by which we can debug both child and parent proccess just the normal program.


Assuming you're debugging with gdb: http://www.delorie.com/gnu/docs/gdb/gdb_26.html

fork returns two values ,0 for child process, and pid of child for parent process. Hence , your if is always true and it runs for parent. If you want some code to run for child you will have to check

if(fork() == 0){
// your code
}


All you did was reverse the test. if (fork()) is equivalent to if (fork() != 0) .

deceptikon
Indubitably
Administrator
632 posts since Jan 2012
Reputation Points: 119
Solved Threads: 105
 

ohh i did not know this

Assuming you're debugging with gdb: http://www.delorie.com/gnu/docs/gdb/gdb_26.html

Many Thanks

Gaiety
Junior Poster
120 posts since Sep 2009
Reputation Points: 13
Solved Threads: 3
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You