CS FundamentalsVery hardProduct company interview · GATE level
How many lines print the value 3, and what is the sum of all the values printed? (Assume every fork() succeeds.)
How many lines print the value 3, and what is the sum of all the values printed? (Assume every fork() succeeds.)
int main() {
int c = 0;
for (int i = 0; i < 4; i++)
if (fork() == 0)
c += i;
printf("%d\n", c);
}- A. 4 lines print 3; the sum is 48
- B. 2 lines print 3; the sum is 48
- C. 4 lines print 3; the sum is 24
- D. 1 line prints 3; the sum is 96
Correct answer: A. 4 lines print 3; the sum is 48
There are 2^4 = 16 processes, and each is identified by the set of iterations in which it was the child; its c is the sum of those i values. c = 3 for the sets {3}, {0, 3}, {1, 2} and {0, 1, 2} - note that being the child at i = 0 adds nothing - so 4 lines. Each i is in exactly half of the 16 sets, so the total is 8 x (0 + 1 + 2 + 3) = 48. 24 counts each i in only 4 processes; 2 forgets the sets that include i = 0.
More Operating Systems questions
- A buddy allocator manages 1024 KB and splits blocks in powers of two, always using the lowest-addressed free block of the needed size…
- A disk has cylinders 0-499. The head is at 237 and is moving towards cylinder 0. Pending requests: 312, 45, 401, 198, 260, 12, 477, 150…
- A disk spins at 12,000 RPM, has 250 sectors of 512 bytes per track, an average seek time of 4 ms and a track-to-track seek time of 0.5 ms…
- A machine has 40-bit virtual addresses, 16 KB pages and 4-byte page-table entries. Every page table at every level must fit in a single…
- A 32-bit system uses two-level paging: 10 bits for the outer index, 10 bits for the inner index and a 12-bit offset, with 4-byte entries…
- A 32-bit system uses two-level paging with a 10-bit outer index, a 10-bit inner index and a 12-bit offset. The virtual address 0xC0A8F3E7…