Rev 9725 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
9725 | turbocat | 1 | /* |
9766 | turbocat | 2 | * This is an example program for sending a message through a "pipe". |
9725 | turbocat | 3 | * Created by turbocat (Maxim Logaev) 2022. |
9766 | turbocat | 4 | */ |
9725 | turbocat | 5 | |
9766 | turbocat | 6 | #include |
9725 | turbocat | 7 | #include |
8 | #include |
||
9 | #include |
||
9766 | turbocat | 10 | #include |
9725 | turbocat | 11 | |
12 | #define TH_STACK_SIZE 1024 |
||
9766 | turbocat | 13 | #define MESSAGE_SIZE 12 |
9725 | turbocat | 14 | |
15 | ksys_colors_table_t sys_colors; |
||
16 | int pipefd[2]; |
||
9766 | turbocat | 17 | char* send_message = "HELLO PIPE!"; |
9725 | turbocat | 18 | |
9766 | turbocat | 19 | void tmain() |
20 | { |
||
9725 | turbocat | 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 | |||
9766 | turbocat | 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 |
||
9725 | turbocat | 33 | if (!th_stack) { |
34 | puts("Memory allocation error for thread!"); |
||
35 | return; |
||
36 | } |
||
9766 | turbocat | 37 | tid = _ksys_create_thread(tmain, th_stack + TH_STACK_SIZE); // Create new thread with entry "main" |
9725 | turbocat | 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 | |||
9766 | turbocat | 45 | void main() |
46 | { |
||
9725 | turbocat | 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 | } |