[ad_1]
I am trying to get the utime, stime, cutime, cstime of a process that I launch with my c++ code. They all return as 0. Is this because my program that I am launching is not performing big enough computations? If so, which code/program should I use so that my utime and stime are greater than 0?
I am trying to calculate the CPU Utilization of a Process using How do I get the total CPU usage of an application from /proc/pid/stat? but its hard to confirm if I am calculating correctly if my values are 0.
launchprocess.cpp
static float calculate_cpu_usage(pid_t ch_pid){
FILE *f;
long unsigned int utime, stime;
unsigned long long start_time;
long cutime, cstime;
char filename[1000];
sprintf(filename, "/proc/%d/stat", ch_pid);
f = fopen(filename, "r");
if (f == NULL){
std::cout<<"Cannot Open Proc File"<<std::endl;
} else {
fscanf(f,"%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu %ld %ld %*d %*d %*d %*d %llu",&utime, &stime, &cutime, &cstime, &start_time);
std::cout << utime << std::endl;
std::cout << stime << std::endl;
std::cout << cutime << std::endl;
std::cout << cstime << std::endl;
std::cout << start_time << std::endl;
}
return 0;
}
int main(int argc, char *argv[]) {
std::vector<char*> arg_list;
std::string program_name = argv[1];
arg_list.push_back(const_cast <char *>(program_name.data()));
for (int i = 2; i < argc; i++) {
arg_list.push_back(argv[i]);
}
arg_list.push_back(nullptr);
pid_t child_pid1 = spawnChild(program_name.c_str(), arg_list.data());
std::chrono::milliseconds timespan(10);
std::this_thread::sleep_for(timespan);
int status;
pid_t result = waitpid(child_pid1, &status, WNOHANG);
if (result == 0) {
calculate_memory_usage(child_pid1);
calculate_cpu_usage(child_pid1);
} else if (result == -1) {
std::cout << "Error" << std::endl;
} else {
std::cout << "Child has been terminated" <<std::endl;
}
pid_t child_pid;
while ((child_pid = wait(nullptr)) > 0)
std::cout << "child " << child_pid << " terminated" << std::endl;
std::cout << std::endl;
return 0;
}
busywaiting.c
#include <pthread.h>
#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/* i is global, so it is visible to all functions. It makes use of the special
* type atomic_int, which allows atomic memory accesses.
*/
atomic_int i = 0;
/* f1 uses a spinlock to wait for i to change from 0. */
static void *f1(void *p)
{
int local_i;
/* Atomically load current value of i into local_i and check if that value
is zero */
while ((local_i = atomic_load(&i)) == 0) {
/* do nothing - just keep checking over and over */
}
printf("i's value has changed to %d.\n", local_i);
return NULL;
}
static void *f2(void *p)
{
int local_i = 99;
sleep(10); /* sleep for 10 seconds */
atomic_store(&i, local_i);
printf("t2 has changed the value of i to %d.\n", local_i);
return NULL;
}
int main()
{
int rc;
pthread_t t1, t2;
rc = pthread_create(&t1, NULL, f1, NULL);
if (rc != 0) {
fprintf(stderr, "pthread f1 failed\n");
return EXIT_FAILURE;
}
rc = pthread_create(&t2, NULL, f2, NULL);
if (rc != 0) {
fprintf(stderr, "pthread f2 failed\n");
return EXIT_FAILURE;
}
pthread_join(t1, NULL);
pthread_join(t2, NULL);
puts("All pthreads finished.");
return 0;
}
(Output) ./launch busy
Spawned Child with Process Identification: 76463
Displaying /proc/[pid]/stat for spawned process below
76463 (busy) S 76462 76462 7030 34816 76462 1077936128 70 0 0 0 0 0 0 0 20 0 3 0 3607759 19496960 138 18446744073709551615 94205623705600 94205623706757 140724160799744 0 0 0 0 0 0 0 0 0 17 15 0 0 0 0 0 94205623717240 94205623717904 94205637992448 140724160806894 140724160806899 140724160806899 140724160806899 0
Resident set Size 138
Page Size 4096
Total Memory 65558824
Memory Consumption by Process 76463: 0.008622%
0
0
0
0
3607759
i's value has changed to 99.
t2 has changed the value of i to 99.
All pthreads finished.
child 76463 terminated
[ad_2]