• XSS.stack #1 – первый литературный журнал от юзеров форума

How to control standard input/output between threads?

el84

(L2) cache
Пользователь
Регистрация
10.01.2023
Сообщения
326
Реакции
143
Депозит
0.00
I have a problem about controlling which thread should have preference when reading from standard input. The program main thread have a prompt loop and another thread which enqueue commands. Some commands is supposed to establish a shell and then takeover the standard input/output. But I don't know how to do this properly yet and both threads are trying to read at the same time.

Any tips are welcome.
 
Пожалуйста, обратите внимание, что пользователь заблокирован
Используй мьютексы или критические секции.
Выглядит как не очень хорошая практика читать из stdin в разных потоках
 
Пожалуйста, обратите внимание, что пользователь заблокирован
Treat stdin as a global singleton, you should either sync its usage from several thread with locks (depending on your language of choice, C# has locks which are very easy to use, C/C++ use mutexes or critical sections depending on operating system), or use it only from one thread and pass the data to other threads (again depending on the language and operating system some methods would be easier than others).
 
I started to implement a simple C2 in C language as a hobby. The code still a mess, the mutex idea worked fine, I just used a global
C:
pthread_mutex_t TERMINAL_LOCK
And get the lock when handling a remote shell, it prevented the main prompt to mess with stdin/stdout. I was thinking to put the code online so other people can read and modify it as wanted. Someone have a recommendation on public git instance?
 


Напишите ответ...
  • Вставить:
Прикрепить файлы
Верх