-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.c
65 lines (59 loc) · 1.18 KB
/
background.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
#include "headers.h"
int gethash(int n)
{
int hash_value = 0;
while(n%10)
{
hash_value += n%10;
n = n/10;
}
return hash_value;
}
void background(char *command)
{
char buffer[MAX_SIZE];
strcpy(buffer,command);
char *args[MAX_SIZE];
int i = 0;
char *cmd = strtok(buffer," ");
while(strcmp(cmd,"&") != 0)
{
args[i++] = cmd;
cmd = strtok(NULL," ");
}
args[i] = NULL;
pid_t pid = fork();
//setpgid(0,0);
if(pid == 0)
{
setpgid(0,0);
if(execvp(args[0],args) < 0)
{
printf("Invalid Command\n");
exit(1);
}
}
else if(pid == -1)
{
perror("Could not execute");
return;
}
else
{
//tcsetpgrp(STDIN_FILENO,shellpid);
printf("%d\n",pid);
jh[jobidx].jno = jobidx + 1;
jh[jobidx].pid = pid;
jh[jobidx].state = 'R';
strcpy(jh[jobidx].jobname,args[0]);
i=1;
while(args[i] != NULL)
{
strcat(jh[jobidx].jobname," ");
strcat(jh[jobidx].jobname,args[i]);
i++;
}
jobidx++;
return;
}
}