-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoit.cpp
More file actions
197 lines (178 loc) · 7.33 KB
/
doit.cpp
File metadata and controls
197 lines (178 loc) · 7.33 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <iostream>
using namespace std;
#include <cstring>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <stdlib.h>
extern char **environ;
struct backgroundJob {
pid_t pid;
struct timeval startTime;
struct timeval endTime;
char command[128];
};
void printProcessStats(struct timeval start, struct timeval end) {
struct rusage usage;
getrusage(RUSAGE_CHILDREN, &usage);
// Print CPU Time
cout << "User CPU Time: " << usage.ru_utime.tv_sec << "s " << usage.ru_utime.tv_usec << "us" << endl;
cout << "System CPU Time: " << usage.ru_stime.tv_sec << "s " << usage.ru_stime.tv_usec << "us" << endl;
// Print Wall-Clock Time
cout << "Wall-Clock Time: " << (end.tv_sec - start.tv_sec) << "s " << (end.tv_usec - start.tv_usec) << "us" << endl;
// Print Preempted Involuntarily
cout << "Preempted Involuntarily: " << usage.ru_nivcsw << endl;
// Print Voluntary Context Switches
cout << "Voluntary Context Switches: " << usage.ru_nvcsw << endl;
// Print Page Faults
cout << "Major Page Faults: " << usage.ru_majflt << endl;
cout << "Minor Page Faults: " << usage.ru_minflt << endl;
}
int main(int argc, char *argv[]) {
char *argvNew[32];
//cout << "Hello, World!" << endl;
/*for (int i = 0; i < argc; i++) {
cout << "Argument " << i << ": " << argv[i] << endl;
}*/
if (argc == 1) {
//Create Shell
char prompt[32] = "==>";
backgroundJob background[128];
int bgCount = 1;
while (true) {
struct timeval start;
struct timeval end;
gettimeofday(&start, NULL);
bool isBackground = false;
char* line = new char[128];
cout << prompt;
cin.getline(line, 128);
gettimeofday(&start, NULL);
if (strcmp(line, "exit") == 0) {
delete[] line;
break;
}
// Tokenize the input line
char* token = strtok(line, " ");
int i = 0;
while (token != NULL && i < 32) {
argvNew[i++] = token;
token = strtok(NULL, " ");
}
if (strcmp(argvNew[i - 1], "&") == 0) {
argvNew[i - 1] = NULL;
isBackground = true;
}
argvNew[i] = NULL;
if (strcmp(argvNew[0], "cd") == 0) {
if (chdir(argvNew[1]) != 0) {
cerr << "CD error\n";
} else {
argvNew[1] = getcwd(NULL, 0);
cout << "Directory changed to: " << argvNew[1] << endl;
}
continue;
} else if (strcmp(argvNew[0], "set") == 0 && strcmp(argvNew[1], "prompt") == 0 && strcmp(argvNew[2], "=") == 0) {
if (argvNew[3] != NULL) {
strncpy(prompt, argvNew[3], sizeof(prompt) - 1);
prompt[sizeof(prompt) - 1] = '\0'; // Ensure null-termination
cout << "Prompt set to: " << prompt << endl;
}
continue;
} else if (strcmp(argvNew[0], "jobs") == 0) {
// List background jobs
for (int i = 1; i < bgCount; i++) {
cout << "[" << i << "] " << background[i].pid << " " << background[i].command << endl;
}
}
// Check if any background jobs have completed
for (int i = 1; i <= bgCount; i++) {
//cout << "Checking job [" << i << "]" << background[i].startTime.tv_sec << endl;
if (waitpid(background[i].pid, NULL, WNOHANG) > 0) {
cout << "Background job [" << i << "] completed." << endl;
gettimeofday(&background[i].endTime, NULL);
//cout << background[i].startTime.tv_sec << " " << background[i].endTime.tv_sec << endl;
printProcessStats(background[i].startTime, background[i].endTime);
// Remove the completed job from the list
for (int j = i; j < bgCount - 1; j++) {
background[j] = background[j + 1];
}
bgCount--;
i--;
}
}
// Fork and exec the command
pid_t child_pid = fork();
if (child_pid == 0) {
execvp(argvNew[0], argvNew);
cerr << "Exec error\n";
exit(1);
} else if (child_pid < 0) {
cerr << "Fork error\n";
exit(1);
} else {
if (isBackground) {
if (waitpid(child_pid, NULL, WNOHANG) == 0) {
struct timeval temp;
background[bgCount].pid = child_pid;
strncpy(background[bgCount].command, argvNew[0], sizeof(background[bgCount].command) - 1);
background[bgCount].command[sizeof(background[bgCount].command) - 1] = '\0'; // Ensure null-termination
temp.tv_sec = start.tv_sec;
temp.tv_usec = start.tv_usec;
background[bgCount].startTime = temp;
cout << bgCount << " " << background[bgCount].startTime.tv_sec << endl;
cout << "[" << bgCount++ << "] " << child_pid << endl;
continue;
} else if (waitpid(child_pid, NULL, 0) < 0) {
cerr << "Waitpid error\n";
} else {
cout << "\n" << background[bgCount].command << " command executed successfully\nCommand Statistics:" << endl;
gettimeofday(&background[bgCount].endTime, NULL);
printProcessStats(background[bgCount].startTime, background[bgCount].endTime);
}
}
waitpid(child_pid, NULL, 0);
cout << "\n" << argvNew[0] << " command executed successfully\nCommand Statistics:" << endl;
gettimeofday(&end, NULL);
printProcessStats(start, end);
}
delete[] line;
}
} else {
pid_t pid = fork();
struct timeval start1, end1;
gettimeofday(&start1, NULL);
if (pid < 0) {
cerr << "Fork error\n";
exit(1);
} else if (pid == 0) {
//cout << "In child process" << endl;
// Child process
//string path = "wc";
argvNew[0] = argv[1];
argvNew[1] = argv[2];
argvNew[2] = NULL;
/*
if (execvp(argvNew[0], argvNew) < 0) {
cerr << "Exec error\n";
exit(1);
}*/
execvp(argvNew[0], argvNew);
cerr << "Exec error\n";
exit(1);
} else {
int status;
// Parent process
waitpid(pid, &status, 0);
if (argc > 1) {
//cout << "\n" << argv[1] << " command executed successfully\nCommand Statistics:" << endl;
gettimeofday(&end1, NULL);
printProcessStats(start1, end1);
}
//cout << "Child exited with status " << status << endl;
}
//cout << "Done" << endl;
return 0;
}
}