ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Hi,
am new to programming.I am currently trying out a C program that can scale up/down child processes in order to achieve a load average as desired by the user.The input can be something like this:
"i want to generate a CPU load average of 15, and sustain it for 5 minutes."
can someone help me out with this interesting C problem?I am using an exponential program as a sample.
thanks you paulsm4 and sundialsvcs.I hv included my code here,but The following problem has arisen:
Though load generation does take place,the program often generates loads larger than that desired by the user.Mind you the load's only for the one minute LA Triplet and the load only increases,I have not included code to decrease load.would br grateful if you guys suggest modifications.
#include<stdio.h> /*a program to find out p^p,ptimes,that is if the input is 2,output gives,2^2^2.if
input is 3,output is 3^3^3,and so on */
#include<stdlib.h>
#include<math.h>
#include<unistd.h>
main()
{double load[3];
if (getloadavg(load, 3) != -1) /*getloadavg is the function used to calculate and obtain the load average*/
{
printf("load average : %f , %f , %f\n", load[0],load[1],load[2]);
}
float m;
int n=2;
long double g,h,i,pid,j,p,x=1,a=1;
printf("enter the number whose exponential's is to be found\n");
scanf("%Lf",&p);
printf("enter the load average to be generated for the 1st minute\n");
scanf("%f",&m); /*reading the input*/
printf("finding (p^p^p^p....ptimes)\n");
h=p;
while(h>0) /*calculating the final exponent value*/
{x=x*p;
h--;
}
for(i=x;i>0;i--) /*calculating the actual answer*/
{a=a*p;
}
printf("the value is:%Lf\n",a);
getloadavg(load,3);
while(load[0]<m)
{
n=n*2; /*increasing the number of child process*/
goto x;
}
/*
printf("enter the number of child processes to be spwaned\n");
scanf("%Lf",&n);
*/
x:
while(load[0]<m)
{
for(j=n;j>0;j--)
{
pid=fork(); /*creating the child process*/
if(pid>0)
wait(); /*making the parent process wait*/
if(pid <0)
{printf("error:cannot fork!!");
exit(1);
}
if (pid==0) /*child process*/
{ long double f=1;
getloadavg(load,3);
if(load[0]>m)
goto y;
printf("finding (p^p^p^p....ptimes)\n");
h=p;
while(h>0)
{x=x*p;
h--;
}
for(i=x;i>0;i--)
{f=f*p;
}
printf("the value is:%Lf\n",a);
if (getloadavg(load, 3) != -1)
{
printf("load average: %f, %f,%f\n",load[0],load[1],load[2]);
}
break;
}
Posting a sample of code might be helpful in getting your problem solved.
hello exvor,
I have posted the sample code.would be grateful if you could go through it and suggest modifications and solutions to my problem.
thanks!!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.