You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

61 line
2.2 KiB

  1. /*
  2. * piHiPri:
  3. * Simple way to get your program running at high priority
  4. * with realtime schedulling.
  5. *
  6. * Copyright (c) 2012 Gordon Henderson
  7. ***********************************************************************
  8. * This file is part of wiringPi:
  9. * https://github.com/WiringPi/WiringPi/
  10. *
  11. * wiringPi is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * wiringPi is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with wiringPi.
  23. * If not, see <http://www.gnu.org/licenses/>.
  24. ***********************************************************************
  25. */
  26. #include <sched.h>
  27. #include <string.h>
  28. #include "wiringPi.h"
  29. /*
  30. * piHiPri:
  31. * Attempt to set a high priority schedulling for the running program.
  32. * A lower priority number = higher priority in the system.
  33. * On linux, this range is 1 through 99.
  34. * Returns result of sched_setscheduler() call: 0 on success, -1 on error.
  35. *********************************************************************************
  36. * @NOTE: This could be done using "int nice(int inc);" in <unistd.h>
  37. * nice() adds inc to the nice value for the calling thread.
  38. * A higher inc value means a lower priority.
  39. * A negative inc value means higher priority.
  40. *********************************************************************************
  41. */
  42. int piHiPri (const int pri)
  43. {
  44. struct sched_param sched;
  45. int max_pri = sched_get_priority_max (SCHED_RR);
  46. memset (&sched, 0, sizeof(sched));
  47. if (pri > max_pri)
  48. sched.sched_priority = max_pri;
  49. else
  50. sched.sched_priority = pri;
  51. // PID = 0 means the current running program.
  52. // SCHED_RR = round-robin scheduling policy.
  53. return sched_setscheduler (0, SCHED_RR, &sched);
  54. }