fork download
  1. /*This code is in RTOS environment*/
  2. /*Title: Encapsulating a Semaphore.*/
  3. /*This is correct version*/
  4.  
  5. /*This code rather than letting just any code that
  6.   wants the value of the lSecondsToday variable read
  7.   it directly and hoping for the best, this construction
  8.   forces any code that wants to know the value of lSecondsToday
  9.   to call lSecondsSinceMidnight to get it*/
  10.  
  11. /*Once lSecondsSinceMidnight uses the semaphore correctly,
  12.   this semaphore will cause no more bugs*/
  13.  
  14. // File: tmrtask.c
  15.  
  16. long int lSecondsToday; //global variable
  17.  
  18. void vTimerTask(void){
  19. .
  20. .
  21. .
  22. GetSemaphore(SEMAPHORE_TIME_OF_DAY); //this function is provided by RTOS vendor
  23. ++lSecondsToday;
  24. if(lSecondsToday == 60 * 60 * 24){
  25. lSecondsToday = 0L;
  26. }
  27. GiveSemaphore(SEMAPHORE_TIME_OF_DAY);
  28. .
  29. .
  30. .
  31. }
  32.  
  33. long lSecondSinceMidnight(void){
  34. long lReturnValue;
  35. GetSemaphore(SEMAPHORE_TIME_OF_DAY);
  36. lReturnValue = lSecondsToday;
  37. GiveSemaphore(SEMAPHORE_TIME_OF_DAY);
  38. return (lReturnValue);
  39. }
  40. -----------------------------------------------------
  41. // File: hacker.c
  42.  
  43. long lSecondsSinceMidnight(void);
  44.  
  45. void vHackerTask(void){
  46. .
  47. .
  48. .
  49. lDeadline = lSecondSinceMidnight() + 1800L;
  50. .
  51. .
  52. .
  53. if(lSecondSinceMidnight() > 3600 * 12)
  54. .
  55. .
  56. .
  57. }
  58. --------------------------------------------------------
  59. // File: junior.c
  60.  
  61. long lSecondsSinceMidnight(void);
  62.  
  63. void vJuniorProgrammerTask(void){
  64. long lTemp;
  65. .
  66. .
  67. .
  68. lTemp = lSecondSinceMidnight();
  69. for(l = lTemp; l < lTemp + 10; ++l) // l is not mentioned by author
  70. .
  71. .
  72. .
  73. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:16: error: class, interface, or enum expected
long int lSecondsToday;	//global variable
^
Main.java:18: error: class, interface, or enum expected
void vTimerTask(void){
^
Main.java:23: error: class, interface, or enum expected
	++lSecondsToday;
	^
Main.java:24: error: class, interface, or enum expected
	if(lSecondsToday == 60 * 60 * 24){
	^
Main.java:26: error: class, interface, or enum expected
	}
	^
Main.java:28: error: class, interface, or enum expected
	.
	^
Main.java:35: error: class, interface, or enum expected
	GetSemaphore(SEMAPHORE_TIME_OF_DAY);
	^
Main.java:36: error: class, interface, or enum expected
	lReturnValue = lSecondsToday;
	^
Main.java:37: error: class, interface, or enum expected
	GiveSemaphore(SEMAPHORE_TIME_OF_DAY);
	^
Main.java:38: error: class, interface, or enum expected
	return (lReturnValue);
	^
Main.java:39: error: class, interface, or enum expected
}
^
Main.java:45: error: class, interface, or enum expected
void vHackerTask(void){
^
Main.java:50: error: class, interface, or enum expected
	.
	^
Main.java:63: error: class, interface, or enum expected
void vJuniorProgrammerTask(void){
^
Main.java:65: error: class, interface, or enum expected
	.
	^
Main.java:69: error: class, interface, or enum expected
	for(l = lTemp; l < lTemp + 10; ++l)	// l is not mentioned by author
	^
Main.java:69: error: class, interface, or enum expected
	for(l = lTemp; l < lTemp + 10; ++l)	// l is not mentioned by author
	               ^
Main.java:69: error: class, interface, or enum expected
	for(l = lTemp; l < lTemp + 10; ++l)	// l is not mentioned by author
	                               ^
18 errors
stdout
Standard output is empty