edited by
608 views
1 votes
1 votes
Q23. The following C program is executed on a Unix/Linux system:
 

main()
{
                 int i=0;
                 while (i<20)
                        if (i%2==0) fork();
                        i++;
                  return 0;
}
 

Calculate how many number of processes will be created after executing the above program.

Options:
1.1023
2.Infinite
3.1024
4.2048
edited by

2 Answers

Best answer
3 votes
3 votes

Option (2)
The provided C program initiates an indefinite generation of processes due to the absence of braces around the while loop, which leads to the exclusion of the i++ statement from the loop's scope. Consequently, the variable i remains unaltered, and the condition i < 20 persistently holds true, resulting in an infinite loop.
Similar to fork bomb :

`while(1) fork()`
The above snippet also creates infinite child processes

Within each iteration of the loop, the fork() function is invoked when i % 2 equals 0. Since i consistently remains zero, fork() gets executed in every iteration, spawning a new process each time. This incessant process creation continues until system resources become depleted, necessitating manual termination of the program.

This behavior mirrors that of a "fork bomb," a malicious construct that exploits the fork() system call to overwhelm system resources.

In contrast, consider the modified version of the program with a for loop:

C

main() {

for(int i = 0; i < 20; i++)

if(i % 2 == 0) fork();

return 0;

}

In this rendition, the fork() function is invoked exclusively during even iterations of the loop. This is due to the fact that, in this version, the value of i is incremented before the fork() call within the loop's scope so it get updated for each child process before making another child process.So here total 1024 processes will be created once code is executed (1023 new Child processes and 1 parent process).

Consequently, the original C code theoretically generate infinite child processes and, practically, as many processes as the system's memory resources permit.
So only option (2) is correct.

0 votes
0 votes

Answer is 1024
fork() will be called 10 times(i=0,2,4,6,8, 10, 12, 14, 16, 18)

so, Total number of process will be created after executing the above program is 2^10 = 1024.

Related questions

876
views
1 answers
1 votes
iarnav asked Oct 17, 2018
876 views
void forkexample() { // child process because return value zero if (fork() == 0) printf("Hello from Child!\n"); // parent process because return value non- ... () { forkexample(); return 0; } Please someone explain how things are working?
2.0k
views
2 answers
3 votes
iarnav asked Oct 18, 2018
2,016 views
A process executes the following segment of code :for(i = 1; i <= n; i++) fork (); fork ();The number of new processes created is
851
views
1 answers
0 votes
Erwin Smith asked Apr 11, 2023
851 views
void main() { int n = 1; if(fork()==0) { n = n<<1; printf(“%d, “, n); n = n <<1; } if(fork()==0) n=n+700; printf(“%d, “,n); }Which of the following output is not possible?2,4,1,701,7041,2,4,704,7012,704,4,701,11,704,2,4,701
1.2k
views
3 answers
1 votes
Sunnidhya Roy asked Dec 14, 2022
1,214 views
Given the following piece of code :Main(){Int child = fork();Int c = 5;if (child == 5){C + = 5;}Else{Child = fork();C + = 5;}}What are the ... Also I am getting the answer as 10,10,10. Given answer is 20,10,15.Can someone please check?