-
Notifications
You must be signed in to change notification settings - Fork 0
/
syscom.c
99 lines (94 loc) · 2.14 KB
/
syscom.c
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "headers.h"
extern int* bgproc;
extern int proc;
void interrupt_handler(){
if(fgp!=0){
kill(fgp,SIGINT);
}
}
void stop_handler(){
if(fgp!=0){
kill(fgp,SIGSTOP);
printf("\n%s with process id %d is stopped\n",fg_name,fgp);
strcpy(bp[b].name,fg_name);
bp[b].pid=fgp;
bp[b].seq=s+1;
b++;
s++;
fgp=0;
}
}
void syscom(char *c, char **arr, int comm,int f)
{
char *command;
strcpy(command, c);
char **args;
pid_t x = 111;
args = (char **)malloc((comm + 1) * sizeof(char **));
int count = 0;
int flag = 0;
for (int j = 0; j < comm; j++)
{
if (strcmp(arr[j], "&") != 0)
{
args[count++] = arr[j];
}
else{
flag=1;
}
}
args[count] = NULL;
int status;
if (comm < 1)
{
perror("Invalid no of arguments\n");
}
else
{
if (flag == 1)
{
//background process
x = fork();
if (x == 0)
{
setpgid(0,0);
if(execvp(args[0], args)<0){
perror("Invalid Command");
}
}
else{
printf("Process id %d\n",x);
bp[b].pid=x;
strcpy(bp[b].name,args[0]);
bp[b].seq=s+1;
tcsetpgrp(STDIN_FILENO,getpgrp());
tcsetpgrp(STDOUT_FILENO,getpgrp());
s++;
b++;
}
}
else
{
//foreground process
x = fork();
if (x == 0)
{
if(execvp(args[0], args)<0){
perror("Invalid Command");
}
}
else
{
signal(SIGTSTP,stop_handler);
signal(SIGINT,interrupt_handler);
setpgid(0,0);
fgp=x;
strcpy(fg_name,arr[0]);
if(fgp!=0){
waitpid(x, &status, WUNTRACED);
}
signal(SIGINT,SIG_IGN);
}
}
}
}