Advertisement
Guest User

Untitled

a guest
Oct 8th, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "commandlinereader.h"
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/wait.h>
  7.  
  8. #define MAXARGS 7
  9.  
  10. //access(args0,F_OK) == 0
  11.  
  12. typedef struct processarray{
  13.     int pid;
  14.     int status;
  15.     struct processarray *next;
  16. } Processarray_t;
  17.  
  18. int nargs;
  19. int nforks = 0;
  20. int pid;
  21. char *args[MAXARGS];
  22. int status;
  23. int processpid;
  24. Processarray_t array;
  25.  
  26. int main(int argv ,char*argc[]) {
  27.  
  28.     while(1){
  29.  
  30.         nargs = readLineArguments(args,MAXARGS);
  31.        
  32.         if (nargs > 0){
  33.  
  34.             if (strcmp(args[0],"exit") != 0){   /* se nao puser a condicao nforks < nargs, ele vai ficar em ciclo infinito */
  35.                 pid = fork();
  36.                 nforks++;
  37.  
  38.                 if (fopen(args[0],"r") != NULL){
  39.    
  40.                     if (pid < 0){
  41.                         printf("Process error\n");  /* Nunca esta a chegar aqui independentemente dos argumentos */
  42.                         exit(EXIT_FAILURE);
  43.                     }
  44.        
  45.                     else{
  46.        
  47.                         if (pid == 0){
  48.                             execv(args[0], args);   /* como faze-lo identificar quais sao realmente ficheiros? */
  49.                             exit(EXIT_FAILURE);
  50.                         }
  51.                     }
  52.                 }
  53.            
  54.             }
  55.    
  56.             if (strcmp(args[0],"exit") == 0){
  57.    
  58.                 while (nforks > 0){
  59.                    
  60.                     processpid = wait(&status);
  61.                     printf("Child process %d terminated with status %d\n", processpid, WEXITSTATUS(status));
  62.                     nforks--;
  63.                    
  64.                 }
  65.                 return 0;
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement