-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.c
88 lines (79 loc) · 1.9 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "headers.h"
//int is_foreground;
//struct to store the background process info
// struct process_info{
// pid_t pid;
// char name[200];
// };
// //array of process_info
struct process_info process[100];
// int process_count = 0;
void delete_process(pid_t pid)
{
if(pid == -1)
{
process_count=0;
return;
}
int i;
for (i = 0; i < process_count; i++)
{
if (process[i].pid == pid)
{
process[i].stat='T';
}
}
// for (; i < process_count - 1; i++)
// {
// process[i] = process[i + 1];
// }
// process_count--;
}
void process_completed(){
if(is_foreground == 1){
is_foreground = 0;
return;
}
if(is_pipes == 1){
is_pipes = 0;
return;
}
int stat;
pid_t pid=waitpid(-1,&stat,WNOHANG);
if(pid < 0){
printf("waitpid failed(check)\n");
}
for(int i=0;i<process_count;i++){
int const exit_status=WEXITSTATUS(stat);
if(WIFEXITED(exit_status) && process[i].pid == pid){
if(exit_status == 0){
printf("%s with pid= %d exited normally\n", process[i].name, pid);
}
else{
printf("%s with pid= %d exited abnormally\n", process[i].name, pid);
}
}
//display(*org_dir,time_span);
delete_process(pid);
}
}
//Background process
void background(char* args[]){
// remove_amp(args[1]);
pid_t pid = fork();
if(pid < 0 ){
printf("Error in forking\n");
}
else if(pid == 0){
if(execvp(args[0],args) < 0){
printf("Error in executing command\n");
}
}
else{
printf("[%d] %d\n", process_count+1, pid);
process[process_count].pid = pid;
process[process_count].stat='R';
strcpy(process[process_count].name,args[0]);
process_count++;
}
}