944,123 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 8556
  • C RSS
Dec 5th, 2007
0

Need a small help in a C program

Expand Post »
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.

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. // #include <system.h>
  5. #include <stdlib.h>
  6. #include <conio.h>
  7. #include <dos.h>
  8. #include <sys\timeb.h>
  9.  
  10.  
  11. float clock = 0.0; //time
  12. float evtype[10][10];
  13. float evdisk[10][10];
  14. int duration = 1000000000; //10^9
  15. int status[100]; // array
  16.  
  17. int losscount = 0;
  18. char type = 'E'; // error code
  19.  
  20. main()
  21. {
  22. int disk;
  23. float lambda;
  24. float mu, mttdl;
  25. int mttf, mttr;
  26. status[0]=1;
  27. status[1]=1;
  28. // int disk;
  29. printf("Enter disk MTTF (hours): ");
  30. scanf("%d", &mttf);
  31. printf("Enter disk MTTR (hours): ");
  32. scanf("%d", &mttr);
  33.  
  34. lambda = 1/mttf;
  35. mu = 1/mttr;
  36.  
  37. printf("Disk failure rate : %f \n", lambda);
  38. printf("Disk repair rate : %f \n", mu);
  39. printf("Simulation duration: %d \n", duration);
  40. /*
  41. schedule(duration, "X", 2);
  42.  
  43. schedule(exponential(lambda), "F", 0);
  44.  
  45. do {
  46. (clock, type, disk) = &nextevent; //needs looking into
  47. if (type == "F") {
  48. // disk failure
  49. failure(disk);
  50. } elsif (type == "R") {
  51. // disk repair
  52. repair(disk)
  53. } # if-else
  54. } while (type != "X"); // do-while
  55.  
  56. */
  57. //closing
  58. printf("SIMULATION RESULTS:\n");
  59. printf("Number of data losses is %d \n", losscount);
  60. mttdl = duration/losscount;
  61. printf("Rough estimate of MTTF is %f \n", mttdl); // end of simulation
  62.  
  63. // }
  64.  
  65.  
  66.  
  67.  
  68. failure(failed)
  69. int failed;
  70. {
  71. // extract disk ID
  72. status[failed] = 0;
  73. printf("Disk %d failed at time %f.\n", failed, clock);
  74. // check for data loss
  75. if (status[1 - failed] == 0) {
  76. losscount++;
  77. print("Data loss at time %f.\n", clock);
  78. } // if
  79. schedule(clock + exponential(mu), "R", failed);
  80. } // failure
  81.  
  82.  
  83. \\*************************************************************************
  84.  
  85.  
  86. repair(repaired)
  87. int repaired;
  88. {
  89. //(my $repaired) = @_; // extract disk ID
  90. status[repaired] = 1;
  91. printf("Disk %d was repaired at time %d.\n", repaired, clock);
  92. schedule(clock + exponential(lambda), "F", repaired);
  93. } // repair
  94.  
  95. /*schedule(thistime, thistype, thisdisk)
  96. float thistime;
  97. char thistype;
  98. int thisdisk;
  99. {
  100. //extract three parameters
  101. //(my $thistime, my $thistype, my $thisdisk) = @_;
  102. $evtype{$thistime} = $thistype; //pascal associative array
  103. $evdisk{$thistime} = $thisdisk; //pascal associative array
  104. } // schedule
  105.  
  106. nextevent() {
  107. int skeys[];
  108. int thattime, thattype, thatdisk;
  109. my @skeys =
  110. sort{$a <=> $b} (keys %evtype);
  111. my $thattime = shift(@skeys);
  112. my $thattype = $evtype{$thattime};
  113. my $thatdisk = $evdisk{$thattime};
  114. delete($evtype{$thattime});
  115. delete($evdisk{$thattime});
  116. ($thattime, $thattype, $thatdisk);
  117. } nextevent
  118.  
  119. exponential() {
  120. (my $rate) = @_;
  121. - log(rand(1))/$rate;
  122. } // exponential
  123.  
  124. */
Last edited by WaltP; Dec 6th, 2007 at 9:25 pm. Reason: OK, I fixed the CODE tag.... Didn't seem to help, though
Similar Threads
Reputation Points: 7
Solved Threads: 0
Newbie Poster
vkiller is offline Offline
8 posts
since Dec 2007
Dec 5th, 2007
0

Re: Need a small help in a C program

any help would be appreciated! thanks
Reputation Points: 7
Solved Threads: 0
Newbie Poster
vkiller is offline Offline
8 posts
since Dec 2007
Dec 5th, 2007
0

Re: Need a small help in a C program

Sure, figuring out how to use code tags would be high on your list.
You were a lot closer than most people manage, but you still didn't make it.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 5th, 2007
0

Re: Need a small help in a C program

But do you think there is a way I can manage to get what I actually need? Or is there a better way of doing it? Looking forward for some help to finish this off.. thanks!
Reputation Points: 7
Solved Threads: 0
Newbie Poster
vkiller is offline Offline
8 posts
since Dec 2007
Dec 6th, 2007
0

Re: Need a small help in a C program

Probably, but I really couldn't care less until either you or a mod fixes the post so that I can read it without going blind in the process.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 6th, 2007
0

Re: Need a small help in a C program

I am sorry I dint understand about the 'fixing' part. And if I can, how should I go about fixing it?
thanks again!
Reputation Points: 7
Solved Threads: 0
Newbie Poster
vkiller is offline Offline
8 posts
since Dec 2007
Dec 6th, 2007
0

Re: Need a small help in a C program

Use "[/code]" at the bottom, what is perl code doing ?
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Dec 7th, 2007
0

Re: Need a small help in a C program

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.
Reputation Points: 7
Solved Threads: 0
Newbie Poster
vkiller is offline Offline
8 posts
since Dec 2007
Dec 11th, 2007
0

Re: Need a small help in a C program

I am still tryng to figure this out and seeking help.
thanks!
Reputation Points: 7
Solved Threads: 0
Newbie Poster
vkiller is offline Offline
8 posts
since Dec 2007
Dec 11th, 2007
0

Re: Need a small help in a C program

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
  1. int main ( ) {
  2. printf("Hello world\n" );
  3. }
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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Square Root approximation
Next Thread in C Forum Timeline: Qsort Pointer To Arrays Of Structs





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC