execute.c
changeset 9 9acd8ae3190c
parent 8 03339adb7014
child 18 af4898956964
equal deleted inserted replaced
8:03339adb7014 9:9acd8ae3190c
     1 #include <unistd.h>
     1 #include <unistd.h>
     2 #include <stdio.h>
     2 #include <stdio.h>
     3 #include <stdlib.h>
     3 #include <stdlib.h>
       
     4 #include <signal.h>
     4 
     5 
       
     6 #include "msg.h"
     5 #include "main.h"
     7 #include "main.h"
       
     8 
       
     9 static void program_signal();
     6 
    10 
     7 static void run_parent()
    11 static void run_parent()
     8 {
    12 {
     9     int status;
    13     int status;
    10     wait(&status);
    14     wait(&status);
    11     printf("End of child\n");
       
    12 };
    15 };
    13 
    16 
    14 static void run_child(const char *command)
    17 static void run_child(const char *command)
    15 {
    18 {
       
    19     int p[2];
       
    20     /* Closing input */
       
    21     pipe(&p);
       
    22     close(p[1]); /* closing the write handle */
       
    23     close(0);
       
    24 
       
    25     dup(p[0]); /* the pipe reading goes to stdin */
    16     execlp("bash", "bash", "-c", command, NULL);
    26     execlp("bash", "bash", "-c", command, NULL);
    17 }
    27 }
    18 
    28 
    19 void run_job(const char *command)
    29 void run_job(const char *command)
    20 {
    30 {
    21     int pid;
    31     int pid;
    22 
    32 
    23     pid = fork();
    33     pid = fork();
    24 
    34 
       
    35     /* For the parent */
       
    36     /*program_signal(); Still not needed*/
       
    37 
    25     switch(pid)
    38     switch(pid)
    26     {
    39     {
    27         case 0:
    40         case 0:
       
    41             close(server_socket);
    28             run_child(command);
    42             run_child(command);
    29             break;
    43             break;
    30         case -1:
    44         case -1:
    31             perror("Error in fork");
    45             perror("Error in fork");
    32             exit(-1);
    46             exit(-1);
    34         default:
    48         default:
    35             run_parent();
    49             run_parent();
    36             break;
    50             break;
    37     }
    51     }
    38 }
    52 }
       
    53 
       
    54 static void sigchld_handler(int val)
       
    55 {
       
    56 }
       
    57 
       
    58 static void program_signal()
       
    59 {
       
    60   struct sigaction act;
       
    61 
       
    62   act.sa_handler = sigchld_handler;
       
    63   /* Reset the mask */
       
    64   memset(&act.sa_mask,0,sizeof(act.sa_mask));
       
    65   act.sa_flags = SA_NOCLDSTOP;
       
    66   act.sa_restorer = NULL;
       
    67 
       
    68   sigaction(SIGCHLD, &act, NULL);
       
    69 }