Need a small help in a C program

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2007
Posts: 8
Reputation: vkiller is an unknown quantity at this point 
Solved Threads: 0
vkiller vkiller is offline Offline
Newbie Poster

Need a small help in a C program

 
0
  #1
Dec 5th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 8
Reputation: vkiller is an unknown quantity at this point 
Solved Threads: 0
vkiller vkiller is offline Offline
Newbie Poster

Re: Need a small help in a C program

 
0
  #2
Dec 5th, 2007
any help would be appreciated! thanks
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Need a small help in a C program

 
0
  #3
Dec 5th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 8
Reputation: vkiller is an unknown quantity at this point 
Solved Threads: 0
vkiller vkiller is offline Offline
Newbie Poster

Re: Need a small help in a C program

 
0
  #4
Dec 5th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Need a small help in a C program

 
0
  #5
Dec 6th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 8
Reputation: vkiller is an unknown quantity at this point 
Solved Threads: 0
vkiller vkiller is offline Offline
Newbie Poster

Re: Need a small help in a C program

 
0
  #6
Dec 6th, 2007
I am sorry I dint understand about the 'fixing' part. And if I can, how should I go about fixing it?
thanks again!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,861
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 120
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: Need a small help in a C program

 
0
  #7
Dec 6th, 2007
Use "[/code]" at the bottom, what is perl code doing ?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 8
Reputation: vkiller is an unknown quantity at this point 
Solved Threads: 0
vkiller vkiller is offline Offline
Newbie Poster

Re: Need a small help in a C program

 
0
  #8
Dec 7th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 8
Reputation: vkiller is an unknown quantity at this point 
Solved Threads: 0
vkiller vkiller is offline Offline
Newbie Poster

Re: Need a small help in a C program

 
0
  #9
Dec 11th, 2007
I am still tryng to figure this out and seeking help.
thanks!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Need a small help in a C program

 
0
  #10
Dec 11th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 8423 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC