Advertisement
Guest User

Untitled

a guest
Oct 8th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.57 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.  
  11. typedef struct processarray{
  12.     int pid;
  13.     int status;
  14.     struct processarray *next;
  15. } Processarray_t;
  16.  
  17.  
  18.  
  19. int main(int argv ,char*argc[]) {
  20.  
  21.     int nargs;
  22.     int nforks = 0;
  23.     int pid;
  24.     char *args[MAXARGS];
  25.     int status;
  26.     int x;
  27.     int y;
  28.  
  29.     Processarray_t* head = NULL;
  30.     Processarray_t* temp;
  31.  
  32.     while(1){
  33.  
  34.         nargs = readLineArguments(args,MAXARGS);
  35.  
  36.         if (nargs > 0){
  37.  
  38.             if (strcmp(args[0],"exit") != 0){
  39.                 pid = fork();
  40.                 nforks++;
  41.  
  42.                 if (pid < 0){
  43.                     printf("Process error\n");
  44.                     exit(EXIT_FAILURE);
  45.                 }
  46.    
  47.                 else{
  48.                     if (pid == 0){
  49.                         execv(args[0], args);
  50.                         exit(EXIT_FAILURE);
  51.                     }
  52.                 }
  53.             }
  54.    
  55.             if (strcmp(args[0],"exit") == 0){
  56.  
  57.                 for (x = 0, x < nforks, x++){
  58.                     if (head == NULL){
  59.                         head = malloc(sizeof(Processarray_t));
  60.                         head->next = NULL;
  61.                         head->pid = wait(&status);
  62.                         head->status = status;
  63.                     }
  64.                     else{
  65.                         temp = head;
  66.                         while (temp->next != NULL){
  67.                             temp = temp->next;
  68.                         }
  69.                         temp->next = malloc(sizeof(Processarray_t));
  70.                         temp->next->next = NULL;
  71.                         temp->next->pid = wait(&status);
  72.                         temp->next->status = status;
  73.                     }
  74.                 }
  75.                
  76.                 temp = head;
  77.  
  78.                 while (temp != NULL){
  79.                     if (WIFEXITED(temp->status)){
  80.                         printf("Child process %d terminated with status %d\n", temp->pid, WEXITSTATUS(temp->status));
  81.                     }
  82.                     temp = temp->next;
  83.                 }
  84.                 return 0;
  85.             }
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement