-
Notifications
You must be signed in to change notification settings - Fork 0
/
back_process.c
67 lines (54 loc) · 2.11 KB
/
back_process.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
#include "headers.h"
#include "back_process.h"
void run_back_background(char* cur_command){
char* argument_list[max_arg_length];
char* old_argument = strdup(cur_command);
int num_arguments;
my_strtok(argument_list, &num_arguments, " \n\t", cur_command);
// extract arguments and run in background
run_in_background(argument_list, num_arguments);
}
void run_in_background(char** argument_list, int argument_num){
if (argument_num == 0){
printf("syntax error near unexpected token `&`\n");
return;
}
// create child process
pid_t back_pid = fork();
if (back_pid < 0){
perror("Could not create forked process");
return;
} else if (back_pid == 0){
setpgrp();
if ( execvp(argument_list[0], argument_list) < 0){
printf("\nBackground process - Command not found\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
} else {
// insert process into dll
insert_dll(running_background_processes, back_pid, parse_to_string(argument_list, argument_num));
printf("[%ld] %d\n", running_background_processes->size, back_pid);
}
fflush(stdout);
}
void background_process_term(){
int status;
pid_t back_process = waitpid(-1, &status, WNOHANG);
if (back_process > 0){
// find node in dll for the process that exited
DLLNode req_node = find_node(running_background_processes, back_process);
printf("\n%s with pid %d exited ", req_node->element->process_name, req_node->element->process_pid);
if (WIFEXITED(status)){
printf("Normally\n");
fprintf(stderr, "Process: %s PID: %d -> exited with status %d\n", req_node->element->process_name, req_node->element->process_pid, WEXITSTATUS(status));
} else {
printf("Abnormally\n");
fprintf(stderr, "Process: %s PID: %d -> FAILED\n", req_node->element->process_name, req_node->element->process_pid);
}
// remove exited process from dll
delete_node(running_background_processes, req_node);
prompt();
}
fflush(stdout);
}