signal_sample.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-24 yangjie the first version
  9. * 2020-10-17 Meco Man translate to English comment
  10. */
  11. /*
  12. * Demo: signal
  13. *
  14. * This demo creates one thread. When the signal is installed, the signal
  15. * processing mode is set to custom processing. The processing function of
  16. * the signal is defined to be thread1_signal_handler(). After the thread is
  17. * running and the signal is installed, a signal is sent to this thread.
  18. * This thread will receive the signal and print the message.
  19. *
  20. * read more:
  21. * https://www.rt-thread.io/document/site/programming-manual/ipc2/ipc2/#signal
  22. */
  23. #include <rtthread.h>
  24. #define THREAD_PRIORITY 25
  25. #define THREAD_STACK_SIZE 512
  26. #define THREAD_TIMESLICE 5
  27. static rt_thread_t tid1 = RT_NULL;
  28. /* callback function of thread #1's signal */
  29. void thread1_signal_handler(int sig)
  30. {
  31. rt_kprintf("thread1 received signal %d\n", sig);
  32. }
  33. /* thread #1 entry function */
  34. static void thread1_entry(void *parameter)
  35. {
  36. int cnt = 0;
  37. /* install signal*/
  38. rt_signal_install(SIGUSR1, thread1_signal_handler);
  39. rt_signal_unmask(SIGUSR1);
  40. while (cnt < 10)
  41. {
  42. rt_kprintf("thread1 count : %d\n", cnt);
  43. cnt++;
  44. rt_thread_mdelay(100);
  45. }
  46. }
  47. int signal_sample(void)
  48. {
  49. tid1 = rt_thread_create("thread1",
  50. thread1_entry, RT_NULL,
  51. THREAD_STACK_SIZE,
  52. THREAD_PRIORITY, THREAD_TIMESLICE);
  53. if (tid1 != RT_NULL)
  54. rt_thread_startup(tid1);
  55. rt_thread_mdelay(300);
  56. /* send the signal "SIGUSR1" to thread #1 */
  57. rt_thread_kill(tid1, SIGUSR1);
  58. return 0;
  59. }
  60. /* export the msh command */
  61. MSH_CMD_EXPORT(signal_sample, signal sample);