I want to write a c program in linux or windows environment(I m not sure about t
ID: 3697819 • Letter: I
Question
I want to write a c program in linux or windows environment(I m not sure about this)
And in this program C language program will extend the Linux Kernel
implementation and extract the number of Voluntary Context Switches and Involuntary
Context Switches of a process.
This program will print these values
Pid : 123
nr_voluntary_switches : 10
nr_involuntary_switches : 12
and in this program we have do our implementation and we cannot use any c function or any linux command
I m trying to write this program but could not succeed yet
#include<time.h>
#include<sched.h>
#include<iostream.h>
void main()
{
struct rusage{
long ru_maxrss;
long ru_ixrss;
long ru_idrss;
long ru_isrss;
long ru_minflt;
long ru_majflt;
long ru_nswap;
long ru_inblock;
long ru_oublock;
long ru_insgsnd;
long ru_msgrcv;
long ru_nsignals;
long ru_nvcsw;
long ru_nivcsw;
};
cout<<"a"<<rusage->nvcsw;
}
I have to modify the kernel files as well, there are some hints about this program which I m not getting
(Hint: getrusage() system call provides the system resource information of a process in struct
named task_struct, to get desired values, modify this struct along with INIT_TASK macro;
both are available in sched.h )
Explanation / Answer
#include <stdio.h>
#include <time.h>
#include <stdint.h>
#include <sys/time.h>
#include <sys/resource.h>
int main(int argc, char *argv[]) {
struct rusage ru;
getrusage(RUSAGE_SELF, &ru);
printf(" voluntary context switches %ld,involuntary context switches %ld ", ru.ru_nvcsw,ru.ru_nivcsw)
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.