Subversion Repositories Kolibri OS

Rev

Rev 9725 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * This is an example program for sending a message through a "pipe".
  3.  * Created by turbocat (Maxim Logaev) 2022.
  4.  */
  5.  
  6. #include <assert.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/ksys.h>
  11.  
  12. #define TH_STACK_SIZE 1024
  13. #define MESSAGE_SIZE 12
  14.  
  15. ksys_colors_table_t sys_colors;
  16. int pipefd[2];
  17. char* send_message = "HELLO PIPE!";
  18.  
  19. void tmain()
  20. {
  21.     char recv_message[MESSAGE_SIZE];
  22.     _ksys_posix_read(pipefd[0], recv_message, MESSAGE_SIZE);
  23.     printf("RECV: %s\n", recv_message);
  24.     assert(!strcmp(recv_message, send_message));
  25.     puts("Successful pipe test");
  26.     exit(0);
  27. }
  28.  
  29. void create_thread(void)
  30. {
  31.     unsigned tid;                           // New thread ID
  32.     void* th_stack = malloc(TH_STACK_SIZE); // Allocate memory for thread stack
  33.     if (!th_stack) {
  34.         puts("Memory allocation error for thread!");
  35.         return;
  36.     }
  37.     tid = _ksys_create_thread(tmain, th_stack + TH_STACK_SIZE); // Create new thread with entry "main"
  38.     if (tid == -1) {
  39.         puts("Unable to create a new thread!");
  40.         return;
  41.     }
  42.     printf("New thread created (TID=%u)\n", tid);
  43. }
  44.  
  45. void main()
  46. {
  47.     if (_ksys_posix_pipe2(pipefd, 0)) {
  48.         puts("Pipe creation error!");
  49.         return;
  50.     }
  51.     printf("SEND: %s\n", send_message);
  52.     _ksys_posix_write(pipefd[1], send_message, MESSAGE_SIZE);
  53.     create_thread();
  54. }
  55.