in Operating System
2,282 views
8 votes
8 votes
main()
{
    if(fork()>=0)
    {
        printf("*");
        if(fork()==0)
        {
            printf("*");
    }
    else{
        //do nothing
    }
    printf("*");
}

How many number of times “*” will be printed?

 

in Operating System
by
2.3k views

4 Comments

@srestha can you check the question once..the parenthesis are not balanced...

1
1
which one not balanced?
0
0
  1. main()
  2. {
  3.      if(fork()>=0)$---$ this if condition's closing bracket is not there..
  4.      {
  5.            printf("*");
  6.            if(fork()==0)
  7.            {
  8.             printf("*");
  9.             }
  10.             else
  11.             {
  12.               //do nothing
  13.              }
  14.             printf("*");
  15. }
0
0

4 Answers

10 votes
10 votes
Best answer
01.main()
02.{
03.    if(fork()>=0)
04.    {
05.        printf("*");
06.        if(fork()==0)
07.        {
08.            printf("*");
09.        }
10.        else{
11.            //do nothing
12.        }
13.        printf("*");
14.    }
15.}

Our execution is one process, So at line 1, number of processes = 1

at line 3, due to fork() statement, one more child process will be created, So at line number 3, number of processes = 2.

BASIC POINT in the view of fork(), is child process return 0, and parent process return a positive value when fork() applied.

in the line 3 :- the condition is ≥ 0 , so both P1 (parent) and P2(child) enter into if block.

line number 5 can be executed by P1 and P2 ===> No.of * prints till now = 2.

 

when P1 executes the line number 6, one more child P3, created.

But note that, condition is fork() == 0, So only P3 can enter into the line 7. but not P1

 

when P2 executes the line number 6, one more child P4, created.

But note that, condition is fork() == 0, So only P4 can enter into the line 7. but not P2.

 

So, line number 8 can be executed by P3 and P4 ===> No.of * prints till now = 2+2 = 4

 

All these 4 processes, P1,P2,P3,P4 can executes the line number 13.

So, line number 13 can be executed by All ===> No.of * prints till now = 4+4 = 8.

 

more problems on fork() concept :- https://gateoverflow.in/302831/gate2019-17?show=303821#c303821

selected by

4 Comments

yes, ur explanation is good, no doubt :)

but let me hop on other questions too like this
0
0

                                          P1

                                    /            \

                                P1=1         P2=0

        (Print"*")       (Print"*")

                        /              \              /           \

               P1=1               P3=0    P2=1         P4=0

                             (Print"*")      (Print"*")

       (Print"*") (Print"*") (Print"*")   (Print"*")

@Shaik Masthan  This is the tree structure of ur ans,

right?

0
0
Thanx for brief explanation.
0
0
5 votes
5 votes

Answer: 8 times ' * ' will print

and total, 3 child process will create

1 vote
1 vote

Ans : 8

Explaination through tree Structure

0 votes
0 votes
​​​​​​8 times * is printed

Related questions

3 votes
3 votes
1 answer
1