2022-02-22 23:16:42 +01:00
|
|
|
/*
|
2022-04-15 11:11:49 +02:00
|
|
|
* This is an example program for sending a message through a "pipe".
|
2022-02-22 23:16:42 +01:00
|
|
|
* Created by turbocat (Maxim Logaev) 2022.
|
2022-04-15 11:11:49 +02:00
|
|
|
*/
|
2022-02-22 23:16:42 +01:00
|
|
|
|
2022-04-15 11:11:49 +02:00
|
|
|
#include <assert.h>
|
2022-02-22 23:16:42 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2022-04-15 11:11:49 +02:00
|
|
|
#include <sys/ksys.h>
|
2022-02-22 23:16:42 +01:00
|
|
|
|
|
|
|
#define TH_STACK_SIZE 1024
|
2022-04-15 11:11:49 +02:00
|
|
|
#define MESSAGE_SIZE 12
|
2022-02-22 23:16:42 +01:00
|
|
|
|
|
|
|
ksys_colors_table_t sys_colors;
|
|
|
|
int pipefd[2];
|
2022-04-15 11:11:49 +02:00
|
|
|
char* send_message = "HELLO PIPE!";
|
2022-02-22 23:16:42 +01:00
|
|
|
|
2022-04-15 11:11:49 +02:00
|
|
|
void tmain()
|
|
|
|
{
|
2022-02-22 23:16:42 +01:00
|
|
|
char recv_message[MESSAGE_SIZE];
|
|
|
|
_ksys_posix_read(pipefd[0], recv_message, MESSAGE_SIZE);
|
|
|
|
printf("RECV: %s\n", recv_message);
|
|
|
|
assert(!strcmp(recv_message, send_message));
|
|
|
|
puts("Successful pipe test");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2022-04-15 11:11:49 +02:00
|
|
|
void create_thread(void)
|
|
|
|
{
|
|
|
|
unsigned tid; // New thread ID
|
|
|
|
void* th_stack = malloc(TH_STACK_SIZE); // Allocate memory for thread stack
|
2022-02-22 23:16:42 +01:00
|
|
|
if (!th_stack) {
|
|
|
|
puts("Memory allocation error for thread!");
|
|
|
|
return;
|
|
|
|
}
|
2022-04-15 11:11:49 +02:00
|
|
|
tid = _ksys_create_thread(tmain, th_stack + TH_STACK_SIZE); // Create new thread with entry "main"
|
2022-02-22 23:16:42 +01:00
|
|
|
if (tid == -1) {
|
|
|
|
puts("Unable to create a new thread!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
printf("New thread created (TID=%u)\n", tid);
|
|
|
|
}
|
|
|
|
|
2022-04-15 11:11:49 +02:00
|
|
|
void main()
|
|
|
|
{
|
2022-02-22 23:16:42 +01:00
|
|
|
if (_ksys_posix_pipe2(pipefd, 0)) {
|
|
|
|
puts("Pipe creation error!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
printf("SEND: %s\n", send_message);
|
|
|
|
_ksys_posix_write(pipefd[1], send_message, MESSAGE_SIZE);
|
|
|
|
create_thread();
|
|
|
|
}
|