Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.14 KB | None | 0 0
  1. /*TODO: really, should try to use RT for management, could prevent unstability*/
  2. /*Supress warning*/
  3. #define _SVID_SOURCE /*WTF? for shm.h*/
  4.  
  5. #include <time.h>
  6. #include <stdio.h>
  7. #include <sys/shm.h>
  8. #include <sys/types.h>
  9. #include <sys/wait.h>
  10. #include <unistd.h>
  11. #include <errno.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sched.h>
  15. #include <sys/resource.h>
  16. #include "hw2_syscalls.h"
  17.  
  18. /*TODO: start ask should we put it somewhere*/
  19. #define SCHED_SHORT 5
  20. struct my_sched_param
  21. {
  22. int sched_priority;
  23. int requested_time;
  24. };
  25. /*TODO: finish ask */
  26.  
  27. #define DO_SYS(syscall) do { \
  28. if ( -1 == (syscall))\
  29. {\
  30. perror(#syscall);\
  31. fprintf(stderr, "Occured on line %d\n", __LINE__);\
  32. exit(1);\
  33. }\
  34. } while (0)
  35.  
  36. #define STUCK_HEURISTIC(sec) { int i; for (i = 0; i < (sec) * 5e8; ++i); /*Could be optimized out better not to use -O*/ }
  37. #define STUCK(ms) { clock_t t; for (t = clock(); (clock() - t) * 1000 <= ms * CLOCKS_PER_SEC; ); }
  38.  
  39. #define ASSERT_MESSAGE(condition, message, ...) do {\
  40. if (!(condition))\
  41. {\
  42. fprintf(stderr, "\x1b[31m" message "\x1b[0m", ##__VA_ARGS__);\
  43. exit(1);\
  44. }\
  45. } while (0)
  46.  
  47. #define ASSERT(condition) ASSERT_MESSAGE((condition), "Asseration failed \"%s\" on line %d\n", #condition, __LINE__)
  48.  
  49. #define RUN_TEST(testName) do {\
  50. fflush(stdout);\
  51. int exitCode = (testName)();\
  52. if (0 == exitCode)\
  53. {\
  54. printf("\x1b[32m" "%s is passed\n" "\x1b[0m", #testName);\
  55. }\
  56. else\
  57. {\
  58. printf("\x1b[31m" "%s is failed (exit code = %d)\n" "\x1b[0m", exitCode, #testName);\
  59. }\
  60. fflush(stdout);\
  61. memset(getSharedMemory(0), 0, sizeof(SharedMemory));\
  62. } while(0)
  63.  
  64. FILE *debugFile;
  65.  
  66. #ifdef DEBUG_OUTPUT
  67.  
  68. #define DEBUG(format, ...) do{\
  69. printf("Process %d Line %d says: " format "\n", getpid(), __LINE__, ##__VA_ARGS__);\
  70. fprintf(debugFile, "Process %d Line %d says: " format "\n", getpid(), __LINE__, ##__VA_ARGS__);\
  71. fflush(debugFile);\
  72. } while(0)
  73.  
  74. #define DEBUG_INIT do{\
  75. FILE *klogdPIDFile = fopen("/var/run/klogd.pid", "r");\
  76. debugFile = fopen("logWithBlackJackAndHookers.txt", "w");\
  77. fprintf(debugFile, "Log started\n");\
  78. fflush(debugFile);\
  79. if (klogdPIDFile)\
  80. {\
  81. long pid;\
  82. char commandKill[4+1+19+1];\
  83. fscanf(klogdPIDFile, "%d", &pid);\
  84. fclose(klogdPIDFile);\
  85. sprintf(commandKill, "kill %d", pid);\
  86. DEBUG("%s", commandKill);\
  87. DO_SYS(system(commandKill));\
  88. }\
  89. sleep(1);\
  90. char commandStart[5+1+2+1+256] = "klogd -f ";\
  91. DO_SYS((int)getcwd(commandStart + strlen(commandStart), sizeof(commandStart) - strlen(commandStart)));\
  92. sprintf(commandStart, "%s/kernel.log", commandStart);\
  93. DEBUG("%s", commandStart);\
  94. DO_SYS(system(commandStart));\
  95. }while(0)
  96. #else
  97. #define DEBUG(format, ...)
  98. #define DEBUG_INIT
  99. #endif
  100.  
  101. #define NUMBER_OF_CHILDREN 18
  102.  
  103. /*Suppress warning*/
  104. int nice(int); /*TODO: why the hell it is not defined in unistd.h?*/
  105.  
  106. /*
  107. If you see
  108. sharedMemoryIdentifier = shmget(key, sizeof(SharedMemory), (create ? IPC_CREAT : 0) | 0666): Invalid argument
  109. please change it
  110. If you can explain why the hell it happens, please tell me
  111. */
  112. #define PATH_NAME "/tmp"
  113. #define PROJECT_ID (42 + NUMBER_OF_CHILDREN)
  114. key_t key; // TODO: why can't use ftok somehow
  115.  
  116.  
  117. typedef struct {
  118. int startedBeingStuck[NUMBER_OF_CHILDREN], finishedBeingStuck[NUMBER_OF_CHILDREN];
  119. int numberOfShortProcesses;
  120. time_t childStartTime;
  121. time_t childFinishTime;
  122. time_t fatherStartTime;
  123. time_t fatherFinishTime;
  124. } SharedMemory;
  125.  
  126. static SharedMemory* getSharedMemory(int create)
  127. {
  128. // key_t key;
  129. // DO_SYS((int)(key == ftok(PATH_NAME, PROJECT_ID)));
  130. // DEBUG("%d", key);
  131.  
  132. int sharedMemoryIdentifier;
  133. DEBUG("create=%d, key=%x, sizeof(key)=%u", create, key, sizeof(key));
  134. DO_SYS(sharedMemoryIdentifier = shmget(key, sizeof(SharedMemory), (create ? IPC_CREAT : 0) | 0666));
  135.  
  136. void *result;
  137. DO_SYS((int)(result = shmat(sharedMemoryIdentifier, NULL, 0)));
  138.  
  139. if (create)
  140. {
  141. memset(result, 0, sizeof(SharedMemory));
  142. }
  143.  
  144. return (SharedMemory*)result;
  145. }
  146.  
  147. #define MAKE_SHORT(pid, time) do {\
  148. struct my_sched_param schedParams = { \
  149. .sched_priority = 42/*Todo ask must we check or must we not check: -42*/,\
  150. .requested_time = (time),\
  151. };\
  152. DO_SYS(sched_setscheduler((pid), SCHED_SHORT, (struct sched_param*)&schedParams)); \
  153. } while (0)
  154.  
  155. #define MAKE_RR(pid) do {\
  156. struct my_sched_param schedParams = { \
  157. .sched_priority = 50,\
  158. .requested_time = 42,\
  159. };\
  160. DO_SYS(sched_setscheduler((pid), SCHED_RR, (struct sched_param*)&schedParams)); \
  161. } while (0)
  162.  
  163. #define MAKE_OTHER(pid) do {\
  164. struct my_sched_param schedParams = { \
  165. .sched_priority = 0,\
  166. .requested_time = 42,\
  167. };\
  168. DO_SYS(sched_setscheduler((pid), SCHED_OTHER, (struct sched_param*)&schedParams)); \
  169. } while (0)
  170.  
  171. static int allChildrenAreNice(pid_t children[])
  172. {
  173. for (int childIndex = 0; childIndex < NUMBER_OF_CHILDREN; ++childIndex)
  174. {
  175. if (getpriority(PRIO_PROCESS, children[childIndex]) == 0)
  176. {
  177. return 0;
  178. }
  179. }
  180. return 1;
  181. }
  182.  
  183. static int becameForkBecameShortAndMakeChildrenShortToCheckOrder(int changeNice)
  184. {
  185. const int requestedTime = 3000;
  186. const int runTime = requestedTime >> 1;
  187.  
  188. pid_t child;
  189. int returnValue;
  190.  
  191. DO_SYS(child = fork());
  192.  
  193. ASSERT(-1 == is_short(getpid()));
  194. if (0 == child)
  195. {
  196. pid_t children[NUMBER_OF_CHILDREN];
  197.  
  198. ASSERT_MESSAGE(NUMBER_OF_CHILDREN < 20, "Not so generic, give me less than 20 children"); /*TODO make cmpile time*/
  199.  
  200. for (int childIndex = 0; childIndex < NUMBER_OF_CHILDREN; ++childIndex)
  201. {
  202. ASSERT(-1 == is_short(getpid()));
  203. DO_SYS(children[childIndex] = fork());
  204.  
  205. if (0 == children[childIndex])
  206. {
  207. DEBUG("%d was born in a hurricane", getpid());
  208.  
  209. if (changeNice)
  210. {
  211. DEBUG("before nice to %d", NUMBER_OF_CHILDREN - childIndex);
  212. DO_SYS(nice(NUMBER_OF_CHILDREN - childIndex));
  213. DEBUG("did nice: %d", getpriority(PRIO_PROCESS, getpid()));
  214. }
  215. else
  216. {
  217. DEBUG("more 'nice' that father by %d", 1);
  218. DO_SYS(nice(1));
  219. DEBUG("did nice: %d", getpriority(PRIO_PROCESS, getpid()));
  220. }
  221.  
  222. SharedMemory *memory = getSharedMemory(0);
  223. while (NUMBER_OF_CHILDREN != memory->numberOfShortProcesses)
  224. {
  225. sched_yield();
  226. }
  227.  
  228. pid_t pid = getpid();
  229.  
  230. DEBUG("nosp: %d", memory->numberOfShortProcesses);
  231. ASSERT_MESSAGE(memory->numberOfShortProcesses == NUMBER_OF_CHILDREN,
  232. "Seems like I have not yielded CPU while becaming less nice or interruped my father with highter priority");
  233. ASSERT(is_short(pid));
  234. if (changeNice)
  235. {
  236. ASSERT_MESSAGE(childIndex + 1 == NUMBER_OF_CHILDREN || memory->finishedBeingStuck[childIndex+1],
  237. "started before more prioritirised short finished");
  238. ASSERT_MESSAGE(childIndex == 0 || !memory->startedBeingStuck[childIndex-1],
  239. "less priored started before me");
  240. }
  241. else
  242. {
  243. ASSERT_MESSAGE(childIndex == 0 || memory->finishedBeingStuck[childIndex-1],
  244. "While becoming short pid %d should have entered before me but have not\n", children[childIndex-1]);
  245. ASSERT_MESSAGE(childIndex + 1 == NUMBER_OF_CHILDREN || !memory->startedBeingStuck[childIndex+1],
  246. "While becoming short should have entered after me but have not\n");
  247. }
  248.  
  249.  
  250. DEBUG("personally I have got %d ms", short_remaining_time(getpid()));
  251. ASSERT(is_short(getpid()));
  252. ASSERT(-1 == was_short(getpid()) && EINVAL == errno);
  253.  
  254. DEBUG("set start");
  255. memory->startedBeingStuck[childIndex] = 1;
  256. STUCK(runTime);
  257. memory->finishedBeingStuck[childIndex] = 1;
  258. DEBUG("finished");
  259. ASSERT(1 == is_short(getpid()));
  260. ASSERT(-1 == was_short(getpid()) && EINVAL == errno);
  261. DEBUG("I've got another %d ms, but gonna die for my government", short_remaining_time(getpid()));
  262. exit(0);
  263. }
  264.  
  265. ASSERT(-1 == is_short(children[childIndex]));
  266. DEBUG("I love my child: %d, he has %d, I've got %d", children[childIndex], short_remaining_time(children[childIndex]), short_remaining_time(getpid()));
  267. }
  268.  
  269. ASSERT(-1 == is_short(getpid()));
  270.  
  271. DEBUG("Going to wait for bastards to do nice");
  272. while(!allChildrenAreNice(children))
  273. {
  274. sched_yield();
  275. }
  276. DEBUG("Everyone is nice");
  277.  
  278. MAKE_SHORT(getpid(), 3000);
  279. DEBUG("My time after forks: %d ms", short_remaining_time(getpid()));
  280. SharedMemory *memory = getSharedMemory(0);
  281. DEBUG("Making my bastards short");
  282. for (int childIndex = 0; childIndex < NUMBER_OF_CHILDREN; ++childIndex)
  283. {
  284. MAKE_SHORT(children[childIndex], requestedTime);
  285. ASSERT(is_short(children[childIndex]));
  286. DEBUG("Made %d-th child %d short, got %d ms", childIndex, children[childIndex], short_remaining_time(getpid()));
  287. ++memory->numberOfShortProcesses;
  288. }
  289. DEBUG("All my bastards are short, I am is_short: %d", is_short(getpid()));
  290. ASSERT_MESSAGE(1 == is_short(getpid()), "Father is not short, could cause other problems");
  291.  
  292. for (int childIndex = 0; childIndex < NUMBER_OF_CHILDREN; ++childIndex)
  293. {
  294. int returnValue;
  295.  
  296. DEBUG("So am I, still waiting, for this world to stop hating");
  297. ASSERT(waitpid(children[childIndex], &returnValue, 0));
  298. DEBUG("finished wait for child %d: exit code=%d", children[childIndex], WEXITSTATUS(returnValue));
  299. ASSERT(0 == WEXITSTATUS(returnValue));
  300.  
  301. ASSERT(memory->finishedBeingStuck[childIndex]);
  302. }
  303.  
  304. exit (0);
  305. }
  306.  
  307. DO_SYS(waitpid(child, &returnValue, 0));
  308. ASSERT(0 == WEXITSTATUS(returnValue));
  309.  
  310. return 0;
  311. }
  312.  
  313. int testToCheckDifferentPriorities()
  314. {
  315. return becameForkBecameShortAndMakeChildrenShortToCheckOrder(1);
  316. }
  317.  
  318. int testToCheckFifoOrderOfTheSamePriorities()
  319. {
  320. return becameForkBecameShortAndMakeChildrenShortToCheckOrder(0);
  321. }
  322.  
  323. #define MS_EPS 3
  324.  
  325. int testToCheckThatSonStartsBefore()
  326. {
  327. const int requestedTime = 3000;
  328. pid_t child;
  329.  
  330. DO_SYS(child = fork());
  331.  
  332. if (0 == child)
  333. {
  334. DEBUG("going to be short");
  335. MAKE_SHORT(getpid(), requestedTime);
  336. pid_t child;
  337.  
  338. DEBUG("going to fork");
  339. DO_SYS(child = fork());
  340.  
  341. time_t startTime = time(NULL); /*bad bad bad*/
  342. STUCK(requestedTime / 3);
  343.  
  344. SharedMemory *memory = getSharedMemory(0);
  345.  
  346. if (child)
  347. {
  348. int returnValue;
  349. ASSERT_MESSAGE(memory->childStartTime, "Seems like my son has not started yet!");
  350. ASSERT_MESSAGE(memory->childFinishTime, "Seems like my son has not finished yet!");
  351. memory->fatherStartTime = startTime;
  352. memory->fatherFinishTime = time(NULL);
  353.  
  354. DO_SYS(waitpid(child, &returnValue, 0));
  355. ASSERT(0 == WEXITSTATUS(returnValue));
  356. DEBUG("My child is dead, see no point in living");
  357. }
  358. else
  359. {
  360. ASSERT_MESSAGE(MS_EPS > abs(short_remaining_time(getppid() - short_remaining_time(getpid()))), "Some problem with remaining time");
  361. ASSERT_MESSAGE(!memory->fatherStartTime, "Seems like my father already started!");
  362. ASSERT_MESSAGE(!memory->fatherFinishTime, "Seems like my father already finished!");
  363. memory->childStartTime = startTime;
  364. memory->childFinishTime = time(NULL);
  365. DEBUG("Suicide");
  366. }
  367. exit(0);
  368. }
  369.  
  370. int returnValue;
  371. DO_SYS(waitpid(child, &returnValue, 0));
  372. ASSERT(0 == WEXITSTATUS(returnValue));
  373.  
  374. SharedMemory *memory = getSharedMemory(0);
  375. ASSERT(memory->childFinishTime && memory->fatherStartTime);
  376.  
  377. return 0;
  378. }
  379.  
  380. int forkBecameShortMakeShortOverdueAndCheckOrder()
  381. {
  382. const int minShortOverdueRemainingTime = 10;
  383. const int requestedTime = minShortOverdueRemainingTime / 2 + 1 + 1500;
  384.  
  385. pid_t child;
  386.  
  387. DO_SYS(child = fork());
  388.  
  389. if (0 == child)
  390. {
  391.  
  392. pid_t children[NUMBER_OF_CHILDREN];
  393. DEBUG("Going to be RR");
  394. MAKE_RR(getpid());
  395. DEBUG("Became RR");
  396. for (int childIndex = 0; childIndex < NUMBER_OF_CHILDREN; ++childIndex)
  397. {
  398. DEBUG("Going to give birth");
  399. DO_SYS(children[childIndex] = fork());
  400.  
  401. if (0 == children[childIndex])
  402. {
  403. DEBUG("Going to wait to become short");
  404. while (-1 == is_short(getpid()));
  405. DEBUG("Became short");
  406.  
  407. ASSERT(1 == is_short(getpid()));
  408. SharedMemory *memory = getSharedMemory(0);
  409. DEBUG("Want to be overdue");
  410. while(1 == is_short(getpid()));
  411. DEBUG("I did it! I am overdue");
  412. memory->startedBeingStuck[childIndex] = time(NULL);
  413. ASSERT_MESSAGE(childIndex == 0 || memory->finishedBeingStuck[childIndex-1], "The guy %d in fifo before me is not done yet", children[childIndex-1]);
  414. STUCK(short_remaining_time(getpid()) > minShortOverdueRemainingTime);
  415. ASSERT_MESSAGE(childIndex+1 == NUMBER_OF_CHILDREN || !memory->startedBeingStuck[childIndex+1],
  416. "That bitch in fifo after me started before I was finished");
  417. memory->finishedBeingStuck[childIndex] = time(NULL);
  418. exit(0);
  419. }
  420. DEBUG("Did birth");
  421. }
  422.  
  423. DEBUG("GOING TO MAKE BASTARDS SHORT");
  424. for (int childIndex = 0; childIndex < NUMBER_OF_CHILDREN; ++childIndex)
  425. {
  426. DEBUG("Making %d short", children[childIndex]);
  427. MAKE_OTHER(children[childIndex]);
  428. MAKE_SHORT(children[childIndex], requestedTime);
  429. DEBUG("Makde %d short", children[childIndex]);
  430. }
  431.  
  432. DEBUG("Going to wait for bastards");
  433. for (int childIndex = 0; childIndex < NUMBER_OF_CHILDREN; ++childIndex)
  434. {
  435. int returnValue, childWasShort, childIsShort;
  436. DEBUG("Going to wait for %d", children[childIndex]);
  437. waitpid(children[childIndex], &returnValue, 0);
  438. DEBUG("Returned");
  439. ASSERT(0 == WEXITSTATUS(returnValue));
  440. }
  441.  
  442. exit(0);
  443. }
  444.  
  445. int returnValue;
  446. DO_SYS(waitpid(child, &returnValue, 0));
  447. ASSERT(0 == WEXITSTATUS(returnValue));
  448.  
  449. return 0;
  450. }
  451.  
  452. int testToCheckTimeAfterForkOfShort()
  453. {
  454. pid_t child;
  455. int requestedTime = 1234;
  456.  
  457. DO_SYS(child = fork());
  458.  
  459. if (0 == child)
  460. {
  461. MAKE_SHORT(getpid(), requestedTime);
  462. int lastFatherTime;
  463. DO_SYS(lastFatherTime = short_remaining_time(getpid()));
  464. DEBUG("%d", lastFatherTime);
  465. ASSERT(lastFatherTime >= 0 && lastFatherTime >= requestedTime - MS_EPS);
  466.  
  467. DO_SYS(child = fork());
  468.  
  469. int myRemainingTime = short_remaining_time(getpid());
  470. ASSERT_MESSAGE(abs(myRemainingTime - lastFatherTime / 2) < MS_EPS, "My remaining time should be about %d and not %d\n", lastFatherTime/2, myRemainingTime);
  471. ASSERT(SCHED_SHORT == sched_getscheduler(0));
  472. struct my_sched_param param;
  473. DO_SYS(sched_getparam(0, (struct sched_param*)&param));
  474. ASSERT(requestedTime == param.requested_time);
  475.  
  476. if (0 != child)
  477. {
  478. int returnValue;
  479. waitpid(child, &returnValue, 0);
  480. ASSERT(0 == WEXITSTATUS(returnValue));
  481. }
  482.  
  483. exit(0);
  484. }
  485.  
  486. int returnValue;
  487. DO_SYS(waitpid(child, &returnValue, 0));
  488. ASSERT(0 == WEXITSTATUS(returnValue));
  489.  
  490. return 0;
  491. }
  492.  
  493. int testToCheckNewSysCalls()
  494. {
  495. pid_t child;
  496.  
  497. DO_SYS(child = fork());
  498.  
  499. if (0 == child)
  500. {
  501. const int runTime = 20;
  502. const int requestedTime = runTime * 2 + MS_EPS;
  503.  
  504. struct my_sched_param param;
  505.  
  506. ASSERT(-1 == is_short(getpid()) && EINVAL == errno);
  507. ASSERT(-1 == short_remaining_time(getpid()) && EINVAL == errno);
  508. ASSERT(0 == was_short(getpid()));
  509. ASSERT(0 == sched_getparam(getpid(), (struct sched_param*)&param));
  510. param.requested_time = 3001;
  511. ASSERT(-1 == sched_setscheduler(0, SCHED_SHORT, (struct sched_param*)&param) && EINVAL == errno);
  512. param.requested_time = 0;
  513. ASSERT(-1 == sched_setscheduler(0, SCHED_SHORT, (struct sched_param*)&param) && EINVAL == errno);
  514. param.requested_time = -1;
  515. ASSERT(-1 == sched_setscheduler(0, SCHED_SHORT, (struct sched_param*)&param) && EINVAL == errno);
  516.  
  517. MAKE_SHORT(getpid(), requestedTime);
  518.  
  519. ASSERT_MESSAGE(abs(requestedTime - short_remaining_time(getpid())) < MS_EPS,
  520. "%d ms were given, but just %d left. Probably conversion problem", requestedTime, short_remaining_time(getpid()));
  521. ASSERT(1 == is_short(getpid()));
  522. ASSERT(-1 == was_short(getpid()) && EINVAL == errno);
  523. ASSERT(0 == sched_getparam(getpid(), (struct sched_param*)&param));
  524. ASSERT(requestedTime == param.requested_time);
  525. param.sched_priority = 0;
  526. ASSERT(-1 == sched_setscheduler(0, SCHED_OTHER, (struct sched_param*)&param) && errno == EPERM);
  527. param.requested_time = 3001;
  528. ASSERT(-1 == sched_setparam(0, (struct sched_param*)&param) && EINVAL == errno);
  529. ASSERT(-1 == sched_setscheduler(0, SCHED_SHORT, (struct sched_param*)&param) && EINVAL == errno);
  530. param.requested_time = 0;
  531. ASSERT(-1 == sched_setparam(0, (struct sched_param*)&param) && EINVAL == errno);
  532. ASSERT(-1 == sched_setscheduler(0, SCHED_SHORT, (struct sched_param*)&param) && EINVAL == errno);
  533. param.requested_time = -1;
  534. ASSERT(-1 == sched_setparam(0, (struct sched_param*)&param) && EINVAL == errno);
  535. ASSERT(-1 == sched_setscheduler(0, SCHED_SHORT, (struct sched_param*)&param) && EINVAL == errno);
  536.  
  537. while (requestedTime - short_remaining_time(getpid()) < runTime + MS_EPS);
  538. param.requested_time = runTime - 1;
  539. ASSERT_MESSAGE(-1 == sched_setparam(0, (struct sched_param*)&param) && EINVAL == errno,
  540. "It is forbidden to decrease the requested time to a value smaller than the actual time the process has already ran");
  541. ASSERT_MESSAGE(-1 == sched_setscheduler(0, SCHED_SHORT, (struct sched_param*)&param) && EINVAL == errno,
  542. "It is forbidden to decrease the requested time to a value smaller than the actual time the process has already ran");
  543.  
  544. while(1 == is_short(getpid()));
  545.  
  546. ASSERT(abs(2 * requestedTime - short_remaining_time(getpid())) < MS_EPS);
  547. ASSERT(0 == is_short(getpid()));
  548. ASSERT(-1 == was_short(getpid()) && EINVAL == errno);
  549. ASSERT(0 == sched_getparam(getpid(), (struct sched_param*)&param));
  550. ASSERT(requestedTime == param.requested_time);
  551. while (0 == is_short(getpid()));
  552.  
  553. ASSERT(-1 == is_short(getpid()) && EINVAL == errno);
  554. ASSERT(-1 == short_remaining_time(getpid()) && EINVAL == errno);
  555. ASSERT(1 == was_short(getpid()));
  556.  
  557. exit(0);
  558. }
  559.  
  560. int returnValue;
  561. DO_SYS(waitpid(child, &returnValue, 0));
  562. ASSERT(0 == WEXITSTATUS(returnValue));
  563.  
  564. return 0;
  565. }
  566.  
  567. int testToCheckMorePriotirisedShortWakeUp()
  568. {
  569. pid_t child;
  570.  
  571. DO_SYS(child = fork());
  572.  
  573. if (0 == child)
  574. {
  575. const int requestedTime = 3000;
  576.  
  577. pid_t child;
  578.  
  579. DO_SYS(child = fork());
  580.  
  581. if (0 == child)
  582. {
  583. MAKE_SHORT(getpid(), requestedTime);
  584. DEBUG("I will sleep a little bit, but afterwards I will wake up and to interrupt my child");
  585. int timeBeforeSleep = short_remaining_time(getpid());
  586. sleep((requestedTime/1000) >> 1);
  587. ASSERT(abs(timeBeforeSleep - short_remaining_time(getpid())) < MS_EPS);
  588.  
  589. SharedMemory *memory = getSharedMemory(0);
  590. ASSERT(memory->fatherStartTime);
  591. ASSERT(!memory->fatherFinishTime);
  592. memory->childFinishTime = time(NULL);
  593. exit(0);
  594. }
  595. DO_SYS(nice(1));
  596. MAKE_SHORT(getpid(), requestedTime);
  597. SharedMemory *memory = getSharedMemory(0);
  598. memory->fatherStartTime = time(NULL);
  599. ASSERT(!memory->childFinishTime);
  600. DEBUG("Going to wait for child to wake up");
  601. while (!memory->childFinishTime);
  602. ASSERT(1 == is_short(getpid()));
  603. DEBUG("Child woken up and interrupted me");
  604. memory->fatherFinishTime = time(NULL);
  605. exit(0);
  606. }
  607.  
  608. int returnValue;
  609. DO_SYS(waitpid(child, &returnValue, 0));
  610. ASSERT(0 == WEXITSTATUS(returnValue));
  611.  
  612. return 0;
  613. }
  614.  
  615. int main(int argc, char *argv[])
  616. {
  617. DEBUG_INIT;
  618.  
  619. DO_SYS(key = ftok(PATH_NAME, PROJECT_ID));
  620. getSharedMemory(1);
  621.  
  622. RUN_TEST(testToCheckNewSysCalls);
  623. RUN_TEST(testToCheckDifferentPriorities);
  624. RUN_TEST(testToCheckFifoOrderOfTheSamePriorities);
  625. RUN_TEST(testToCheckThatSonStartsBefore);
  626. RUN_TEST(testToCheckTimeAfterForkOfShort);
  627. RUN_TEST(forkBecameShortMakeShortOverdueAndCheckOrder);
  628. RUN_TEST(testToCheckMorePriotirisedShortWakeUp);
  629.  
  630. return 0;
  631. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement