Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- int main (int argc, char *argv[])
- {
- int i,arg,j,pid,nivel;
- if ((argc < 2) || (argc > 4))
- {
- printf("Error en el numero de datos\n");
- return(1);
- }
- for (i=1;i<argc;i++)
- {
- arg=atoi(argv[i]);
- if ((arg<1) || (arg>5))
- {
- printf("error\n");
- return(1);
- }
- else
- {
- /************************************************
- creación de un proceso concurrente con el creador
- *************************************************/
- for(j=1;j<arg;j++)
- {
- pid=fork ();
- if ((pid == -1))
- {
- printf ("error en creacion de proceso hijo\n");
- exit(1);
- }
- else
- if ( pid == 0) /* proceso hijo */
- {
- for(j=1;j<arg;j++)
- {
- pid=fork ();
- if ((pid == -1))
- {
- printf ("error en creacion de proceso hijo\n");
- exit(1);
- }
- else
- if ( pid == 0)
- /* proceso hijo */
- {
- nivel=2;
- for(j=1;j<arg;j++)
- {
- pid=fork ();
- if ((pid == -1))
- {
- printf ("error en creacion de proceso hijo\n");
- exit(1);
- }
- else
- if ( pid == 0)
- /* proceso hijo */
- {
- nivel=3;
- printf ("nivel %d PID=%d PID de su padre =%d\n",nivel,getpid(),getppid());
- fflush(stdout);
- exit (0); /* terminacion con codigo 0 */
- }
- else /* proceso padre */ {
- fflush(stdout);
- wait (0);
- }
- }
- printf ("\tnivel %d PID=%d PID de su padre =%d\n",nivel,getpid(),getppid());
- fflush(stdout);
- exit (0); /* terminacion con codigo 0 */
- }
- else /* proceso padre */
- {
- fflush(stdout);
- wait (0);
- }
- }
- nivel=1;
- printf ("\t\tnivel %d PID=%d PID de su padre =%d\n\n",nivel,getpid(),getppid());
- fflush(stdout);
- exit (0); /* terminacion con codigo 0 */
- }
- else /* proceso padre */
- {
- fflush(stdout);
- wait (0);
- }
- }
- }
- }
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement