Interview Question

• 0

result of this:

``````main()
{
fork();
fork();
fork();

printf("hello\n");
}``````

Comment hidden because of low score. Click to expand.
4
of 4 vote

For Such Cases.We can Calculate the Output by one Formula

(2^(n) -1) + 1

I know Above expression is equal to 2^n then..I am trying to Explain tat (2^(n) -1) Child Process would be there and 1 is for Parent Process.

So 8 times Hello is getting printed
7 + 1 (7 Times by Child Process and 1 time by its parent)=8

Plz ignore Spelling and Grammar Mistakes

Comment hidden because of low score. Click to expand.
0

Wow!! Looks like you put your solution in the code, instead of the print statement! =)

Comment hidden because of low score. Click to expand.
0

BTW, I'd say any combination of 8 sets of 'h' 'e' 'l' 'l' 'o' characters could be printed. The processes are not synchronized.

Comment hidden because of low score. Click to expand.
2
of 2 vote

8 "hello" statements will be printed. Its 2^n. In this case 2^3.

Comment hidden because of low score. Click to expand.
1
of 1 vote

For Such Cases.We can Calculate the Output by one Formula

(2^(n) -1) + 1

I know Above expression is equal to 2^n then..I am trying to Explain tat (2^(n) -1) Child Process would be there and 1 is for Parent Process.

So 8 times Hello is getting printed
7 + 1 (7 Times by Child Process and 1 time by its parent)=8

Plz ignore Spelling and Grammar Mistakes

Comment hidden because of low score. Click to expand.
0
of 0 vote

Explanation????

Comment hidden because of low score. Click to expand.
0
of 0 vote

{f -fork, p - print

f ->f -> f ->p
f f p
f p ->p
p ->p
|
f ->p
p}

Comment hidden because of low score. Click to expand.
0

sorry for the poor post
1) the parent invokes ffp (i), fp (ii), p
2) Now (i) invokes fp(iii),p
3) (ii) invokes p
4) (iii) invokes another p
There are 7 p's plus the parent prints once. So totally there are 8 "hellos" printed. Forking creates a child process and the excecution of the child process begins following the fork that is used to invoke it. So the output changes if the print statement position changes.

Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
int pid;
if(!fork()) {
printf("Hello world\n");
if(!fork()) {
printf("Hello world\n");
if(!fork()) {
printf("Hello world\n");
}
else
printf("Hello world\n");
}
else
printf("Hello world\n");
}
else
printf("Hello world\n");
return 0;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
int pid;
if(!fork()) {
printf("Hello world\n");
if(!fork()) {
printf("Hello world\n");
if(!fork()) {
printf("Hello world\n");
}
else
printf("Hello world\n");
}
else
printf("Hello world\n");
}
else
printf("Hello world\n");
return 0;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
int pid;
if(!fork()) {
printf("Hello world\n");
if(!fork()) {
printf("Hello world\n");
if(!fork()) {
printf("Hello world\n");
}
else
printf("Hello world\n");
}
else
printf("Hello world\n");
}
else
printf("Hello world\n");
return 0;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
int pid;
if(!fork()) {
printf("Hello world\n");
if(!fork()) {
printf("Hello world\n");
if(!fork()) {
printf("Hello world\n");
}
else
printf("Hello world\n");
}
else
printf("Hello world\n");
}
else
printf("Hello world\n");
return 0;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
int pid;
if(!fork()) {
printf("Hello world\n");
if(!fork()) {
printf("Hello world\n");
if(!fork()) {
printf("Hello world\n");
}
else
printf("Hello world\n");
}
else
printf("Hello world\n");
}
else
printf("Hello world\n");
return 0;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

For Such Cases.We can Calculate the Output by one Formula

(2^(n) -1) + 1

I know Above expression is equal to 2^n then..I am trying to Explain tat (2^(n) -1) Child Process would be there and 1 is for Parent Process.

So 8 times Hello is getting printed
7 + 1 (7 Times by Child Process and 1 time by its parent)=8

Plz ignore Spelling and Grammar Mistakes

Comment hidden because of low score. Click to expand.
0
of 0 vote

For Such Cases.We can Calculate the Output by one Formula

(2^(n) -1) + 1

I know Above expression is equal to 2^n then..I am trying to Explain tat (2^(n) -1) Child Process would be there and 1 is for Parent Process.

So 8 times Hello is getting printed
7 + 1 (7 Times by Child Process and 1 time by its parent)=8

Plz ignore Spelling and Grammar Mistakes

Comment hidden because of low score. Click to expand.
0
of 0 vote

For Such Cases.We can Calculate the Output by one Formula

(2^(n) -1) + 1

I know Above expression is equal to 2^n then..I am trying to Explain tat (2^(n) -1) Child Process would be there and 1 is for Parent Process.

So 8 times Hello is getting printed
7 + 1 (7 Times by Child Process and 1 time by its parent)=8

Plz ignore Spelling and Grammar Mistakes

Comment hidden because of low score. Click to expand.
0
of 0 vote

For Such Cases.We can Calculate the Output by one Formula

(2^(n) -1) + 1

I know Above expression is equal to 2^n then..I am trying to Explain tat (2^(n) -1) Child Process would be there and 1 is for Parent Process.

So 8 times Hello is getting printed
7 + 1 (7 Times by Child Process and 1 time by its parent)=8

Plz ignore Spelling and Grammar Mistakes

Comment hidden because of low score. Click to expand.
0
of 0 vote

For Such Cases.We can Calculate the Output by one Formula

(2^(n) -1) + 1

I know Above expression is equal to 2^n then..I am trying to Explain tat (2^(n) -1) Child Process would be there and 1 is for Parent Process.

So 8 times Hello is getting printed
7 + 1 (7 Times by Child Process and 1 time by its parent)=8

Plz ignore Spelling and Grammar Mistakes

Comment hidden because of low score. Click to expand.
0
of 0 vote

For Such Cases.We can Calculate the Output by one Formula

(2^(n) -1) + 1

I know Above expression is equal to 2^n then..I am trying to Explain tat (2^(n) -1) Child Process would be there and 1 is for Parent Process.

So 8 times Hello is getting printed
7 + 1 (7 Times by Child Process and 1 time by its parent)=8

Plz ignore Spelling and Grammar Mistakes

Comment hidden because of low score. Click to expand.
-1
of 1 vote

int main()
{
int pid;
if(!fork()) {
printf("Hello world\n");
if(!fork()) {
printf("Hello world\n");
if(!fork()) {
printf("Hello world\n");
}
else
printf("Hello world\n");
}
else
printf("Hello world\n");
}
else
printf("Hello world\n");
return 0;
}

Comment hidden because of low score. Click to expand.
-1
of 1 vote

int main()
{
int pid;
if(!fork()) {
printf("Hello world\n");
if(!fork()) {
printf("Hello world\n");
if(!fork()) {
printf("Hello world\n");
}
else
printf("Hello world\n");
}
else
printf("Hello world\n");
}
else
printf("Hello world\n");
return 0;
}

Comment hidden because of low score. Click to expand.
-1
of 1 vote

int main()
{
int pid;
if(!fork()) {
printf("Hello world\n");
if(!fork()) {
printf("Hello world\n");
if(!fork()) {
printf("Hello world\n");
}
else
printf("Hello world\n");
}
else
printf("Hello world\n");
}
else
printf("Hello world\n");
return 0;
}

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.