Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. int main (int argc, char *argv[])
  6. {
  7. int i,arg,j,pid,nivel;
  8. if ((argc < 2) || (argc > 4))
  9. {
  10. printf("Error en el numero de datos\n");
  11. return(1);
  12. }
  13. for (i=1;i<argc;i++)
  14. {
  15. arg=atoi(argv[i]);
  16. if ((arg<1) || (arg>5))
  17. {
  18. printf("error\n");
  19. return(1);
  20. }
  21. else
  22. {
  23. /************************************************
  24. creación de un proceso concurrente con el creador
  25. *************************************************/
  26. for(j=1;j<arg;j++)
  27. {
  28. pid=fork ();
  29.  
  30.  
  31. if ((pid == -1))
  32. {
  33. printf ("error en creacion de proceso hijo\n");
  34. exit(1);
  35. }
  36. else
  37. if ( pid == 0) /* proceso hijo */
  38. {
  39. for(j=1;j<arg;j++)
  40. {
  41. pid=fork ();
  42. if ((pid == -1))
  43. {
  44. printf ("error en creacion de proceso hijo\n");
  45. exit(1);
  46. }
  47. else
  48. if ( pid == 0)
  49. /* proceso hijo */
  50. {
  51. nivel=2;
  52. for(j=1;j<arg;j++)
  53. {
  54. pid=fork ();
  55. if ((pid == -1))
  56. {
  57. printf ("error en creacion de proceso hijo\n");
  58. exit(1);
  59. }
  60. else
  61. if ( pid == 0)
  62. /* proceso hijo */
  63. {
  64. nivel=3;
  65. printf ("nivel %d PID=%d PID de su padre =%d\n",nivel,getpid(),getppid());
  66. fflush(stdout);
  67. exit (0); /* terminacion con codigo 0 */
  68. }
  69. else /* proceso padre */ {
  70. fflush(stdout);
  71. wait (0);
  72. }
  73. }
  74. printf ("\tnivel %d PID=%d PID de su padre =%d\n",nivel,getpid(),getppid());
  75. fflush(stdout);
  76. exit (0); /* terminacion con codigo 0 */
  77. }
  78. else /* proceso padre */
  79. {
  80. fflush(stdout);
  81. wait (0);
  82. }
  83. }
  84.  
  85.  
  86. nivel=1;
  87. printf ("\t\tnivel %d PID=%d PID de su padre =%d\n\n",nivel,getpid(),getppid());
  88. fflush(stdout);
  89. exit (0); /* terminacion con codigo 0 */
  90. }
  91. else /* proceso padre */
  92. {
  93. fflush(stdout);
  94. wait (0);
  95. }
  96. }
  97. }
  98. }
  99. return(0);
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement