SmokinIndo
SmokinIndo

Reputation: 19

Determining possible output from a read() and fork() system call

I have a problem on my test review that is asking me to find five possible outputs of this code snippet.

mydata.txt contains "1234567890".

I keep trying to wrap my head around the sequences that is happening, but I can't create a concrete answer in my head.

Furthermore, I have to explain whether 1423 is a possible output, and explain why.

What I've figured out so far is that, since the fork() occurs after the open, that the parent and child process share the same file descriptor.

But I can't figure out all the possible outputs, and explain why 1423 isn't possible. I am hoping that someone here could help me out.

int fd;
char buf[5] = "wxyz";
fd = open("mydata.txt", O_RDONLY);
fork();
read(fd, buf, 1);
read(fd, buf+1, 1);
printf("%c%c", buf[0], buf[1]);

Upvotes: 0

Views: 100

Answers (1)

KamilCuk
KamilCuk

Reputation: 141698

After fork() you have two processes that I name A and B that execute these operations:

1) read(fd, buf, 1);
2) read(fd, buf+1, 1);
3) printf("%c%c", buf[0], buf[1]);

You can write all possible combinations. An example combination is:

  • A executes statement 1, thus reads 1 from the file and increases cursor pos in file
  • A executes statement 2, thus reads 2 from the file and increases cursor pos in file
  • A excites statement 3, ie. prints 12
  • B reads 3 from file
  • B reads 4 from file
  • B prints 34.

And so on. These statements may execute in any order, so first process B with first read, then process A with it's first read, then process B and so on.

We can observe that the processes will always read increasing numbers from the file. There are possibilities that:

  • a) The first process will read 12, then the second will read 34.
  • b) The first process will read 1, then the second will read 2, then the first will read 3, then the second will read 4
  • c) The first process can read 1, then the second will read 23, then the first will read 4

The "first process" and the "second process" is not process A or B, it does not matter, it's the first process that get's cpu time. The printfs from both processes also can run in any order. So we can now that all possible outputs are:

  • a)1) 1234
  • a)2) 3412
  • b)1) 1324
  • b)2) 2413
  • c)1) 1423
  • c)2) 2314

I am assuming that the output from processes is fully buffered, so a process either writes the full output of printf("%c%c", buf[0], buf[1]); or not. If the output is not buffered, then for example one process may print printf("%c", buf[0]) then the second may print it's buf[0], then the first will print it's buf[1], then the second's buf[1]. Then you will get many more combinations, as basically the statement printf("%c%c", buf[0], buf[1]) changes into two statements printf("%c", buf[0]) and printf("%c", buf[1]) that execute one after another in each process, but both processes can run them in any order.

The output 1423 is possible, if one of the processes reads 1 from the file, then the other reads 23 from the file, then the one of the processes reads 4 and printes 14, and then the other prints 23.

Upvotes: 0

Related Questions