14.6k views
1 vote
Two of the most fundamental functions for dealing with interprocess communication are read() and write(). Consider the following otherwise valid C program:

int r, pipeFDs[2];
char message[512];
pid_t spawnpid;

pipe(pipeFDs);
spawnpid = fork();

switch (spawnpid)
{
case 0:
close(pipeFDs[0]); // close the input file descriptor
write(pipeFDs[1], "hi process, this is the STUFF!!", 21);
break;

default:
close(pipeFDs[1]); // close output file descriptor
r = read(pipeFDs[0], message, sizeof(message));
printf("Message received from other: %s\\", message);
break;
}
Select each of the following answers that is correct.


a. The read() call may block until data becomes available
b. When the read() call returns, this one call will return all of the data that was sent through the pipe, which is different behavior than if this was a socket
c. If the read() call blocks, the process will be suspended until data arrives
d. The write() call will return before all of the data has been written, if the corresponding read() call blocks mid-transfer
e. Pipes can fill, which will cause the write() call to block until the read() call is able to read data from the pipe

User Darlyn
by
4.2k points

1 Answer

3 votes

Solution and Explanation:

"The read() call may block until data becomes available"

This is true for Blocking read, false if Non-Blocking read

There are 2 kinds of read() call

1)Blocking (Reader will block until some data is there for read)

2)Non-Blocking (Reader won't blocked, but ERROR status will be returned)

To make Non-Blocking we have to call

fcntl(pipeFDs[1], F_SETFL, O_NONBLOCK);

If Non-Blocking read() call won't block the process, Error status EAGAIN will be returned instead of number of bytes read.

If Blocking (By Default) , read() call will block the process until some bytes write by writer .

When the read() call returns, this one call will return all of the data that was sent through the pipe, which is different behavior than if this was a socket "This is false"

For Both Socket and Pipe Read will give bytes based on Buffer size specified.

IF More Bytes available than given buffer size, only bytes that can be fit it Buffer capacity will be copied to Buffer. Next Bytes will be given in next read() call

If Less or equal number of bytes to Buffer size is available, all will be copied to buffer

User Adhyatmik
by
4.8k points