-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.c
More file actions
39 lines (33 loc) · 850 Bytes
/
background.c
File metadata and controls
39 lines (33 loc) · 850 Bytes
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
#include "headers.h"
void background(char *commands[], ll n){
int pid;
pid = fork();
char *subset[10000];
for(ll i =0; i < n; i++){ /* If command is `emacs & hi` */
while(strcmp(commands[i], "&")){
subset[i] = commands[i];
i++;
}
break;
}
if(pid == -1){
printf("\033[0;31mError: Unable to fork\033[0m\n");
perror("fork");
return;
}
else if(pid == 0){
int x;
x = execvp(subset[0], subset);
if(x == -1){
printf("\033[0;31mError: Unable to execute\033[0m\n");
perror("execvp");
return;
}
}
else{
njobs++;
pids[njobs]=pid;
strcpy(names[njobs], commands[0]);
// printf("bg: %d, %s, %d\n", njobs, names[njobs], pids[njobs]);
}
}