-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecuteCMD.c
More file actions
78 lines (70 loc) · 1.37 KB
/
executeCMD.c
File metadata and controls
78 lines (70 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
typedef struct Node{
pid_t pid_process;
char pname[100];
int num;
struct Node *next;
}node;
#define MAX_COMMAND_LENGTH 100
#define MAX_NUMBER_OF_PARAMS 10
pid_t pid;
extern node *head;
extern int counter;
int childStatus;
// char ** params;
void handler1(int sig)
{
pid_t pid_h;
pid_h = wait(NULL);
printf("Pid %d exited.\n", pid_h);
}
int executeCmm(char ** params,int andPresent)
{
signal(SIGCHLD, SIG_IGN);
pid = fork(); // fork process
if(andPresent)
{
insert(pid,params[0]);
}
// Error
if (pid == -1) {
char* error = strerror(errno);
printf("fork: %s\n", error);
return 1;
}
// Child process
else if (pid == 0) {
// execute execvp
int i=0;
while(params[i]!=NULL)
{
i++;
}
execvp(params[0], params);
printf("dion\n");
// if error occured
char* error = strerror(errno);
printf("shell: %s: %s\n", params[0], error);
return 0;
}
// Parent process
else {
if(!andPresent) //if foreground process then wait for the child process to finish
{
signal(SIGCHLD, SIG_DFL);
waitpid(pid, &childStatus, WCONTINUED);
}
else // if process is background then don't wait for the child process and print the pid when exited
{
signal(SIGCHLD, handler1);
}
return 1;
}
}