recategorized by
1,484 views
0 votes
0 votes

How many times the word "PROCESS" will be printed when executing the following program ?

main(){
    printf("PROCESS");
    fflush();
    fork();
    fork();
}
  1. $8$
  2. $4$
  3. $6$
  4. $7$
recategorized by

2 Answers

Best answer
2 votes
2 votes
The options here are wrong. The word will be printed only once, which will be done by the parent or main thread. the fork() instructions are executed after printf so nothing will be printed by the child processes.
selected by
2 votes
2 votes
OPTION  B

The number of times "PROCESS" is printed is equal to number of process created. Total Number of Processes = $2^{n}$, where n is number of fork system calls. So here$ n = 2, 2^{2} = 4$

fflush() is typically used for output stream only. Its purpose is to clear (or flush) the output buffer and move the buffered data to console (in caseof stdout) or disk (in case of file output stream).

The two fork() calls create $3$ child processes, and hence "PROCESS" will be executed $4$ times if we don't use fflush.

If we put a '\n' at end of printf or use fflush(stdout); only $1$ printf will be done.
Answer:

Related questions

1 votes
1 votes
4 answers
1
admin asked Mar 31, 2020
2,158 views
If file size is large and if it is to be accessed randomly then which of the following allocation strategy should be best to use in a system?Linked allocationIndexed allo...
2 votes
2 votes
4 answers
2
admin asked Mar 31, 2020
17,328 views
Operating System maintains the page table for :each processeach threadeach instructioneach address
1 votes
1 votes
2 answers
3
admin asked Mar 31, 2020
2,480 views
Non-contiguous memory allocation splits program into blocks of memory called ________ that can be loaded in non-adjacent holes in main memory.PagesFramesPartitionSegments...
1 votes
1 votes
1 answer
4
admin asked Mar 31, 2020
1,618 views
What is Compaction ?a technique for overcoming internal fragmentationa paging techniquea technique for overcoming external fragmentationa technique for overcoming fatal e...