Subversion Repositories Kolibri OS

Rev

Go to most recent revision | 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 <sys/ksys.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <assert.h>
  10. #include <string.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.     char recv_message[MESSAGE_SIZE];
  21.     _ksys_posix_read(pipefd[0], recv_message, MESSAGE_SIZE);
  22.     printf("RECV: %s\n", recv_message);
  23.     assert(!strcmp(recv_message, send_message));
  24.     puts("Successful pipe test");
  25.     exit(0);
  26. }
  27.  
  28. void create_thread(void){
  29.     unsigned tid;  // New thread ID
  30.     void *th_stack = malloc(TH_STACK_SIZE); // Allocate memory for thread stack
  31.     if (!th_stack) {
  32.         puts("Memory allocation error for thread!");
  33.         return;
  34.     }
  35.     tid = _ksys_create_thread(tmain, th_stack+TH_STACK_SIZE); // Create new thread with entry "main"
  36.     if (tid == -1) {
  37.         puts("Unable to create a new thread!");
  38.         return;
  39.     }
  40.     printf("New thread created (TID=%u)\n", tid);
  41. }
  42.  
  43. void main() {    
  44.     if (_ksys_posix_pipe2(pipefd, 0)) {
  45.         puts("Pipe creation error!");
  46.         return;
  47.     }
  48.     printf("SEND: %s\n", send_message);
  49.     _ksys_posix_write(pipefd[1], send_message, MESSAGE_SIZE);
  50.     create_thread();
  51. }
  52.