2,485 views
1 1 vote

main()

{

printf(" * ");

for(i=0;i<n;i++)

fork();

printf(" * ");

}

how many times * is printed??

1 Answer

2 2 votes

2n  +1     

Try drawing a tree to understand how each fork works.

http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html

• edited by
Position:
Show:

Related questions

5 5 votes
2 2 answers
458
458 views
GO Classes asked Jul 15
458 views
Consider the following C program. Assume that every $\texttt{fork()}$ call succeeds.int main(void) { unsigned long int i; for (i = 0; i < 6; i++) if (fork() >= 0) continu...
3 3 votes
1 1 answer
313
313 views
GO Classes asked Jul 15
313 views
Assume that every $\texttt{fork()}$ call succeeds.int main() { int a = 0; int rc = fork(); a++; if (rc == 0) { rc = fork(); a++; } else { a++; } printf("a is %d\n", a); }...
4 4 votes
1 1 answer
310
310 views
GO Classes asked Jul 15
310 views
Assume that every $\texttt{fork()}$ call succeeds.int main() { int pid; pid = fork() && fork(); if (!pid) printf("A\n"); else printf("B\n"); return 0; }How many times are...
5 5 votes
1 1 answer
290
290 views
GO Classes asked Jul 15
290 views
Consider the following program. Assume that every $\texttt{fork()}$ call succeeds.int main() { int p1 = 1, p2 = 2, p3 = 3; p1 = fork(); if (p2 0) p2 = fork(); if (p1 0)...