| | |
Need a small help in a C program
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2007
Posts: 8
Reputation:
Solved Threads: 0
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.
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.
C Syntax (Toggle Plain Text)
#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 */
Last edited by WaltP; Dec 6th, 2007 at 9:25 pm. Reason: OK, I fixed the CODE tag.... Didn't seem to help, though
Use "[/code]" at the bottom, what is perl code doing ?
•
•
Join Date: Dec 2007
Posts: 8
Reputation:
Solved Threads: 0
Ok. Let me tell you what the whole program intends to do. There is an event list that contains events that would occur at predefined intervals. The events are - disk mean time to failure (m t t f), and disk mean time to repair(m t t r). We should also take care in our program that the repairs should be carried within a specified period of time interval, else it would be a data loss, which we do not want too early to happen in our program. We continue to run these events by taking inputs from the event list for these two events. We intend to run the loop for a possibly large number of iterations i.e failures and repairs, until we encounter disk data loos - i.e we reach a point comes when the disk was not repaired within the specified time and that would indicate a data loss i.e mean time to data loss(m t t d l). And run the program again to simulate using another set of values. The C code posted above might give you an idea as to what we were trying to accomplish.
a) Does the PERL program actually work ?
b) Post your latest C code as it currently stands. Don't forget to use the tags which everyone mentions, and don't forget to click on "preview post" or "go advanced" to make sure your code looks like this
and not like this, like you originally posted.
int main ( ) {
printf("Hello world\n" );
}
c) Post your reference PERL code "as is". That is, without all your attempts to mix in various bits of commented out C, or what have you.
b) Post your latest C code as it currently stands. Don't forget to use the tags which everyone mentions, and don't forget to click on "preview post" or "go advanced" to make sure your code looks like this
c Syntax (Toggle Plain Text)
int main ( ) { printf("Hello world\n" ); }
int main ( ) {
printf("Hello world\n" );
}
c) Post your reference PERL code "as is". That is, without all your attempts to mix in various bits of commented out C, or what have you.
![]() |
Similar Threads
- Program to change desktop background (C++)
- array program, dont know where to even begin! (C)
- help : external program wont execute. (Visual Basic 4 / 5 / 6)
- Creating a program to run under various OS (Pascal and Delphi)
- Small ATM banking program (C++)
- small program request (C++)
- Windows Wallpaper program (Computer Science)
- connection program for sybase (C)
Other Threads in the C Forum
- Previous Thread: Square Root approximation
- Next Thread: Qsort Pointer To Arrays Of Structs
Views: 8423 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






