<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Frage zur Ausgabe eines Programmcodes]]></title><description><![CDATA[<p>Hey, ich hoffe mir kann jemand helfen zu verstehen was bei folgendem Programmcode als Ausgabe erscheint:</p>
<pre><code>#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;

int x =0;
int main(){
fork();
fork();
fork();
printf(&quot;pid=%d x=%d\n&quot;, getpid(), x++);
}
</code></pre>
<p>Und zwar hätte ich erwartet das die jeweile PID des Prozesses ausgegeben wird und x halt jeweils hochgezählt wird, oder das bei allen Prozessen x = 1 ist, aber wenn ich das Programm kompiliere ist bei allen Prozessen x = 0. Kann mir jemand helfen das irgendwie zu verstehen ?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/338705/frage-zur-ausgabe-eines-programmcodes</link><generator>RSS for Node</generator><lastBuildDate>Mon, 13 Apr 2026 10:15:22 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/338705.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 04 Jul 2016 12:46:59 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Frage zur Ausgabe eines Programmcodes on Mon, 04 Jul 2016 12:46:59 GMT]]></title><description><![CDATA[<p>Hey, ich hoffe mir kann jemand helfen zu verstehen was bei folgendem Programmcode als Ausgabe erscheint:</p>
<pre><code>#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;

int x =0;
int main(){
fork();
fork();
fork();
printf(&quot;pid=%d x=%d\n&quot;, getpid(), x++);
}
</code></pre>
<p>Und zwar hätte ich erwartet das die jeweile PID des Prozesses ausgegeben wird und x halt jeweils hochgezählt wird, oder das bei allen Prozessen x = 1 ist, aber wenn ich das Programm kompiliere ist bei allen Prozessen x = 0. Kann mir jemand helfen das irgendwie zu verstehen ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2501139</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501139</guid><dc:creator><![CDATA[Bobbi Bob]]></dc:creator><pubDate>Mon, 04 Jul 2016 12:46:59 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zur Ausgabe eines Programmcodes on Mon, 04 Jul 2016 13:09:41 GMT]]></title><description><![CDATA[<p>Bobbi Bob schrieb:</p>
<blockquote>
<p>Und zwar hätte ich erwartet das die jeweile PID des Prozesses ausgegeben wird und x halt jeweils hochgezählt wird, oder das bei allen Prozessen x = 1 ist</p>
</blockquote>
<p>Also eine &quot;oder&quot;-Erwartung ist schonmal merkwürdig <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
<p>Die jeweilige PID kommt doch raus. Aber warum erwartest du andere Ausgaben als x=0? fork kopiert den Prozess, nach einem fork hast du also zwei unabhängige Variablen x, eine in jedem der Prozesse. Die sind dann beide 0. Wenn du x in einem Prozess änderst, hat das keine Auswirkungen auf die anderen Prozesse. Und wie und warum sollte überall x=1 herauskommen?</p>
<p>Und drei forks hintereinander machen dir 8 Prozesse. Normalerweise macht es auch Sinn, die Rückgabe von fork zu überprüfen, um zwischen Kind, Elternteil und Fehler zu unterscheiden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2501152</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501152</guid><dc:creator><![CDATA[wob]]></dc:creator><pubDate>Mon, 04 Jul 2016 13:09:41 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zur Ausgabe eines Programmcodes on Mon, 04 Jul 2016 13:18:02 GMT]]></title><description><![CDATA[<p>man könnte noch darauf hinweisen, dass es in C++ auch standardmittel fürs multithreading gibt, zwecks portabilität...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2501155</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501155</guid><dc:creator><![CDATA[ddavie]]></dc:creator><pubDate>Mon, 04 Jul 2016 13:18:02 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zur Ausgabe eines Programmcodes on Mon, 04 Jul 2016 14:02:45 GMT]]></title><description><![CDATA[<p>ddavie schrieb:</p>
<blockquote>
<p>man könnte noch darauf hinweisen, dass es in C++ auch standardmittel fürs multithreading gibt, zwecks portabilität...</p>
</blockquote>
<p>Was hat der gezeigte Code mit Multithreading zu tun?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2501175</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501175</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Mon, 04 Jul 2016 14:02:45 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zur Ausgabe eines Programmcodes on Mon, 04 Jul 2016 14:22:09 GMT]]></title><description><![CDATA[<p>man kann nur raten, was der OP meint. außerdem immerhin mehr als mit C++.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2501194</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501194</guid><dc:creator><![CDATA[dove]]></dc:creator><pubDate>Mon, 04 Jul 2016 14:22:09 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zur Ausgabe eines Programmcodes on Mon, 04 Jul 2016 14:23:24 GMT]]></title><description><![CDATA[<p>Man sollte noch anmerken, dass x++ nicht 1 ergibt, sondern 0, welche weiter an printf (in C++??) gegeben wird. Informiere dich mal über Präfix- und Suffixinkrement- ubd dekrementoperatoren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2501195</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2501195</guid><dc:creator><![CDATA[Techel]]></dc:creator><pubDate>Mon, 04 Jul 2016 14:23:24 GMT</pubDate></item></channel></rss>