I am workring on a program that can simulate an array of disks and from an event list, record their mean time to failure (mttf), mean time to repair(mttr) and stop the program when it encounters dataloss. so we would also need to record the mean time to data loss (mttdl).
I wrote the following code by converting a perl code into c with help from a friend. in the bottom part of the code you would see ************** beyond that we have all perl code. needs to be done in C. Need to work on the nextevent() function. In the perl code, it is asking for three values- my $thistime, my $thistype, my $thisdisk. I think if we make 2 variables global, we can get $disk. Rest 2 variables are already in the global category. Please note that 'keys' and 'shift' are perl functions working on associative arrays, We have to find a way around those too.All $ variables are perl so it means that code is untouched.
#include <stdio.h>
#include <string.h>
// #include <system.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <sys\timeb.h>
float clock = 0.0; //time
float evtype[10][10];
float evdisk[10][10];
int duration = 1000000000; //10^9
int status[100]; // array
int losscount = 0;
char type = 'E'; // error code
main()
{
int disk;
float lambda;
float mu, mttdl;
int mttf, mttr;
status[0]=1;
status[1]=1;
// int disk;
printf("Enter disk MTTF (hours): ");
scanf("%d", &mttf);
printf("Enter disk MTTR (hours): ");
scanf("%d", &mttr);
lambda = 1/mttf;
mu = 1/mttr;
printf("Disk failure rate : %f \n", lambda);
printf("Disk repair rate : %f \n", mu);
printf("Simulation duration: %d \n", duration);
/*
schedule(duration, "X", 2);
schedule(exponential(lambda), "F", 0);
do {
(clock, type, disk) = &nextevent; //needs looking into
if (type == "F") {
// disk failure
failure(disk);
} elsif (type == "R") {
// disk repair
repair(disk)
} # if-else
} while (type != "X"); // do-while
*/
//closing
printf("SIMULATION RESULTS:\n");
printf("Number of data losses is %d \n", losscount);
mttdl = duration/losscount;
printf("Rough estimate of MTTF is %f \n", mttdl); // end of simulation
// }
failure(failed)
int failed;
{
// extract disk ID
status[failed] = 0;
printf("Disk %d failed at time %f.\n", failed, clock);
// check for data loss
if (status[1 - failed] == 0) {
losscount++;
print("Data loss at time %f.\n", clock);
} // if
schedule(clock + exponential(mu), "R", failed);
} // failure
\\*************************************************************************
repair(repaired)
int repaired;
{
//(my $repaired) = @_; // extract disk ID
status[repaired] = 1;
printf("Disk %d was repaired at time %d.\n", repaired, clock);
schedule(clock + exponential(lambda), "F", repaired);
} // repair
/*schedule(thistime, thistype, thisdisk)
float thistime;
char thistype;
int thisdisk;
{
//extract three parameters
//(my $thistime, my $thistype, my $thisdisk) = @_;
$evtype{$thistime} = $thistype; //pascal associative array
$evdisk{$thistime} = $thisdisk; //pascal associative array
} // schedule
nextevent() {
int skeys[];
int thattime, thattype, thatdisk;
my @skeys =
sort{$a <=> $b} (keys %evtype);
my $thattime = shift(@skeys);
my $thattype = $evtype{$thattime};
my $thatdisk = $evdisk{$thattime};
delete($evtype{$thattime});
delete($evdisk{$thattime});
($thattime, $thattype, $thatdisk);
} nextevent
exponential() {
(my $rate) = @_;
- log(rand(1))/$rate;
} // exponential
*/