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,

Recommended Answers

All 5 Replies

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.

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.

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
}

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) .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.