<?xml version="1.0" encoding="utf-8"?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
	<channel>
		<title>C/C++ - Форум программистов и сисадминов Киберфорум</title>
		<link>https://www.cyberforum.ru/</link>
		<description>Форум программистов C++. Обсуждение языка программирования C++, решение задач, программирование и разработка.</description>
		<language>ru</language>
		<lastBuildDate>Mon, 10 Aug 2026 17:26:18 GMT</lastBuildDate>
		<generator>vBulletin</generator>
		<ttl>60</ttl>
		<image>
			<url>https://www.cyberforum.ru//cyberstatic.net/images/misc/rss.jpg</url>
			<title>C/C++ - Форум программистов и сисадминов Киберфорум</title>
			<link>https://www.cyberforum.ru/</link>
		</image>
		<item>
			<title>Реализация ДКА связанная со временем - C/С++ под Linux</title>
			<link>https://www.cyberforum.ru/cpp-linux/thread3224968.html</link>
			<pubDate>Fri, 07 Aug 2026 14:02:08 GMT</pubDate>
			<description>юЗахотелось создать ДКА так, как он традиционно описывается в литературе. А именно с помощью...</description>
			<content:encoded><![CDATA[<div>юЗахотелось создать ДКА так, как он традиционно описывается в литературе. А именно с помощью функции по времени. Вот что получилось, добиться наносекундного соответствия работы одного и того же алгоритма до конца не удалось. Но время выполнения похоже. Один ДКА  отмеряет время в тактах, другой подсчитывает по отмеренным тактам, проверят делимость) Если вы знаете как в условиях   GCC/LINUX  добиться что бы алгоритм обрабатывался ТОЧНО одно и тоже время каждый раз когда он исполняется, пишите.<div class="printablecode">
	<div class="smallfont">:</div>
	<hr /><code dir="ltr">#define _POSIX_C_SOURCE 200112L<br />
#include &lt;stdio.h&gt;<br />
#include &lt;stdlib.h&gt;<br />
#include &lt;pthread.h&gt;<br />
#include &lt;semaphore.h&gt;<br />
#include &lt;time.h&gt;<br />
#include &lt;string.h&gt;<br />
#include &lt;stdatomic.h&gt;<br />
<br />
//gcc -std=c11 -pthread dfa1.c&nbsp; -std=gnu99 -o timer_fsm<br />
<br />
static int state = 0;<br />
sem_t tick_sem;<br />
sem_t ack_sem;<br />
atomic_int running = 1;<br />
<br />
// Состояния автомата (остатки от деления на 3)<br />
typedef enum {<br />
&nbsp; &nbsp; REM_0 = 0,&nbsp; // Число делится на 3<br />
&nbsp; &nbsp; REM_1 = 1,&nbsp; // Остаток 1<br />
&nbsp; &nbsp; REM_2 = 2&nbsp;  // Остаток 2<br />
} MState;<br />
<br />
// a. Функция перехода автомата<br />
MState transition(MState current, char bit) {<br />
&nbsp; &nbsp; // Если бит передан как символ '0' или '1', преобразуем в число 0 или 1<br />
&nbsp; &nbsp; int b = (bit == '1') ? 1 : 0;<br />
&nbsp; &nbsp; // Главное правило: (старый_остаток * 2 + новый_бит) % 3<br />
&nbsp; &nbsp; int next_remainder = (current * 2 + b) % 3;<br />
&nbsp; &nbsp; return (MState)next_remainder;<br />
}<br />
// a. Функция перехода автомата проверки деления на 3<br />
MState dispatcher_tick(const char* input, int i, int length, MState current_state) {<br />
&nbsp; &nbsp; char bit = input&#91;i&#93;;<br />
&nbsp; &nbsp; if (i &lt;= length){<br />
&nbsp; &nbsp;  return transition(current_state, bit);<br />
&nbsp; &nbsp; } else {<br />
&nbsp; &nbsp; &nbsp; return&nbsp; bit;<br />
&nbsp; &nbsp; }&nbsp; &nbsp; <br />
}<br />
<br />
// a. Функция перехода автомата проверки деления на 3<br />
MState call_dispatcher_tick(const char* input, MState current_state, MState current_state_R) {<br />
&nbsp; int length = strlen(input);<br />
&nbsp; current_state = current_state - 1; <br />
<br />
&nbsp; if ((current_state &gt;= length) &amp;&amp; (current_state_R == REM_0)) {<br />
&nbsp; &nbsp; &nbsp; // Основание 2 означает двоичную систему<br />
&nbsp; &nbsp; &nbsp; char* endptr;<br />
&nbsp; &nbsp; &nbsp; unsigned long long decimal_value = strtoull(input, &amp;endptr, 2);<br />
&nbsp; &nbsp; &nbsp; printf(&quot;Утверждение: Число %lld ДЕЛИТСЯ на 3.\n&quot;,&nbsp; decimal_value);<br />
&nbsp; &nbsp; &nbsp; return -1;<br />
&nbsp;  } else if&nbsp; ((current_state &gt; length) &amp;&amp; (current_state_R != REM_0)) {<br />
&nbsp; &nbsp; &nbsp; char* endptr;<br />
&nbsp; &nbsp; &nbsp; unsigned long long decimal_value = strtoull(input, &amp;endptr, 2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; printf(&quot;Утверждение: Число %%lld НЕ делится на 3 (остаток %d).\n&quot;, decimal_value, current_state_R);<br />
&nbsp; &nbsp; &nbsp; return -1;<br />
&nbsp;  }<br />
&nbsp; return dispatcher_tick(input, (current_state - 1), length, current_state_R);<br />
}<br />
<br />
void* thread1(void* arg) {<br />
&nbsp; &nbsp; long long interval_ms = *(long long*)arg;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; while (1) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; // Ожидание интервала<br />
&nbsp; &nbsp; &nbsp; &nbsp; struct timespec start;<br />
&nbsp; &nbsp; &nbsp; &nbsp; clock_gettime(CLOCK_MONOTONIC, &amp;start);<br />
&nbsp; &nbsp; &nbsp; &nbsp; long long target_ns = (long long)start.tv_sec * 1000000000LL + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  start.tv_nsec + interval_ms * 1000000LL;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; struct timespec target;<br />
&nbsp; &nbsp; &nbsp; &nbsp; target.tv_sec = target_ns / 1000000000LL;<br />
&nbsp; &nbsp; &nbsp; &nbsp; target.tv_nsec = target_ns % 1000000000LL;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &amp;target, NULL);<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; // Такт<br />
&nbsp; &nbsp; &nbsp; &nbsp; state++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;Такт %d\n&quot;, state);<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; // Ожидание с таймаутом<br />
&nbsp; &nbsp; &nbsp; &nbsp; struct timespec ts_wait;<br />
&nbsp; &nbsp; &nbsp; &nbsp; clock_gettime(CLOCK_REALTIME, &amp;ts_wait);<br />
&nbsp; &nbsp; &nbsp; &nbsp; ts_wait.tv_sec += 1;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; if (sem_timedwait(&amp;ack_sem, &amp;ts_wait) != 0) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Таймаут - возможно, поток 2 уже завершился<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!atomic_load(&amp;running)) break;<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; sem_post(&amp;tick_sem);<br />
&nbsp;  <br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; return NULL;<br />
}<br />
<br />
void* thread2(void* arg) {<br />
&nbsp; &nbsp; int last_state = -1;<br />
&nbsp; &nbsp; MState current_state_R = REM_0;<br />
&nbsp; &nbsp; // Входная последовательность<br />
&nbsp; &nbsp; const char* input = &quot;10010100000001111001110011010001101001000010101010101010101&quot;;&nbsp; &nbsp; <br />
&nbsp; &nbsp; while (1) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; sem_wait(&amp;tick_sem);<br />
&nbsp; &nbsp; &nbsp; &nbsp; if (state != last_state) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;Поток 2 обработал такт %d\n&quot;, state);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  current_state_R = call_dispatcher_tick(input, state, current_state_R);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (current_state_R == (-1)) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;Поток 2 завершается: достигнут конец строки\n&quot;);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; atomic_store(&amp;running, 0);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sem_post(&amp;ack_sem);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last_state = state;<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; sem_post(&amp;ack_sem);<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; return NULL;<br />
}<br />
<br />
int main() {<br />
&nbsp; &nbsp; pthread_t t1, t2;<br />
&nbsp; &nbsp; long long interval = 150;<br />
&nbsp; &nbsp; struct timespec start, end;<br />
&nbsp; &nbsp; // Фиксируем реальное время (TIME_UTC)<br />
&nbsp; &nbsp; clock_gettime(CLOCK_MONOTONIC, &amp;start);<br />
&nbsp; &nbsp; &nbsp; &nbsp; long long begin_ns = (long long)start.tv_sec * 1000000000LL + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  start.tv_nsec + interval * 1000000LL;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; sem_init(&amp;tick_sem, 0, 0);<br />
&nbsp; &nbsp; sem_init(&amp;ack_sem, 0, 0);<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; pthread_create(&amp;t1, NULL, thread1, &amp;interval);<br />
&nbsp; &nbsp; pthread_create(&amp;t2, NULL, thread2, NULL);<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; pthread_join(t1, NULL);<br />
&nbsp; &nbsp; pthread_join(t2, NULL);<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; sem_destroy(&amp;tick_sem);<br />
&nbsp; &nbsp; sem_destroy(&amp;ack_sem);<br />
<br />
&nbsp; &nbsp; pthread_cancel(t1);<br />
&nbsp; &nbsp; pthread_cancel(t2);<br />
<br />
&nbsp; &nbsp; clock_gettime(CLOCK_MONOTONIC, &amp;start);<br />
&nbsp; &nbsp; &nbsp; &nbsp; long long end_ns = (long long)start.tv_sec * 1000000000LL + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  start.tv_nsec + interval * 1000000LL;<br />
<br />
&nbsp; &nbsp; // Считаем разницу<br />
&nbsp; &nbsp; double elapsed = end_ns - begin_ns;<br />
&nbsp; &nbsp; printf(&quot;Реальное время: %.9f сек\n&quot;, elapsed);<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; return 0;<br />
}</code><hr />
</div></div>

]]></content:encoded>
			<category domain="https://www.cyberforum.ru/cpp-linux/">C/С++ под Linux</category>
			<dc:creator>JZKatsman</dc:creator>
			<guid isPermaLink="true">https://www.cyberforum.ru/cpp-linux/thread3224968.html</guid>
		</item>
		<item>
			<title>Как понять тему ссылки и указатели - С++ для начинающих</title>
			<link>https://www.cyberforum.ru/cpp-beginners/thread3224953.html</link>
			<pubDate>Wed, 05 Aug 2026 17:53:49 GMT</pubDate>
			<description>я не понимаю эту тему я постоянно путаюсь дайте какойто видеоурок самый понятный или сами объясните...</description>
			<content:encoded><![CDATA[<div>я не понимаю эту тему я постоянно путаюсь дайте какойто видеоурок самый понятный или сами объясните я не понимаю.</div>

]]></content:encoded>
			<category domain="https://www.cyberforum.ru/cpp-beginners/">С++ для начинающих</category>
			<dc:creator>bima8</dc:creator>
			<guid isPermaLink="true">https://www.cyberforum.ru/cpp-beginners/thread3224953.html</guid>
		</item>
	</channel>
</rss>
