Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include "commandlinereader.h"
- #include <string.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #define MAXARGS 7
- //access(args0,F_OK) == 0
- typedef struct processarray{
- int pid;
- int status;
- struct processarray *next;
- } Processarray_t;
- int nargs;
- int nforks = 0;
- int pid;
- char *args[MAXARGS];
- int status;
- int processpid;
- Processarray_t array;
- int main(int argv ,char*argc[]) {
- while(1){
- nargs = readLineArguments(args,MAXARGS);
- if (nargs > 0){
- if (strcmp(args[0],"exit") != 0){ /* se nao puser a condicao nforks < nargs, ele vai ficar em ciclo infinito */
- pid = fork();
- nforks++;
- if (fopen(args[0],"r") != NULL){
- if (pid < 0){
- printf("Process error\n"); /* Nunca esta a chegar aqui independentemente dos argumentos */
- exit(EXIT_FAILURE);
- }
- else{
- if (pid == 0){
- execv(args[0], args); /* como faze-lo identificar quais sao realmente ficheiros? */
- exit(EXIT_FAILURE);
- }
- }
- }
- }
- if (strcmp(args[0],"exit") == 0){
- while (nforks > 0){
- processpid = wait(&status);
- printf("Child process %d terminated with status %d\n", processpid, WEXITSTATUS(status));
- nforks--;
- }
- return 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement