By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,188 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,188 IT Pros & Developers. It's quick & easy.

Can any help me to write program using Fork in C to generate this tree structure?

P: 1
the tree in the the attachment below

this is my code that I'm tried to write :

Expand|Select|Wrap|Line Numbers
  1. #include<sys/types.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<unistd.h>
  5.  
  6. int main()
  7. {
  8.  
  9. printf("the parent id %d" , getpid());
  10.  
  11. pid_t pid1,pid2,pid3;
  12.  
  13. for (j=0;j<3;j++)
  14. {
  15. pid1=fork();
  16. printf("%d",getpid());
  17. }
  18. if (pid1==0)
  19. {
  20.  pid2=fork();
  21. elseif (pid3=fork();
  22. }
  23.  if((pid2 == 0)&&(pid3 == 0)) 
  24.    {
  25.        if (pid1 !=0)
  26.        fork();
  27.    }
  28.  
  29. printf("the process id %d" , getpid());
  30.  
  31. return 0;
  32.  
  33. }
  34.  
Attached Images
File Type: jpg 12745814_806546132825460_2140659547443282482_n (2).jpg (24.9 KB, 382 views)
Feb 21 '16 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
This is too big a subject for a thread like this.

Start here:

https://www.youtube.com/watch?v=HDMkuDVuDyc
Feb 21 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.