-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.c
More file actions
67 lines (62 loc) · 1.85 KB
/
Copy pathprocess.c
File metadata and controls
67 lines (62 loc) · 1.85 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
/*
* semsem is a basic shell for Unix/Linux systems
* Written by: Islam Faisal (decltypeme)
* The American University in Cairo
* For License, please see LICENSE
*/
/*
* File: process.c
* Author: Islam Faisal
* Utility functions to handle child jobs submitted to the shell
*/
#include "process.h"
int current_child_executing_pid = -1;
void SIG_INT_HANDLER(int sig) {
if (current_child_executing_pid != -1) {
kill(current_child_executing_pid, SIGKILL);
}
return;
}
void flush_all_buffers(void) {
fflush(stdout);
fflush(stderr);
fflush(stdin);
}
void execute(char** _args, int _argc, bool _bg) {
flush_all_buffers();
pid_t pid = fork();
//Failed to create process
if (pid == -1) {
fprintf(stderr, "Failed to create a child process\n");
return;
}//We are in the child's code
else if (pid == 0) {
if (execvp(_args[0], _args) == -1) {
if (errno == ENOENT)
printf("%s", COMMAND_NOT_FOUND__MSG);
else {
printf("%s\n", strerror(errno));
}
exit(CHILD_FAILED);
}
}//We are in the parent's code
else {
//You know parents need to have some control over their children
//If the process is not set to work in the background, we must wait.
if (!_bg) {
current_child_executing_pid = pid;
int exit_status;
//Wait till child executes either normally or abnormally
waitpid(pid, &exit_status, 0);
flush_all_buffers();
current_child_executing_pid = -1;
#ifdef ENABLE_PARENT_MESSAGE_ON_FAIULRE
if (exit_status == CHILD_FAILED)
printf("The child process failed to create.\n");
#endif
#ifdef PRINT_CHILD_EXIT_CODE
printf("Child process exited with %d\n", exit_status);
#endif
}
}
}