-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.cpp
More file actions
216 lines (172 loc) · 5 KB
/
monitor.cpp
File metadata and controls
216 lines (172 loc) · 5 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "project.h"
#include <iostream>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <stdexcept>
#include <stdlib.h>
using namespace Project;
using namespace std;
double Monitor::getTick(clock_t time){
long tps=sysconf(_SC_CLK_TCK);
return (double)time/tps;
}
//string to char**
char** Monitor::cmdSplit(std::string str)
{
//string to char*
std::vector<std::string> buf;
std::string tmp;
std::stringstream ss;
ss<<str;
while(ss>>tmp)
{
buf.push_back(tmp + "");
}
//char* to char**
char** pList=new char*[buf.size()];
for(int i=0; i!=buf.size();i++)
{
pList[i]=const_cast<char*>(buf[i].c_str());
}
//cout<<pList<<endl;
//delete[] pList;
return pList;
}
Monitor::Monitor(Job j):job(j)
{
//cout<<j.get_cmd()<<endl;
char** command = cmdSplit(j.get_cmd());
int process;
int status;
set_self_pid(getpid());
start = times(&t_start);
process = fork();
if(process < 0){
// error
status = -1;
}
else if(process == 0){
//cout << "child suspend!!!!!!!!!!!!" << endl;
//kill(getpid(), SIGTSTP); // suspend child
// child process
cout << "=================================================" << endl;
cout << "Job Process Start ! " << getpid() << endl;
cout << endl;
cout<<"command "<<command[0]<<endl;
execvp(command[0],command);
throw invalid_argument("Fail to execute Child Process!\n");
}
else{
set_pid(process);
if(m[getpid()]==0){m[getpid()] = process;}
signal(SIGTSTP, sig_handler);
signal(SIGCONT, sig_handler);
signal(SIGTERM, sig_handler);
//cout << process << " " << getpid() << endl;
// parent process
waitpid(process,&status,0);
std::cout<<"Child Process Terminate"<<std::endl;
//std::cout << getpid() << " " << m[getpid()] << std::endl;
//signal(SIGTERM, sig_handler);
//signal(SIGCHLD, sig_handler);
//if(this->job.get_dur_time()>=0){
// alarm(this->job.get_dur_time());
//}
// calculate time
end = times(&t_end);
set_timeElapsed(end-start);
set_userTime(t_end.tms_cutime);
set_systermTime(t_end.tms_cstime);
// output
cout << endl;
cout << endl;
print_time();
}
//if(this->job.get_dur_time()>=0){
// double remain_time=this->job.get_dur_time()-this->get_timeElapsed();
// if(remain_time>0)
// {
// sleep(remain_time);
// }
//}
//else{
// this->job.set_dur_time(this->get_timeElapsed());
//}
}
Monitor::Monitor(char *command[]):command(command)
{
int process;
int status;
start = times(&t_start);
process = fork();
if(process < 0){
status = -1;
}
else if(process == 0){
//signal(SIGINT, SIG_DFL); //Default handle every child process
execvp(command[0],command);
throw invalid_argument("Fail to execute Child Process!\n");
}
else{
int status;
set_pid(process);
signal(SIGTSTP, sig_handler);
signal(SIGCONT, sig_handler);
signal(SIGTERM, sig_handler);
waitpid(process,&status,0);
// quit loop if the child process has been terminated!
}
// calculate time
end = times(&t_end);
set_timeElapsed(end-start);
set_userTime(t_end.tms_cutime);
set_systermTime(t_end.tms_cstime);
// output
cout << endl;
cout << endl;
print_time();
}
void Monitor::set_self_pid(int pid){
this->self_pid=pid;
}
void Monitor::set_pid(int pid){
this->process_pid=pid;
}
void Monitor::set_systermTime(clock_t time){
this->system_time=getTick(time);
}
void Monitor::set_userTime(clock_t time){
this->user_time=getTick(time);
}
void Monitor::set_timeElapsed(clock_t time){
this->time_elapsed=getTick(time);
}
void Monitor::set_all(int pid,clock_t use_time,clock_t system_time,clock_t time_elapsed){
}
int Monitor::get_pid() const{
return this->process_pid;
}
int Monitor::get_self_pid()const{
return this->self_pid;
}
double Monitor::get_userTime() const{
return this->user_time;
}
double Monitor::get_systemTime() const{
return this->system_time;
}
double Monitor::get_timeElapsed() const{
return this->time_elapsed;
}
void Monitor::print_time(){
cout<<"Job Process "<<setw(6)<<get_pid()<<" : Time Elapsed: "<<get_timeElapsed()<<'\n';
cout<<setfill(' ')<<setw(35)<<"User Time : "<<get_userTime()<<'\n';
cout<<setfill(' ')<<setw(35)<<"System Time : "<<get_systemTime()<<'\n';
cout << "=================================================" << '\n';
cout << '\n';
}