<?xml version="1.0"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title>WikiLabs  - Schimbări recente [ro]</title>
		<link>http://wiki.dcae.pub.ro/index.php/Special:Schimb%C4%83ri_recente</link>
		<description>Urmărește cele mai recente schimbări folosind acest flux.</description>
		<language>ro</language>
		<generator>MediaWiki 1.35.14</generator>
		<lastBuildDate>Sat, 05 Dec 2026 02:32:28 GMT</lastBuildDate>
		<item>
			<title>OS Lab 9 - USB drivers. USB transfers</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=OS_Lab_9_-_USB_drivers._USB_transfers&amp;diff=8466&amp;oldid=8463</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=OS_Lab_9_-_USB_drivers._USB_transfers&amp;diff=8466&amp;oldid=8463</guid>
			<description>&lt;p&gt;&lt;/p&gt;
&lt;a href=&quot;http://wiki.dcae.pub.ro/index.php?title=OS_Lab_9_-_USB_drivers._USB_transfers&amp;amp;diff=8466&amp;amp;oldid=8463&quot;&gt;Afișare diferențe&lt;/a&gt;</description>
			<pubDate>Fri, 04 Dec 2026 14:23:12 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie:OS_Lab_9_-_USB_drivers._USB_transfers</comments>
		</item>
		<item>
			<title>Arhitectura Sistemelor de Calcul (curs)</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Arhitectura_Sistemelor_de_Calcul_(curs)&amp;diff=8465&amp;oldid=8455</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Arhitectura_Sistemelor_de_Calcul_(curs)&amp;diff=8465&amp;oldid=8455</guid>
			<description>&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left diff-editfont-monospace&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ro&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Versiunea anterioară&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Versiunea de la data 3 decembrie 2026 23:44&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;diff-multi&quot; lang=&quot;ro&quot;&gt;(Nu s-a afișat o versiune intermediară efectuată de același utilizator)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l44&quot; &gt;Linia 44:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Linia 44:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://www.youtube.com/watch?v=0ttx0AuNezo Superscalar vs Superpipelined]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://www.youtube.com/watch?v=0ttx0AuNezo Superscalar vs Superpipelined]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://www.youtube.com/watch?v=6tbNew87fZU All CPU Architectures from von Neumann to Hybrid Multicores]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://www.youtube.com/watch?v=6tbNew87fZU All CPU Architectures from von Neumann to Hybrid Multicores]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;== Specific Architectures Detailed ==&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* [https://www.youtube.com/watch?v=vgPFzblBh7w Intel Modern CPU Architecture 1 – Key Concepts]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* [https://www.youtube.com/watch?v=o_WXTRS2qTY Intel Modern CPU Architecture 2 - Microarchitecture Deep Dive]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Current Hot Topics ==&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Current Hot Topics ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</description>
			<pubDate>Thu, 03 Dec 2026 23:44:27 GMT</pubDate>
			<dc:creator>Zhascsi</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie:Arhitectura_Sistemelor_de_Calcul_(curs)</comments>
		</item>
		<item>
			<title>OS Lab 9 - USB drivers. USB transfers</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=OS_Lab_9_-_USB_drivers._USB_transfers&amp;diff=8463&amp;oldid=0</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=OS_Lab_9_-_USB_drivers._USB_transfers&amp;diff=8463&amp;oldid=0</guid>
			<description>&lt;p&gt;Pagină nouă: In &lt;a href=&quot;/index.php/OS_Lab_8_-_Kernel_modules._Character_devices&quot; title=&quot;OS Lab 8 - Kernel modules. Character devices&quot;&gt;the last lab&lt;/a&gt;, we wrote a kernel module with a character device, whose &amp;lt;code&amp;gt;read()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;write()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ioctl(...&lt;/p&gt;
&lt;a href=&quot;http://wiki.dcae.pub.ro/index.php?title=OS_Lab_9_-_USB_drivers._USB_transfers&amp;amp;diff=8463&quot;&gt;Afișare diferențe&lt;/a&gt;</description>
			<pubDate>Fri, 27 Nov 2026 15:14:42 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie:OS_Lab_9_-_USB_drivers._USB_transfers</comments>
		</item>
		<item>
			<title>Operating Systems</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Operating_Systems&amp;diff=8462&amp;oldid=8457</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Operating_Systems&amp;diff=8462&amp;oldid=8457</guid>
			<description>&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Lab Sessions&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left diff-editfont-monospace&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ro&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Versiunea anterioară&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Versiunea de la data 27 noiembrie 2026 15:14&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;diff-multi&quot; lang=&quot;ro&quot;&gt;(Nu s-a afișat o versiune intermediară efectuată de același utilizator)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l9&quot; &gt;Linia 9:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Linia 9:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab 7 - [[OS Lab 7 - Sockets. Datagrams]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab 7 - [[OS Lab 7 - Sockets. Datagrams]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab 8 - [[OS Lab 8 - Kernel modules. Character devices]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab 8 - [[OS Lab 8 - Kernel modules. Character devices]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* Lab 9 - [[OS Lab 9 - USB drivers. USB transfers ]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=== Old Lab Sessions (2025) ===&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=== Old Lab Sessions (2025) ===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</description>
			<pubDate>Fri, 27 Nov 2026 15:14:05 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie:Operating_Systems</comments>
		</item>
		<item>
			<title>Fișier:OS Lab 8 - container of.png</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:OS_Lab_8_-_container_of.png&amp;diff=8460&amp;oldid=0</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:OS_Lab_8_-_container_of.png&amp;diff=8460&amp;oldid=0</guid>
			<description>&lt;p&gt;&lt;a href=&quot;/index.php?title=Utilizator:Vserbu&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new mw-userlink&quot; title=&quot;Utilizator:Vserbu (pagină inexistentă)&quot;&gt;&lt;bdi&gt;Vserbu&lt;/bdi&gt;&lt;/a&gt; a încărcat &lt;a href=&quot;/index.php/Fi%C8%99ier:OS_Lab_8_-_container_of.png&quot; title=&quot;Fișier:OS Lab 8 - container of.png&quot;&gt;Fișier:OS Lab 8 - container of.png&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pagină nouă&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;/div&gt;</description>
			<pubDate>Fri, 27 Nov 2026 15:01:26 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie_Fi%C8%99ier:OS_Lab_8_-_container_of.png</comments>
		</item>
		<item>
			<title>Fișier:OS Lab 8 - list.png</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:OS_Lab_8_-_list.png&amp;diff=8459&amp;oldid=0</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:OS_Lab_8_-_list.png&amp;diff=8459&amp;oldid=0</guid>
			<description>&lt;p&gt;&lt;a href=&quot;/index.php?title=Utilizator:Vserbu&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new mw-userlink&quot; title=&quot;Utilizator:Vserbu (pagină inexistentă)&quot;&gt;&lt;bdi&gt;Vserbu&lt;/bdi&gt;&lt;/a&gt; a încărcat &lt;a href=&quot;/index.php/Fi%C8%99ier:OS_Lab_8_-_list.png&quot; title=&quot;Fișier:OS Lab 8 - list.png&quot;&gt;Fișier:OS Lab 8 - list.png&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pagină nouă&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;/div&gt;</description>
			<pubDate>Fri, 27 Nov 2026 15:00:50 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie_Fi%C8%99ier:OS_Lab_8_-_list.png</comments>
		</item>
		<item>
			<title>OS Lab 8 - Kernel modules. Character devices</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=OS_Lab_8_-_Kernel_modules._Character_devices&amp;diff=8458&amp;oldid=0</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=OS_Lab_8_-_Kernel_modules._Character_devices&amp;diff=8458&amp;oldid=0</guid>
			<description>&lt;p&gt;Pagină nouă: In &lt;a href=&quot;/index.php/OS_Lab_7_-_Sockets._Datagrams&quot; title=&quot;OS Lab 7 - Sockets. Datagrams&quot;&gt;the last lab&lt;/a&gt;, processes talked to each other through sockets, and the kernel carried the data between them. Until now, all our code ran in user...&lt;/p&gt;
&lt;a href=&quot;http://wiki.dcae.pub.ro/index.php?title=OS_Lab_8_-_Kernel_modules._Character_devices&amp;amp;diff=8458&quot;&gt;Afișare diferențe&lt;/a&gt;</description>
			<pubDate>Fri, 27 Nov 2026 14:59:53 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie:OS_Lab_8_-_Kernel_modules._Character_devices</comments>
		</item>
		<item>
			<title>Operating Systems</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Operating_Systems&amp;diff=8457&amp;oldid=8451</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Operating_Systems&amp;diff=8457&amp;oldid=8451</guid>
			<description>&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Lab Sessions&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left diff-editfont-monospace&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ro&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Versiunea anterioară&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Versiunea de la data 27 noiembrie 2026 14:59&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l8&quot; &gt;Linia 8:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Linia 8:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab 6 - [[OS Lab 6 - Users. Permissions]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab 6 - [[OS Lab 6 - Users. Permissions]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab 7 - [[OS Lab 7 - Sockets. Datagrams]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab 7 - [[OS Lab 7 - Sockets. Datagrams]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* Lab 8 - [[OS Lab 8 - Kernel modules. Character devices]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=== Old Lab Sessions (2025) ===&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=== Old Lab Sessions (2025) ===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</description>
			<pubDate>Fri, 27 Nov 2026 14:59:38 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie:Operating_Systems</comments>
		</item>
		<item>
			<title>Fișier:Skeleton driver.zip</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Skeleton_driver.zip&amp;diff=8456&amp;oldid=0</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:Skeleton_driver.zip&amp;diff=8456&amp;oldid=0</guid>
			<description>&lt;p&gt;&lt;a href=&quot;/index.php?title=Utilizator:Vserbu&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new mw-userlink&quot; title=&quot;Utilizator:Vserbu (pagină inexistentă)&quot;&gt;&lt;bdi&gt;Vserbu&lt;/bdi&gt;&lt;/a&gt; a încărcat &lt;a href=&quot;/index.php/Fi%C8%99ier:Skeleton_driver.zip&quot; title=&quot;Fișier:Skeleton driver.zip&quot;&gt;Fișier:Skeleton driver.zip&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pagină nouă&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;/div&gt;</description>
			<pubDate>Fri, 27 Nov 2026 14:49:49 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie_Fi%C8%99ier:Skeleton_driver.zip</comments>
		</item>
		<item>
			<title>Arhitectura Sistemelor de Calcul (curs)</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Arhitectura_Sistemelor_de_Calcul_(curs)&amp;diff=8455&amp;oldid=8446</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Arhitectura_Sistemelor_de_Calcul_(curs)&amp;diff=8455&amp;oldid=8446</guid>
			<description>&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left diff-editfont-monospace&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ro&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Versiunea anterioară&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Versiunea de la data 25 noiembrie 2026 21:32&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l44&quot; &gt;Linia 44:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Linia 44:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://www.youtube.com/watch?v=0ttx0AuNezo Superscalar vs Superpipelined]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://www.youtube.com/watch?v=0ttx0AuNezo Superscalar vs Superpipelined]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://www.youtube.com/watch?v=6tbNew87fZU All CPU Architectures from von Neumann to Hybrid Multicores]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://www.youtube.com/watch?v=6tbNew87fZU All CPU Architectures from von Neumann to Hybrid Multicores]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;== Current Hot Topics ==&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* [https://www.youtube.com/watch?v=0JxowHz0JsM Nvidia AI Factory]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* [https://www.youtube.com/watch?v=yuQaRQUexmU IBM Quantum System Two]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* [https://www.youtube.com/watch?v=qVc9OePUdyo DNA Computer]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</description>
			<pubDate>Wed, 25 Nov 2026 21:32:34 GMT</pubDate>
			<dc:creator>Zhascsi</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie:Arhitectura_Sistemelor_de_Calcul_(curs)</comments>
		</item>
		<item>
			<title>Fișier:OS Lab 7 - mychat.png</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:OS_Lab_7_-_mychat.png&amp;diff=8454&amp;oldid=0</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:OS_Lab_7_-_mychat.png&amp;diff=8454&amp;oldid=0</guid>
			<description>&lt;p&gt;&lt;a href=&quot;/index.php?title=Utilizator:Vserbu&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new mw-userlink&quot; title=&quot;Utilizator:Vserbu (pagină inexistentă)&quot;&gt;&lt;bdi&gt;Vserbu&lt;/bdi&gt;&lt;/a&gt; a încărcat &lt;a href=&quot;/index.php/Fi%C8%99ier:OS_Lab_7_-_mychat.png&quot; title=&quot;Fișier:OS Lab 7 - mychat.png&quot;&gt;Fișier:OS Lab 7 - mychat.png&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pagină nouă&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;/div&gt;</description>
			<pubDate>Fri, 20 Nov 2026 12:38:51 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie_Fi%C8%99ier:OS_Lab_7_-_mychat.png</comments>
		</item>
		<item>
			<title>Fișier:OS Lab 7 - dgram.png</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:OS_Lab_7_-_dgram.png&amp;diff=8453&amp;oldid=0</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:OS_Lab_7_-_dgram.png&amp;diff=8453&amp;oldid=0</guid>
			<description>&lt;p&gt;&lt;a href=&quot;/index.php?title=Utilizator:Vserbu&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new mw-userlink&quot; title=&quot;Utilizator:Vserbu (pagină inexistentă)&quot;&gt;&lt;bdi&gt;Vserbu&lt;/bdi&gt;&lt;/a&gt; a încărcat &lt;a href=&quot;/index.php/Fi%C8%99ier:OS_Lab_7_-_dgram.png&quot; title=&quot;Fișier:OS Lab 7 - dgram.png&quot;&gt;Fișier:OS Lab 7 - dgram.png&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pagină nouă&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;/div&gt;</description>
			<pubDate>Fri, 20 Nov 2026 12:38:08 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie_Fi%C8%99ier:OS_Lab_7_-_dgram.png</comments>
		</item>
		<item>
			<title>OS Lab 7 - Sockets. Datagrams</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=OS_Lab_7_-_Sockets._Datagrams&amp;diff=8452&amp;oldid=0</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=OS_Lab_7_-_Sockets._Datagrams&amp;diff=8452&amp;oldid=0</guid>
			<description>&lt;p&gt;Pagină nouă: In &lt;a href=&quot;/index.php/OS_Lab_6_-_Users._Permissions&quot; title=&quot;OS Lab 6 - Users. Permissions&quot;&gt;the last lab&lt;/a&gt;, we looked at how the kernel decides what a task may do with a file. In this lab, processes will talk to each other through &amp;#039;&amp;#039;&amp;#039;soc...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pagină nouă&lt;/b&gt;&lt;/p&gt;&lt;div&gt;In [[OS_Lab_6_-_Users._Permissions|the last lab]], we looked at how the kernel decides what a task may do with a file. In this lab, processes will talk to each other through &amp;#039;&amp;#039;&amp;#039;sockets&amp;#039;&amp;#039;&amp;#039;. We will use local datagram sockets, which carry whole messages, each with the address of its sender. With them, a server needs a single socket and a single thread to serve many clients at once. We will also see how a program can wait for the keyboard and for a socket at the same time, still with one thread. As an application, we will write MyChat, a small chat server and client, in the style of IRC (ancestor of WhatsApp, Discord, etc.).&lt;br /&gt;
&lt;br /&gt;
== Learning objectives ==&lt;br /&gt;
&lt;br /&gt;
By the end of this lab you should be able to:&lt;br /&gt;
&lt;br /&gt;
# create a local socket and give it an address;&lt;br /&gt;
# send packets to an address with [https://man7.org/linux/man-pages/man2/sendto.2.html &amp;lt;code&amp;gt;sendto(2)&amp;lt;/code&amp;gt;], and receive them, with the address of their sender, with [https://man7.org/linux/man-pages/man2/recvfrom.2.html &amp;lt;code&amp;gt;recvfrom(2)&amp;lt;/code&amp;gt;];&lt;br /&gt;
# say what the flag &amp;lt;code&amp;gt;MSG_DONTWAIT&amp;lt;/code&amp;gt; changes;&lt;br /&gt;
# serve many clients from a single socket;&lt;br /&gt;
# receive packets while [https://man7.org/linux/man-pages/man3/readline.3.html &amp;lt;code&amp;gt;readline(3)&amp;lt;/code&amp;gt;] waits for the user.&lt;br /&gt;
&lt;br /&gt;
== Sockets ==&lt;br /&gt;
&lt;br /&gt;
A socket is one end of a communication. The process sees it as a file descriptor. Every socket has a &amp;#039;&amp;#039;&amp;#039;family&amp;#039;&amp;#039;&amp;#039;, which says where the other end is, and a &amp;#039;&amp;#039;&amp;#039;type&amp;#039;&amp;#039;&amp;#039;, which says how the data moves. In this lab we use a single combination:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;AF_UNIX&amp;lt;/code&amp;gt;: both ends are on the same machine. The address is a path, and the kernel copies the data from one process to the other, without touching the disk.&lt;br /&gt;
* &amp;lt;code&amp;gt;SOCK_DGRAM&amp;lt;/code&amp;gt;: the data travels in &amp;#039;&amp;#039;&amp;#039;datagrams&amp;#039;&amp;#039;&amp;#039;, or packets, which keep their boundaries. One [https://man7.org/linux/man-pages/man2/sendto.2.html &amp;lt;code&amp;gt;sendto(2)&amp;lt;/code&amp;gt;] makes one packet, and one [https://man7.org/linux/man-pages/man2/recvfrom.2.html &amp;lt;code&amp;gt;recvfrom(2)&amp;lt;/code&amp;gt;] takes one whole packet. There is no connection: every packet is sent to an address, and the receiver learns the address of the sender, so it can reply.&lt;br /&gt;
&lt;br /&gt;
Every socket which receives packets has a queue in the kernel. Packets from all senders wait there, in the order in which they arrived, until the owner of the socket takes them. With &amp;lt;code&amp;gt;AF_UNIX&amp;lt;/code&amp;gt;, no packet is lost.&lt;br /&gt;
&lt;br /&gt;
One side, the &amp;#039;&amp;#039;&amp;#039;server&amp;#039;&amp;#039;&amp;#039;, has an address which everybody knows. The others, the &amp;#039;&amp;#039;&amp;#039;clients&amp;#039;&amp;#039;&amp;#039;, send packets to that address. Both sides make the same calls:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Server !! Client&lt;br /&gt;
|-&lt;br /&gt;
| [https://man7.org/linux/man-pages/man2/socket.2.html &amp;lt;code&amp;gt;socket(2)&amp;lt;/code&amp;gt;]: a new socket || [https://man7.org/linux/man-pages/man2/socket.2.html &amp;lt;code&amp;gt;socket(2)&amp;lt;/code&amp;gt;]&lt;br /&gt;
|-&lt;br /&gt;
| [https://man7.org/linux/man-pages/man2/bind.2.html &amp;lt;code&amp;gt;bind(2)&amp;lt;/code&amp;gt;]: give it a known address || [https://man7.org/linux/man-pages/man2/bind.2.html &amp;lt;code&amp;gt;bind(2)&amp;lt;/code&amp;gt;]: give it any free address, so that the server can reply&lt;br /&gt;
|-&lt;br /&gt;
| [https://man7.org/linux/man-pages/man2/recvfrom.2.html &amp;lt;code&amp;gt;recvfrom(2)&amp;lt;/code&amp;gt;]: a packet, and who sent it || [https://man7.org/linux/man-pages/man2/sendto.2.html &amp;lt;code&amp;gt;sendto(2)&amp;lt;/code&amp;gt;]: a packet, to the address of the server&lt;br /&gt;
|-&lt;br /&gt;
| [https://man7.org/linux/man-pages/man2/sendto.2.html &amp;lt;code&amp;gt;sendto(2)&amp;lt;/code&amp;gt;]: the reply, to that sender || [https://man7.org/linux/man-pages/man2/recv.2.html &amp;lt;code&amp;gt;recv(2)&amp;lt;/code&amp;gt;]: the reply&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Creating a socket ===&lt;br /&gt;
&lt;br /&gt;
Both sides start in the same way: they create a socket, and fill in an address.&lt;br /&gt;
&lt;br /&gt;
==== socket(2) ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// In: &amp;lt;sys/socket.h&amp;gt;&lt;br /&gt;
int socket(int domain, int type, int protocol);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Parameter !! Description&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;domain&amp;lt;/code&amp;gt; || The family: &amp;lt;code&amp;gt;AF_UNIX&amp;lt;/code&amp;gt; in this lab&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;type&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;SOCK_DGRAM&amp;lt;/code&amp;gt; in this lab. Others are &amp;lt;code&amp;gt;SOCK_STREAM&amp;lt;/code&amp;gt; (a connection which carries a stream of bytes, like a pipe) and &amp;lt;code&amp;gt;SOCK_SEQPACKET&amp;lt;/code&amp;gt; (a connection which carries packets)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;protocol&amp;lt;/code&amp;gt; || 0: the usual protocol for the family and type&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It returns a new file descriptor, or -1 on error. The new socket has no address yet.&lt;br /&gt;
&lt;br /&gt;
==== Addresses ====&lt;br /&gt;
&lt;br /&gt;
An &amp;lt;code&amp;gt;AF_UNIX&amp;lt;/code&amp;gt; address is a path, kept in a &amp;lt;code&amp;gt;struct sockaddr_un&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// In: &amp;lt;sys/un.h&amp;gt;&lt;br /&gt;
struct sockaddr_un {&lt;br /&gt;
    sa_family_t sun_family; // AF_UNIX&lt;br /&gt;
    char sun_path[108];     // The path of the socket file&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Every family has its own structure, but the socket functions take any of them, as a &amp;lt;code&amp;gt;struct sockaddr *&amp;lt;/code&amp;gt; and a size. We fill in our structure, and cast its address:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct sockaddr_un addr = { .sun_family = AF_UNIX, .sun_path = &amp;quot;upper.sock&amp;quot; };&lt;br /&gt;
bind(s, (struct sockaddr *)&amp;amp;addr, sizeof addr);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A relative path, like the one above, is relative to the current directory of the process.&lt;br /&gt;
&lt;br /&gt;
=== bind(2) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// In: &amp;lt;sys/socket.h&amp;gt;&lt;br /&gt;
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://man7.org/linux/man-pages/man2/bind.2.html &amp;lt;code&amp;gt;bind(2)&amp;lt;/code&amp;gt;] gives the socket an address, so that others can send packets to it.&lt;br /&gt;
&lt;br /&gt;
On the server, the address is a path. [https://man7.org/linux/man-pages/man2/bind.2.html &amp;lt;code&amp;gt;bind(2)&amp;lt;/code&amp;gt;] creates a &amp;#039;&amp;#039;&amp;#039;socket file&amp;#039;&amp;#039;&amp;#039; at that path, whose type &amp;lt;code&amp;gt;ls -l&amp;lt;/code&amp;gt; shows as &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;. The file is only a name: no data is ever written to it. To send to the socket, a process needs write permission on this file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039; The socket file stays after the server ends, and [https://man7.org/linux/man-pages/man2/bind.2.html &amp;lt;code&amp;gt;bind(2)&amp;lt;/code&amp;gt;] fails with &amp;lt;code&amp;gt;EADDRINUSE&amp;lt;/code&amp;gt; if the path exists. That is why servers usually call [https://man7.org/linux/man-pages/man2/unlink.2.html &amp;lt;code&amp;gt;unlink(2)&amp;lt;/code&amp;gt;] on the path first.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A client does not care about its own address, but it needs one: without it, the server could not reply. If &amp;lt;code&amp;gt;addrlen&amp;lt;/code&amp;gt; covers only &amp;lt;code&amp;gt;sun_family&amp;lt;/code&amp;gt;, the kernel picks a free name, such as &amp;lt;code&amp;gt;@a50a0&amp;lt;/code&amp;gt;. This name is not a file, and it goes away when the socket is closed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct sockaddr_un me = { .sun_family = AF_UNIX };&lt;br /&gt;
bind(s, (struct sockaddr *)&amp;amp;me, sizeof(sa_family_t)); // Any free name&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== sendto(2) and recvfrom(2) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// In: &amp;lt;sys/socket.h&amp;gt;&lt;br /&gt;
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,&lt;br /&gt;
               const struct sockaddr *dest_addr, socklen_t addrlen);&lt;br /&gt;
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,&lt;br /&gt;
                 struct sockaddr *src_addr, socklen_t *addrlen);&lt;br /&gt;
ssize_t recv(int sockfd, void *buf, size_t len, int flags);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* [https://man7.org/linux/man-pages/man2/sendto.2.html &amp;lt;code&amp;gt;sendto(2)&amp;lt;/code&amp;gt;] sends &amp;lt;code&amp;gt;len&amp;lt;/code&amp;gt; bytes as one packet, to the socket at &amp;lt;code&amp;gt;dest_addr&amp;lt;/code&amp;gt;.&lt;br /&gt;
* [https://man7.org/linux/man-pages/man2/recvfrom.2.html &amp;lt;code&amp;gt;recvfrom(2)&amp;lt;/code&amp;gt;] takes one packet from the queue, and returns its size. It blocks while the queue is empty. If the packet is larger than &amp;lt;code&amp;gt;len&amp;lt;/code&amp;gt;, the rest of it is lost.&lt;br /&gt;
* [https://man7.org/linux/man-pages/man2/recvfrom.2.html &amp;lt;code&amp;gt;recvfrom(2)&amp;lt;/code&amp;gt;] also tells us who sent the packet. Before the call, we set &amp;lt;code&amp;gt;*addrlen&amp;lt;/code&amp;gt; to the size allocated for the structure at &amp;lt;code&amp;gt;src_addr&amp;lt;/code&amp;gt;. The call writes the address of the sender into that structure, and sets &amp;lt;code&amp;gt;*addrlen&amp;lt;/code&amp;gt; to the real length of the address. To reply, we give the recieved address and length to [https://man7.org/linux/man-pages/man2/sendto.2.html &amp;lt;code&amp;gt;sendto(2)&amp;lt;/code&amp;gt;], as &amp;lt;code&amp;gt;dest_addr&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;addrlen&amp;lt;/code&amp;gt;.&lt;br /&gt;
* [https://man7.org/linux/man-pages/man2/recv.2.html &amp;lt;code&amp;gt;recv(2)&amp;lt;/code&amp;gt;] is [https://man7.org/linux/man-pages/man2/recvfrom.2.html &amp;lt;code&amp;gt;recvfrom(2)&amp;lt;/code&amp;gt;] without the address, for when we do not care who sent the packet.&lt;br /&gt;
&lt;br /&gt;
[https://man7.org/linux/man-pages/man2/sendto.2.html &amp;lt;code&amp;gt;sendto(2)&amp;lt;/code&amp;gt;] fails if nobody can receive at &amp;lt;code&amp;gt;dest_addr&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Error !! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt; || There is no such socket file&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;ECONNREFUSED&amp;lt;/code&amp;gt; || The address exists, but no socket has it anymore: for example, the server has ended, and its socket file stayed&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;EACCES&amp;lt;/code&amp;gt; || We have no write permission on the socket file&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;flags&amp;lt;/code&amp;gt; adds additional options to a single call. We will need one flag:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Flag !! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;MSG_DONTWAIT&amp;lt;/code&amp;gt; || Do not block. If there is no packet to receive, or no room to send one, fail at once with &amp;lt;code&amp;gt;EAGAIN&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039; The queue of a socket is limited. If the receiver does not take its packets, its queue fills up, and [https://man7.org/linux/man-pages/man2/sendto.2.html &amp;lt;code&amp;gt;sendto(2)&amp;lt;/code&amp;gt;] blocks until there is room again. A server must not stop because one client does not read, so it sends with &amp;lt;code&amp;gt;MSG_DONTWAIT&amp;lt;/code&amp;gt;.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following program is a server which sends back every packet in upper case, and a client which connects to it and sends it its command line arguments:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot; line&amp;gt;&lt;br /&gt;
#include &amp;lt;ctype.h&amp;gt;&lt;br /&gt;
#include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/socket.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/un.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define PATH &amp;quot;upper.sock&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void print_error(const char *what)&lt;br /&gt;
{&lt;br /&gt;
    char msg[256];&lt;br /&gt;
    snprintf(msg, sizeof msg, &amp;quot;%s: %s\n&amp;quot;, what, strerror(errno));&lt;br /&gt;
    write(STDERR_FILENO, msg, strlen(msg));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int server(void)&lt;br /&gt;
{&lt;br /&gt;
    int s = socket(AF_UNIX, SOCK_DGRAM, 0);&lt;br /&gt;
    if (s == -1) {&lt;br /&gt;
        print_error(&amp;quot;socket&amp;quot;);&lt;br /&gt;
        return EXIT_FAILURE;&lt;br /&gt;
    }&lt;br /&gt;
    struct sockaddr_un addr = { .sun_family = AF_UNIX, .sun_path = PATH };&lt;br /&gt;
    unlink(PATH); // remove it in case it existed before&lt;br /&gt;
    if (bind(s, (struct sockaddr *)&amp;amp;addr, sizeof addr) == -1) {&lt;br /&gt;
        print_error(&amp;quot;bind&amp;quot;);&lt;br /&gt;
        return EXIT_FAILURE;&lt;br /&gt;
    }&lt;br /&gt;
    for (;;) {&lt;br /&gt;
        char buf[256];&lt;br /&gt;
        struct sockaddr_un from;&lt;br /&gt;
        socklen_t len = sizeof from;&lt;br /&gt;
        ssize_t n = recvfrom(s, buf, sizeof buf, 0, (struct sockaddr *)&amp;amp;from, &amp;amp;len);&lt;br /&gt;
        if (n == -1) {&lt;br /&gt;
            print_error(&amp;quot;recvfrom&amp;quot;);&lt;br /&gt;
            continue;&lt;br /&gt;
        }&lt;br /&gt;
        for (ssize_t i = 0; i &amp;lt; n; i++) {&lt;br /&gt;
            buf[i] = toupper(buf[i]);&lt;br /&gt;
        }&lt;br /&gt;
        sendto(s, buf, n, 0, (struct sockaddr *)&amp;amp;from, len); // back to the sender&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int client(int count, char *words[])&lt;br /&gt;
{&lt;br /&gt;
    int s = socket(AF_UNIX, SOCK_DGRAM, 0);&lt;br /&gt;
    if (s == -1) {&lt;br /&gt;
        print_error(&amp;quot;socket&amp;quot;);&lt;br /&gt;
        return EXIT_FAILURE;&lt;br /&gt;
    }&lt;br /&gt;
    struct sockaddr_un me = { .sun_family = AF_UNIX };&lt;br /&gt;
    if (bind(s, (struct sockaddr *)&amp;amp;me, sizeof(sa_family_t)) == -1) { // any free name&lt;br /&gt;
        print_error(&amp;quot;bind&amp;quot;);&lt;br /&gt;
        return EXIT_FAILURE;&lt;br /&gt;
    }&lt;br /&gt;
    struct sockaddr_un server = { .sun_family = AF_UNIX, .sun_path = PATH };&lt;br /&gt;
    for (int i = 0; i &amp;lt; count; i++) {&lt;br /&gt;
        if (sendto(s, words[i], strlen(words[i]), 0, (struct sockaddr *)&amp;amp;server, sizeof server) == -1) {&lt;br /&gt;
            print_error(&amp;quot;sendto&amp;quot;);&lt;br /&gt;
            return EXIT_FAILURE;&lt;br /&gt;
        }&lt;br /&gt;
        char buf[256];&lt;br /&gt;
        ssize_t n = recv(s, buf, sizeof buf, 0);&lt;br /&gt;
        if (n == -1) {&lt;br /&gt;
            print_error(&amp;quot;recv&amp;quot;);&lt;br /&gt;
            return EXIT_FAILURE;&lt;br /&gt;
        }&lt;br /&gt;
        char line[300];&lt;br /&gt;
        int len = snprintf(line, sizeof line, &amp;quot;%.*s\n&amp;quot;, (int)n, buf);&lt;br /&gt;
        write(STDOUT_FILENO, line, len);&lt;br /&gt;
        sleep(1);&lt;br /&gt;
    }&lt;br /&gt;
    close(s);&lt;br /&gt;
    return EXIT_SUCCESS;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    if (argc == 2 &amp;amp;&amp;amp; strcmp(argv[1], &amp;quot;server&amp;quot;) == 0) {&lt;br /&gt;
        return server();&lt;br /&gt;
    }&lt;br /&gt;
    if (argc &amp;gt;= 2 &amp;amp;&amp;amp; strcmp(argv[1], &amp;quot;client&amp;quot;) == 0) {&lt;br /&gt;
        return client(argc - 2, argv + 2);&lt;br /&gt;
    }&lt;br /&gt;
    const char *usage = &amp;quot;usage: app server | client &amp;lt;word&amp;gt;...\n&amp;quot;;&lt;br /&gt;
    write(STDERR_FILENO, usage, strlen(usage));&lt;br /&gt;
    return EXIT_FAILURE;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start the server in the background with &amp;lt;code&amp;gt;&amp;amp;&amp;lt;/code&amp;gt;, and run the client:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./build/debug/app server &amp;amp;&lt;br /&gt;
$ ls -l upper.sock&lt;br /&gt;
srwxr-xr-x 1 john john 0 Nov 17 12:03 upper.sock&lt;br /&gt;
$ ./build/debug/app client hello socket world&lt;br /&gt;
HELLO&lt;br /&gt;
SOCKET&lt;br /&gt;
WORLD&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now start two clients at the same time:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./build/debug/app client one two three &amp;amp; ./build/debug/app client four five six&lt;br /&gt;
ONE&lt;br /&gt;
FOUR&lt;br /&gt;
FIVE&lt;br /&gt;
TWO&lt;br /&gt;
THREE&lt;br /&gt;
SIX&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Both clients are served at the same time, by a single thread. The server has one socket for all its clients, and takes the packets in the order in which they arrive, whoever sent them. The order of the lines may differ from one run to the next.&lt;br /&gt;
&lt;br /&gt;
[[File:OS_Lab_7_-_dgram.png|center|thumb|500px|Two clients sent a packet to &amp;lt;code&amp;gt;upper.sock&amp;lt;/code&amp;gt;. [https://man7.org/linux/man-pages/man2/recvfrom.2.html &amp;lt;code&amp;gt;recvfrom(2)&amp;lt;/code&amp;gt;] returns the first one, with the address of its sender, and the server replies to that address.]]&lt;br /&gt;
&lt;br /&gt;
Stop the server with &amp;lt;code&amp;gt;kill %1&amp;lt;/code&amp;gt;, and run a client again. Its [https://man7.org/linux/man-pages/man2/sendto.2.html &amp;lt;code&amp;gt;sendto(2)&amp;lt;/code&amp;gt;] fails with &amp;lt;code&amp;gt;ECONNREFUSED&amp;lt;/code&amp;gt;: the socket file is still there, but no process has a socket with that address anymore.&lt;br /&gt;
&lt;br /&gt;
== Waiting in readline ==&lt;br /&gt;
&lt;br /&gt;
As part of this lab we will build a chat client.&lt;br /&gt;
&lt;br /&gt;
A chat client must wait for two things at once: the user typing a line, and packets from the server. But [https://man7.org/linux/man-pages/man3/readline.3.html &amp;lt;code&amp;gt;readline(3)&amp;lt;/code&amp;gt;] only returns when the user presses Enter. For this, readline can call a function of ours while it waits:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// In: &amp;lt;readline/readline.h&amp;gt;&lt;br /&gt;
extern int (*rl_event_hook)(void); // Called while readline waits for a key&lt;br /&gt;
extern int rl_done;                // Set it to 1, and readline() returns&lt;br /&gt;
&lt;br /&gt;
int rl_on_new_line(void); // Tells readline that the cursor is on a new, empty line&lt;br /&gt;
void rl_redisplay(void);  // Draws the prompt and the line typed so far&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While no key is pressed, [https://man7.org/linux/man-pages/man3/readline.3.html &amp;lt;code&amp;gt;readline(3)&amp;lt;/code&amp;gt;] calls &amp;lt;code&amp;gt;rl_event_hook&amp;lt;/code&amp;gt; about 10 times a second. The function must return quickly, so it must not block: it can receive with &amp;lt;code&amp;gt;MSG_DONTWAIT&amp;lt;/code&amp;gt;, and stop when [https://man7.org/linux/man-pages/man2/recv.2.html &amp;lt;code&amp;gt;recv(2)&amp;lt;/code&amp;gt;] fails with &amp;lt;code&amp;gt;EAGAIN&amp;lt;/code&amp;gt;. Its return value is not used.&lt;br /&gt;
&lt;br /&gt;
When the hook prints something, the cursor is after the prompt and the half-typed line. To print above them:&lt;br /&gt;
&lt;br /&gt;
# write &amp;lt;code&amp;gt;&amp;quot;\r&amp;quot;&amp;lt;/code&amp;gt;, which moves the cursor to the start of the line, and &amp;lt;code&amp;gt;&amp;quot;\033[K&amp;quot;&amp;lt;/code&amp;gt;, an escape sequence which asks the terminal to clear the rest of the line;&lt;br /&gt;
# write our text, ending with &amp;lt;code&amp;gt;&amp;quot;\n&amp;quot;&amp;lt;/code&amp;gt;;&lt;br /&gt;
# call &amp;lt;code&amp;gt;rl_on_new_line()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;rl_redisplay()&amp;lt;/code&amp;gt;, so that readline draws the prompt and the typed text again, on the next line.&lt;br /&gt;
&lt;br /&gt;
The following program prints the time every 3 seconds, while the user types:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot; line&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;time.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;readline/history.h&amp;gt;&lt;br /&gt;
#include &amp;lt;readline/readline.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
static time_t last;&lt;br /&gt;
&lt;br /&gt;
static void print(const char *msg)&lt;br /&gt;
{&lt;br /&gt;
    write(STDOUT_FILENO, msg, strlen(msg));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Prints a line above the prompt, and draws the prompt again below it.&lt;br /&gt;
static void print_above(const char *msg)&lt;br /&gt;
{&lt;br /&gt;
    print(&amp;quot;\r\033[K&amp;quot;); // Back to the start of the line, and clear it&lt;br /&gt;
    print(msg);&lt;br /&gt;
    rl_on_new_line();&lt;br /&gt;
    rl_redisplay();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// readline() calls this about 10 times a second, while it waits for a key.&lt;br /&gt;
static int on_idle(void)&lt;br /&gt;
{&lt;br /&gt;
    time_t now = time(nullptr);&lt;br /&gt;
    if (now - last &amp;gt;= 3) {&lt;br /&gt;
        last = now;&lt;br /&gt;
        struct tm tm;&lt;br /&gt;
        localtime_r(&amp;amp;now, &amp;amp;tm);&lt;br /&gt;
        char msg[64];&lt;br /&gt;
        strftime(msg, sizeof msg, &amp;quot;* %H:%M:%S\n&amp;quot;, &amp;amp;tm);&lt;br /&gt;
        print_above(msg);&lt;br /&gt;
    }&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
    last = time(nullptr);&lt;br /&gt;
    rl_event_hook = on_idle;&lt;br /&gt;
&lt;br /&gt;
    char *line;&lt;br /&gt;
    while ((line = readline(&amp;quot;&amp;gt; &amp;quot;)) != nullptr) {&lt;br /&gt;
        char msg[512];&lt;br /&gt;
        snprintf(msg, sizeof msg, &amp;quot;you typed: %s\n&amp;quot;, line);&lt;br /&gt;
        print(msg);&lt;br /&gt;
        free(line);&lt;br /&gt;
    }&lt;br /&gt;
    return EXIT_SUCCESS;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./build/debug/app&lt;br /&gt;
* 12:33:21&lt;br /&gt;
&amp;gt; hello wor&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Start typing, and wait: the time is printed above the prompt, and what you typed is drawn again below it, so you can go on typing.&lt;br /&gt;
&lt;br /&gt;
== Exercises ==&lt;br /&gt;
&lt;br /&gt;
Write the program in a new copy of the project template. As a reminder from [[OS_Lab_1_-_Linux_setup._Bash_primer._C_primer#getting-the-template|the first lab]]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mkdir -p ~/oslab &amp;amp;&amp;amp; cd ~/oslab&lt;br /&gt;
curl -OL https://wiki.dcae.pub.ro/images/9/93/Os_lab_skeleton.zip&lt;br /&gt;
unzip -d lab07 Os_lab_skeleton.zip&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For this lab:&lt;br /&gt;
&lt;br /&gt;
* As in the last labs, do not use the standard I/O library for output: build the text with [https://man7.org/linux/man-pages/man3/snprintf.3.html &amp;lt;code&amp;gt;snprintf(3)&amp;lt;/code&amp;gt;] and [https://man7.org/linux/man-pages/man2/write.2.html &amp;lt;code&amp;gt;write(2)&amp;lt;/code&amp;gt;] it. Use [https://man7.org/linux/man-pages/man3/readline.3.html &amp;lt;code&amp;gt;readline(3)&amp;lt;/code&amp;gt;] for input.&lt;br /&gt;
* Both the server and the client have a single thread: no [https://man7.org/linux/man-pages/man3/pthread_create.3.html &amp;lt;code&amp;gt;pthread_create(3)&amp;lt;/code&amp;gt;], no [https://man7.org/linux/man-pages/man2/fork.2.html &amp;lt;code&amp;gt;fork(2)&amp;lt;/code&amp;gt;].&lt;br /&gt;
* The server has a single socket, and the only call in which it waits is [https://man7.org/linux/man-pages/man2/recvfrom.2.html &amp;lt;code&amp;gt;recvfrom(2)&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
=== MyChat ===&lt;br /&gt;
&lt;br /&gt;
In this exercise you will write MyChat, a chat room for the users of one machine. A single program is both the server and the client, depending on its argument. To try it, start the server in one terminal, and each client in a terminal of its own, all in the folder of the project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
./build/debug/app --server&lt;br /&gt;
./build/debug/app --client&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The client reads lines from the user. A client which has just started is not in the chat yet: the user must first pick a name with &amp;lt;code&amp;gt;/join&amp;lt;/code&amp;gt;. The client should understand the following:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! The user types !! What the client should do&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;/join NAME&amp;lt;/code&amp;gt; || Join the chat as &amp;lt;code&amp;gt;NAME&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;/leave&amp;lt;/code&amp;gt; || Leave the chat. The client keeps running, and can &amp;lt;code&amp;gt;/join&amp;lt;/code&amp;gt; again&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;/dm NAME TEXT&amp;lt;/code&amp;gt; || Send &amp;lt;code&amp;gt;TEXT&amp;lt;/code&amp;gt; only to &amp;lt;code&amp;gt;NAME&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Any other line || Send it to everyone in the chat&lt;br /&gt;
|-&lt;br /&gt;
| Ctrl+D || Leave the chat, if it joined, and end&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
To understand each other, the server and the clients must agree on what a packet contains: they need a &amp;#039;&amp;#039;&amp;#039;protocol&amp;#039;&amp;#039;&amp;#039;. A real chat protocol, such as IRC, has to work between different programs on different machines, and takes many pages to describe.&lt;br /&gt;
&lt;br /&gt;
The protocol we propose for this lab is a simple example: every packet is one &amp;lt;code&amp;gt;struct myc_packet&amp;lt;/code&amp;gt;, sent as it is in memory. The structure is defined as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// The socket file, in the current directory.&lt;br /&gt;
#define MYC_PATH &amp;quot;mychat.sock&amp;quot;&lt;br /&gt;
&lt;br /&gt;
#define MYC_NAME_MAX 32  // Including the final &amp;#039;\0&amp;#039;&lt;br /&gt;
#define MYC_TEXT_MAX 256 // Including the final &amp;#039;\0&amp;#039;&lt;br /&gt;
&lt;br /&gt;
enum myc_type {&lt;br /&gt;
    // Sent by the user&lt;br /&gt;
    MYC_U_JOIN = 1, // name: the nickname&lt;br /&gt;
    MYC_U_LEAVE,    // -&lt;br /&gt;
    MYC_U_MSG,      // text&lt;br /&gt;
    MYC_U_DM,       // name: to whom&lt;br /&gt;
                    // text&lt;br /&gt;
&lt;br /&gt;
    // Sent by the server&lt;br /&gt;
    MYC_S_JOIN,  // name: who joined&lt;br /&gt;
    MYC_S_LEAVE, // name: who left&lt;br /&gt;
                 // clean: true if they sent MYC_U_LEAVE, false if they errored out&lt;br /&gt;
    MYC_S_MSG,   // name: from whom&lt;br /&gt;
                 // dm: if it was sent only to us&lt;br /&gt;
                 // text&lt;br /&gt;
    MYC_S_ERR,   // text: what went wrong&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Every packet has the same size, whatever its type.&lt;br /&gt;
struct myc_packet {&lt;br /&gt;
    int type;&lt;br /&gt;
    bool clean;&lt;br /&gt;
    bool dm;&lt;br /&gt;
    char name[MYC_NAME_MAX];&lt;br /&gt;
    char text[MYC_TEXT_MAX];&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this protocol, every packet has space for all the fields, but each type uses only some of them:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Type !! Sent by !! Fields !! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;MYC_U_JOIN&amp;lt;/code&amp;gt; || client || &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; || I want to join with this name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;MYC_U_LEAVE&amp;lt;/code&amp;gt; || client || || I am leaving the chat&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;MYC_U_MSG&amp;lt;/code&amp;gt; || client || &amp;lt;code&amp;gt;text&amp;lt;/code&amp;gt; || Send this to everyone&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;MYC_U_DM&amp;lt;/code&amp;gt; || client || &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;text&amp;lt;/code&amp;gt; || Send this only to &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;MYC_S_JOIN&amp;lt;/code&amp;gt; || server || &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; joined. Sent to everyone in the chat, including the new user&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;MYC_S_LEAVE&amp;lt;/code&amp;gt; || server || &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;clean&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; left. &amp;lt;code&amp;gt;clean&amp;lt;/code&amp;gt; is true if they sent &amp;lt;code&amp;gt;MYC_U_LEAVE&amp;lt;/code&amp;gt;, and false if their client ended without it&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;MYC_S_MSG&amp;lt;/code&amp;gt; || server || &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;dm&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;text&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; wrote &amp;lt;code&amp;gt;text&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;dm&amp;lt;/code&amp;gt; is true if it was sent only to us. A message is not sent back to its author&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;MYC_S_ERR&amp;lt;/code&amp;gt; || server || &amp;lt;code&amp;gt;text&amp;lt;/code&amp;gt; || The last request failed, and why&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
[[File:OS_Lab_7_-_mychat.png|center|thumb|500px|ana writes to everyone, and the server sends the message to everyone else in the chat. bob writes only to eve.]]&lt;br /&gt;
&lt;br /&gt;
With this protocol, a session with a server and two clients, in three terminals, could look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./build/debug/app --server&lt;br /&gt;
serving on mychat.sock&lt;br /&gt;
ana joined&lt;br /&gt;
john joined&lt;br /&gt;
ana left&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./build/debug/app --client&lt;br /&gt;
&amp;gt; hello?&lt;br /&gt;
! join first: /join &amp;lt;name&amp;gt;&lt;br /&gt;
&amp;gt; /join ana&lt;br /&gt;
* ana joined&lt;br /&gt;
* john joined&lt;br /&gt;
&amp;gt; hi john&lt;br /&gt;
&amp;lt;john&amp;gt; hey ana&lt;br /&gt;
[dm] &amp;lt;john&amp;gt; a secret&lt;br /&gt;
&amp;gt; /dm nobody hi&lt;br /&gt;
! no such user: nobody&lt;br /&gt;
&amp;gt; /leave&lt;br /&gt;
* ana left&lt;br /&gt;
&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./build/debug/app --client&lt;br /&gt;
&amp;gt; /join ana&lt;br /&gt;
! this name is taken&lt;br /&gt;
&amp;gt; /join john&lt;br /&gt;
* john joined&lt;br /&gt;
&amp;lt;ana&amp;gt; hi john&lt;br /&gt;
&amp;gt; hey ana&lt;br /&gt;
&amp;gt; /dm ana a secret&lt;br /&gt;
* ana left&lt;br /&gt;
&amp;gt; ^C&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
ana left with &amp;lt;code&amp;gt;/leave&amp;lt;/code&amp;gt; and then Ctrl+D, and john with Ctrl+C. The server does not know yet that john is gone: no packet told it. The server prints whatever you find useful.&lt;br /&gt;
&lt;br /&gt;
Build it in these steps:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Talking.&amp;#039;&amp;#039;&amp;#039; In &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt;, pick the server or the client from the argument. The server creates the socket, removes an old &amp;lt;code&amp;gt;mychat.sock&amp;lt;/code&amp;gt;, binds, and receives packets with [https://man7.org/linux/man-pages/man2/recvfrom.2.html &amp;lt;code&amp;gt;recvfrom(2)&amp;lt;/code&amp;gt;], in a loop. For each one, it prints its size. The client creates its socket, binds it to any free name, reads lines with [https://man7.org/linux/man-pages/man3/readline.3.html &amp;lt;code&amp;gt;readline(3)&amp;lt;/code&amp;gt;] until Ctrl+D, and sends each one to the server as a &amp;lt;code&amp;gt;MYC_U_MSG&amp;lt;/code&amp;gt;. Start a server and a few clients, and type some lines.&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Joining.&amp;#039;&amp;#039;&amp;#039; The server keeps a table of users, for example an array of 64 elements, each with a name and an address. An empty name marks a free element. It answers &amp;lt;code&amp;gt;MYC_U_JOIN&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;MYC_S_JOIN&amp;lt;/code&amp;gt; to everyone in the chat, or with &amp;lt;code&amp;gt;MYC_S_ERR&amp;lt;/code&amp;gt; if this address has already joined, if the name is empty, contains a space, or is taken, or if the table is full. It passes &amp;lt;code&amp;gt;MYC_U_MSG&amp;lt;/code&amp;gt; on as &amp;lt;code&amp;gt;MYC_S_MSG&amp;lt;/code&amp;gt;. Anything else from an address which has not joined gets a &amp;lt;code&amp;gt;MYC_S_ERR&amp;lt;/code&amp;gt;. The client sends &amp;lt;code&amp;gt;/join&amp;lt;/code&amp;gt; and plain lines, and sets &amp;lt;code&amp;gt;rl_event_hook&amp;lt;/code&amp;gt; to a function which receives every waiting packet and prints it, as in the session above.&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Leaving.&amp;#039;&amp;#039;&amp;#039; Handle &amp;lt;code&amp;gt;/leave&amp;lt;/code&amp;gt;, and send &amp;lt;code&amp;gt;MYC_S_LEAVE&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;clean&amp;lt;/code&amp;gt; set to everyone in the chat, including the user who left. On Ctrl+D, a client which is in the chat sends &amp;lt;code&amp;gt;MYC_U_LEAVE&amp;lt;/code&amp;gt; before it ends. To know whether it is in the chat, it can remember the name of its last &amp;lt;code&amp;gt;/join&amp;lt;/code&amp;gt;, and look for &amp;lt;code&amp;gt;MYC_S_JOIN&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MYC_S_LEAVE&amp;lt;/code&amp;gt; packets with that name. A client which ends without leaving, for example with Ctrl+C, sends nothing. The server finds out the next time it sends that user a packet: [https://man7.org/linux/man-pages/man2/sendto.2.html &amp;lt;code&amp;gt;sendto(2)&amp;lt;/code&amp;gt;] fails with &amp;lt;code&amp;gt;ECONNREFUSED&amp;lt;/code&amp;gt;. Then it removes the user, and sends &amp;lt;code&amp;gt;MYC_S_LEAVE&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;clean&amp;lt;/code&amp;gt; false to the others. In the same way, when the client&amp;#039;s [https://man7.org/linux/man-pages/man2/sendto.2.html &amp;lt;code&amp;gt;sendto(2)&amp;lt;/code&amp;gt;] fails with &amp;lt;code&amp;gt;ECONNREFUSED&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;, the server is gone: print it, and end.&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Direct messages.&amp;#039;&amp;#039;&amp;#039; Handle &amp;lt;code&amp;gt;/dm NAME TEXT&amp;lt;/code&amp;gt;. The server sends &amp;lt;code&amp;gt;MYC_S_MSG&amp;lt;/code&amp;gt;, with &amp;lt;code&amp;gt;dm&amp;lt;/code&amp;gt; set, only to &amp;lt;code&amp;gt;NAME&amp;lt;/code&amp;gt;, or &amp;lt;code&amp;gt;MYC_S_ERR&amp;lt;/code&amp;gt; if there is no such user.&lt;br /&gt;
&lt;br /&gt;
Some rules and hints:&lt;br /&gt;
&lt;br /&gt;
* The server sends with &amp;lt;code&amp;gt;MSG_DONTWAIT&amp;lt;/code&amp;gt;. A client which does not read must not stop the server. If a send fails with &amp;lt;code&amp;gt;EAGAIN&amp;lt;/code&amp;gt;, that client loses the packet.&lt;br /&gt;
* The client sends without flags, and receives, in the hook, with &amp;lt;code&amp;gt;MSG_DONTWAIT&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Two addresses are the same if they have the same length, and [https://man7.org/linux/man-pages/man3/memcmp.3.html &amp;lt;code&amp;gt;memcmp(3)&amp;lt;/code&amp;gt;] finds the same bytes in that length. Keep the length which [https://man7.org/linux/man-pages/man2/recvfrom.2.html &amp;lt;code&amp;gt;recvfrom(2)&amp;lt;/code&amp;gt;] gave you next to each address.&lt;br /&gt;
* While the server sends a packet to everyone, one send can fail with &amp;lt;code&amp;gt;ECONNREFUSED&amp;lt;/code&amp;gt;. Removing that user sends &amp;lt;code&amp;gt;MYC_S_LEAVE&amp;lt;/code&amp;gt; to everyone, in the middle of the first loop. This only works if you take a user out of the table before you send &amp;lt;code&amp;gt;MYC_S_LEAVE&amp;lt;/code&amp;gt; about them.&lt;br /&gt;
* &amp;lt;code&amp;gt;struct myc_packet p = { .type = MYC_S_JOIN };&amp;lt;/code&amp;gt; sets every field which is not named to 0.&lt;br /&gt;
* &amp;lt;code&amp;gt;snprintf(p.name, sizeof p.name, &amp;quot;%s&amp;quot;, name)&amp;lt;/code&amp;gt; copies a string without overflowing. It returns the length of the whole string, so a result of &amp;lt;code&amp;gt;sizeof p.name&amp;lt;/code&amp;gt; or more means that the name did not fit.&lt;br /&gt;
* To split the line of &amp;lt;code&amp;gt;/dm&amp;lt;/code&amp;gt;, take the command and the name with [https://man7.org/linux/man-pages/man3/strtok_r.3.html &amp;lt;code&amp;gt;strtok_r(3)&amp;lt;/code&amp;gt;], and then the rest of the line with &amp;lt;code&amp;gt;strtok_r(nullptr, &amp;quot;&amp;quot;, &amp;amp;save)&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Lines which do not come from the hook, such as an error for an unknown command, are printed with a plain [https://man7.org/linux/man-pages/man2/write.2.html &amp;lt;code&amp;gt;write(2)&amp;lt;/code&amp;gt;]: readline is not waiting at that moment.&lt;/div&gt;</description>
			<pubDate>Fri, 20 Nov 2026 12:20:01 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie:OS_Lab_7_-_Sockets._Datagrams</comments>
		</item>
		<item>
			<title>Operating Systems</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Operating_Systems&amp;diff=8451&amp;oldid=8447</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Operating_Systems&amp;diff=8451&amp;oldid=8447</guid>
			<description>&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Lab Sessions&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left diff-editfont-monospace&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ro&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Versiunea anterioară&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Versiunea de la data 20 noiembrie 2026 12:19&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l7&quot; &gt;Linia 7:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Linia 7:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab 5 - [[OS Lab 5 - Threads. Mutexes]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab 5 - [[OS Lab 5 - Threads. Mutexes]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab 6 - [[OS Lab 6 - Users. Permissions]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab 6 - [[OS Lab 6 - Users. Permissions]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* Lab 7 - [[OS Lab 7 - Sockets. Datagrams]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=== Old Lab Sessions (2025) ===&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=== Old Lab Sessions (2025) ===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</description>
			<pubDate>Fri, 20 Nov 2026 12:19:35 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie:Operating_Systems</comments>
		</item>
		<item>
			<title>Fișier:OS Lab 6 - open check.png</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:OS_Lab_6_-_open_check.png&amp;diff=8450&amp;oldid=0</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:OS_Lab_6_-_open_check.png&amp;diff=8450&amp;oldid=0</guid>
			<description>&lt;p&gt;&lt;a href=&quot;/index.php?title=Utilizator:Vserbu&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new mw-userlink&quot; title=&quot;Utilizator:Vserbu (pagină inexistentă)&quot;&gt;&lt;bdi&gt;Vserbu&lt;/bdi&gt;&lt;/a&gt; a încărcat &lt;a href=&quot;/index.php/Fi%C8%99ier:OS_Lab_6_-_open_check.png&quot; title=&quot;Fișier:OS Lab 6 - open check.png&quot;&gt;Fișier:OS Lab 6 - open check.png&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pagină nouă&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;/div&gt;</description>
			<pubDate>Fri, 13 Nov 2026 14:15:58 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie_Fi%C8%99ier:OS_Lab_6_-_open_check.png</comments>
		</item>
		<item>
			<title>Fișier:OS Lab 6 - mode bits.png</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:OS_Lab_6_-_mode_bits.png&amp;diff=8449&amp;oldid=0</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:OS_Lab_6_-_mode_bits.png&amp;diff=8449&amp;oldid=0</guid>
			<description>&lt;p&gt;&lt;a href=&quot;/index.php?title=Utilizator:Vserbu&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new mw-userlink&quot; title=&quot;Utilizator:Vserbu (pagină inexistentă)&quot;&gt;&lt;bdi&gt;Vserbu&lt;/bdi&gt;&lt;/a&gt; a încărcat &lt;a href=&quot;/index.php/Fi%C8%99ier:OS_Lab_6_-_mode_bits.png&quot; title=&quot;Fișier:OS Lab 6 - mode bits.png&quot;&gt;Fișier:OS Lab 6 - mode bits.png&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pagină nouă&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;/div&gt;</description>
			<pubDate>Fri, 13 Nov 2026 14:15:45 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie_Fi%C8%99ier:OS_Lab_6_-_mode_bits.png</comments>
		</item>
		<item>
			<title>OS Lab 6 - Users. Permissions</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=OS_Lab_6_-_Users._Permissions&amp;diff=8448&amp;oldid=0</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=OS_Lab_6_-_Users._Permissions&amp;diff=8448&amp;oldid=0</guid>
			<description>&lt;p&gt;Pagină nouă: In &lt;a href=&quot;/index.php/OS_Lab_5_-_Threads._Mutexes&quot; title=&quot;OS Lab 5 - Threads. Mutexes&quot;&gt;the last lab&lt;/a&gt;, we gave one process several threads. In this lab, we will look at how the kernel decides what a task is allowed to do. It compares...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pagină nouă&lt;/b&gt;&lt;/p&gt;&lt;div&gt;In [[OS_Lab_5_-_Threads._Mutexes|the last lab]], we gave one process several threads. In this lab, we will look at how the kernel decides what a task is allowed to do. It compares the numbers of the task, its &amp;#039;&amp;#039;&amp;#039;credentials&amp;#039;&amp;#039;&amp;#039;, with the owner, group and mode of the file. We will read these numbers, turn them into names, and change the permissions of our own files. As an application, we will write our own &amp;lt;code&amp;gt;ls -l&amp;lt;/code&amp;gt;, and a tool which finds and repairs dangerous permissions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039; Everything in this lab works as a normal user, and you will not need &amp;lt;code&amp;gt;sudo&amp;lt;/code&amp;gt;. Some of the commands below fail with a permission error on purpose.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Learning objectives ==&lt;br /&gt;
&lt;br /&gt;
By the end of this lab you should be able to:&lt;br /&gt;
&lt;br /&gt;
# read the user and group IDs of a task, and turn them into names;&lt;br /&gt;
# read the owner, group and mode of a file with [https://man7.org/linux/man-pages/man2/statx.2.html &amp;lt;code&amp;gt;statx(2)&amp;lt;/code&amp;gt;], and change the mode with [https://man7.org/linux/man-pages/man2/chmod.2.html &amp;lt;code&amp;gt;chmod(2)&amp;lt;/code&amp;gt;];&lt;br /&gt;
# say which class of permission bits applies when a task uses a file;&lt;br /&gt;
# explain what read, write and execute mean for files and for directories;&lt;br /&gt;
# recognize the set-user-ID, set-group-ID and sticky bits.&lt;br /&gt;
&lt;br /&gt;
== The credentials of a task ==&lt;br /&gt;
&lt;br /&gt;
Every task has a set of numbers, which the kernel uses for every permission check. First, it has three user IDs:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! ID !! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| Real UID || Who started the task&lt;br /&gt;
|-&lt;br /&gt;
| Effective UID || Whose rights the task uses&lt;br /&gt;
|-&lt;br /&gt;
| Saved set-user-ID || One more value the task can go back to&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It also has a real GID, an effective GID and a saved set-group-ID, which work in the same way, and a list of &amp;#039;&amp;#039;&amp;#039;supplementary groups&amp;#039;&amp;#039;&amp;#039;. All of them are copied to the child by [https://man7.org/linux/man-pages/man2/fork.2.html &amp;lt;code&amp;gt;fork(2)&amp;lt;/code&amp;gt;], and kept across &amp;lt;code&amp;gt;exec&amp;lt;/code&amp;gt;, so everything started from a shell normally has the same credentials as the shell.&lt;br /&gt;
&lt;br /&gt;
=== getresuid(2) and getresgid(2) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// In: &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);&lt;br /&gt;
int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);&lt;br /&gt;
&lt;br /&gt;
uid_t getuid(void);  // The real UID&lt;br /&gt;
uid_t geteuid(void); // The effective UID&lt;br /&gt;
gid_t getgid(void);  // The real GID&lt;br /&gt;
gid_t getegid(void); // The effective GID&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://man7.org/linux/man-pages/man2/getresuid.2.html &amp;lt;code&amp;gt;getresuid(2)&amp;lt;/code&amp;gt;] and [https://man7.org/linux/man-pages/man2/getresgid.2.html &amp;lt;code&amp;gt;getresgid(2)&amp;lt;/code&amp;gt;] store the three IDs at the given addresses. The four shorter functions return a single ID, and cannot fail.&lt;br /&gt;
&lt;br /&gt;
=== getgroups(2) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// In: &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
int getgroups(int size, gid_t list[]);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Parameter !! Description&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;size&amp;lt;/code&amp;gt; || How many elements &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; can hold&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; || Where to store the supplementary groups&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
It returns the number of groups, or -1 on error. If &amp;lt;code&amp;gt;size&amp;lt;/code&amp;gt; is 0, &amp;lt;code&amp;gt;list&amp;lt;/code&amp;gt; is not touched, and the call only returns the number of groups. This lets us first ask how many groups there are, and then declare an array of exactly that size: a &amp;#039;&amp;#039;&amp;#039;variable-length array&amp;#039;&amp;#039;&amp;#039; (VLA), whose size is only known at run time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039; A VLA must have at least one element. A task may have no supplementary groups at all, so the programs below make room for one more group than needed.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following program prints all the credentials of the task running it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot; line&amp;gt;&lt;br /&gt;
#include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
static void print_error(const char *what)&lt;br /&gt;
{&lt;br /&gt;
    char msg[256];&lt;br /&gt;
    snprintf(msg, sizeof msg, &amp;quot;%s: %s\n&amp;quot;, what, strerror(errno));&lt;br /&gt;
    write(STDERR_FILENO, msg, strlen(msg));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
    uid_t ruid, euid, suid;&lt;br /&gt;
    gid_t rgid, egid, sgid;&lt;br /&gt;
&lt;br /&gt;
    getresuid(&amp;amp;ruid, &amp;amp;euid, &amp;amp;suid);&lt;br /&gt;
    getresgid(&amp;amp;rgid, &amp;amp;egid, &amp;amp;sgid);&lt;br /&gt;
&lt;br /&gt;
    int n = getgroups(0, nullptr);&lt;br /&gt;
    if (n == -1) {&lt;br /&gt;
        print_error(&amp;quot;getgroups&amp;quot;);&lt;br /&gt;
        return EXIT_FAILURE;&lt;br /&gt;
    }&lt;br /&gt;
    gid_t groups[n + 1]; // + 1: a VLA cannot be empty&lt;br /&gt;
    n = getgroups(n, groups);&lt;br /&gt;
    if (n == -1) {&lt;br /&gt;
        print_error(&amp;quot;getgroups&amp;quot;);&lt;br /&gt;
        return EXIT_FAILURE;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    char msg[256];&lt;br /&gt;
    snprintf(msg, sizeof msg, &amp;quot;UID: real %u, effective %u, saved %u\n&amp;quot;, ruid, euid, suid);&lt;br /&gt;
    write(STDOUT_FILENO, msg, strlen(msg));&lt;br /&gt;
    snprintf(msg, sizeof msg, &amp;quot;GID: real %u, effective %u, saved %u\n&amp;quot;, rgid, egid, sgid);&lt;br /&gt;
    write(STDOUT_FILENO, msg, strlen(msg));&lt;br /&gt;
&lt;br /&gt;
    write(STDOUT_FILENO, &amp;quot;Groups:&amp;quot;, 7);&lt;br /&gt;
    for (int i = 0; i &amp;lt; n; i++) {&lt;br /&gt;
        snprintf(msg, sizeof msg, &amp;quot; %u&amp;quot;, groups[i]);&lt;br /&gt;
        write(STDOUT_FILENO, msg, strlen(msg));&lt;br /&gt;
    }&lt;br /&gt;
    write(STDOUT_FILENO, &amp;quot;\n&amp;quot;, 1);&lt;br /&gt;
    return EXIT_SUCCESS;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./build/debug/app&lt;br /&gt;
UID: real 1000, effective 1000, saved 1000&lt;br /&gt;
GID: real 1000, effective 1000, saved 1000&lt;br /&gt;
Groups: 1000&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Your numbers may be different. The three IDs are the same for almost every task, and only differ while special programs run, as we will see below.&lt;br /&gt;
&lt;br /&gt;
== From numbers to names ==&lt;br /&gt;
&lt;br /&gt;
The kernel does not know what users are called. Names are looked up in user space, by the C library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// In: &amp;lt;pwd.h&amp;gt;&lt;br /&gt;
struct passwd *getpwuid(uid_t uid);&lt;br /&gt;
&lt;br /&gt;
// In: &amp;lt;grp.h&amp;gt;&lt;br /&gt;
struct group *getgrgid(gid_t gid);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
They return a structure describing the user or group, or &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt; if nothing is known about that number. For example, a file can still belong to a user who has been deleted. A program must be ready to print the number instead.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// In: &amp;lt;pwd.h&amp;gt; (simplified)&lt;br /&gt;
struct passwd {&lt;br /&gt;
    char *pw_name;  // The name of the user&lt;br /&gt;
    uid_t pw_uid;   // Its UID&lt;br /&gt;
    gid_t pw_gid;   // Its primary group&lt;br /&gt;
    char *pw_dir;   // Its home directory&lt;br /&gt;
    char *pw_shell; // Its shell&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// In: &amp;lt;grp.h&amp;gt; (simplified)&lt;br /&gt;
struct group {&lt;br /&gt;
    char *gr_name; // The name of the group&lt;br /&gt;
    gid_t gr_gid;  // Its GID&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039; The returned structure is kept in a static buffer, which the next call to the same function overwrites. If you need a name after another lookup, copy it first.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following program prints the same line as the &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot; line&amp;gt;&lt;br /&gt;
#include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
#include &amp;lt;grp.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pwd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
static void print_error(const char *what)&lt;br /&gt;
{&lt;br /&gt;
    char msg[256];&lt;br /&gt;
    snprintf(msg, sizeof msg, &amp;quot;%s: %s\n&amp;quot;, what, strerror(errno));&lt;br /&gt;
    write(STDERR_FILENO, msg, strlen(msg));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void print_group(gid_t gid)&lt;br /&gt;
{&lt;br /&gt;
    const struct group *gr = getgrgid(gid);&lt;br /&gt;
    char msg[256];&lt;br /&gt;
    snprintf(msg, sizeof msg, &amp;quot;%u(%s)&amp;quot;, gid, gr != nullptr ? gr-&amp;gt;gr_name : &amp;quot;?&amp;quot;);&lt;br /&gt;
    write(STDOUT_FILENO, msg, strlen(msg));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
    int n = getgroups(0, nullptr);&lt;br /&gt;
    if (n == -1) {&lt;br /&gt;
        print_error(&amp;quot;getgroups&amp;quot;);&lt;br /&gt;
        return EXIT_FAILURE;&lt;br /&gt;
    }&lt;br /&gt;
    gid_t groups[n + 1]; // + 1: a VLA cannot be empty&lt;br /&gt;
    n = getgroups(n, groups);&lt;br /&gt;
    if (n == -1) {&lt;br /&gt;
        print_error(&amp;quot;getgroups&amp;quot;);&lt;br /&gt;
        return EXIT_FAILURE;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    uid_t uid = getuid();&lt;br /&gt;
    const struct passwd *pw = getpwuid(uid);&lt;br /&gt;
&lt;br /&gt;
    char msg[256];&lt;br /&gt;
    snprintf(msg, sizeof msg, &amp;quot;uid=%u(%s) gid=&amp;quot;, uid, pw != nullptr ? pw-&amp;gt;pw_name : &amp;quot;?&amp;quot;);&lt;br /&gt;
    write(STDOUT_FILENO, msg, strlen(msg));&lt;br /&gt;
    print_group(getgid());&lt;br /&gt;
&lt;br /&gt;
    write(STDOUT_FILENO, &amp;quot; groups=&amp;quot;, 8);&lt;br /&gt;
    for (int i = 0; i &amp;lt; n; i++) {&lt;br /&gt;
        if (i &amp;gt; 0) {&lt;br /&gt;
            write(STDOUT_FILENO, &amp;quot;,&amp;quot;, 1);&lt;br /&gt;
        }&lt;br /&gt;
        print_group(groups[i]);&lt;br /&gt;
    }&lt;br /&gt;
    write(STDOUT_FILENO, &amp;quot;\n&amp;quot;, 1);&lt;br /&gt;
    return EXIT_SUCCESS;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./build/debug/app&lt;br /&gt;
uid=1000(john) gid=1000(john) groups=1000(john)&lt;br /&gt;
$ id&lt;br /&gt;
uid=1000(john) gid=1000(john) groups=1000(john)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== The permissions of a file ==&lt;br /&gt;
&lt;br /&gt;
Every file has an &amp;#039;&amp;#039;&amp;#039;owner&amp;#039;&amp;#039;&amp;#039; (a UID), a &amp;#039;&amp;#039;&amp;#039;group&amp;#039;&amp;#039;&amp;#039; (a GID) and a &amp;#039;&amp;#039;&amp;#039;mode&amp;#039;&amp;#039;&amp;#039; of 16 bits. The mode holds the type of the file, 3 special bits, and 9 permission bits: read, write and execute, for each of the three classes.&lt;br /&gt;
&lt;br /&gt;
[[File:OS_Lab_6_-_mode_bits.png|center|thumb|500px|The owner, group and mode of a file. The mode is usually written in octal: each digit is one group of 3 bits. The file here is &amp;lt;code&amp;gt;rwxr-x---&amp;lt;/code&amp;gt;, or 0750.]]&lt;br /&gt;
&lt;br /&gt;
When a task uses a file, the kernel picks &amp;#039;&amp;#039;&amp;#039;exactly one&amp;#039;&amp;#039;&amp;#039; class, and only checks that one:&lt;br /&gt;
&lt;br /&gt;
[[File:OS_Lab_6_-_open_check.png|center|thumb|500px|The first match decides. An owner with &amp;lt;code&amp;gt;---&amp;lt;/code&amp;gt; cannot read the file, even if the others can.]]&lt;br /&gt;
&lt;br /&gt;
The checks use the effective IDs. A task with effective UID 0, the superuser, skips them. In addition, every directory on the path needs the execute permission.&lt;br /&gt;
&lt;br /&gt;
The mode bits have names, defined in &amp;lt;code&amp;gt;&amp;amp;lt;sys/stat.h&amp;amp;gt;&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Class !! Read !! Write !! Execute&lt;br /&gt;
|-&lt;br /&gt;
| Owner || &amp;lt;code&amp;gt;S_IRUSR&amp;lt;/code&amp;gt; (0400) || &amp;lt;code&amp;gt;S_IWUSR&amp;lt;/code&amp;gt; (0200) || &amp;lt;code&amp;gt;S_IXUSR&amp;lt;/code&amp;gt; (0100)&lt;br /&gt;
|-&lt;br /&gt;
| Group || &amp;lt;code&amp;gt;S_IRGRP&amp;lt;/code&amp;gt; (0040) || &amp;lt;code&amp;gt;S_IWGRP&amp;lt;/code&amp;gt; (0020) || &amp;lt;code&amp;gt;S_IXGRP&amp;lt;/code&amp;gt; (0010)&lt;br /&gt;
|-&lt;br /&gt;
| Others || &amp;lt;code&amp;gt;S_IROTH&amp;lt;/code&amp;gt; (0004) || &amp;lt;code&amp;gt;S_IWOTH&amp;lt;/code&amp;gt; (0002) || &amp;lt;code&amp;gt;S_IXOTH&amp;lt;/code&amp;gt; (0001)&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The three special bits are:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Bit !! Value !! Shown as !! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| Set-user-ID, &amp;lt;code&amp;gt;S_ISUID&amp;lt;/code&amp;gt; || 04000 || &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; instead of the owner&amp;#039;s &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; || The program runs with the effective UID of the owner of the file&lt;br /&gt;
|-&lt;br /&gt;
| Set-group-ID, &amp;lt;code&amp;gt;S_ISGID&amp;lt;/code&amp;gt; || 02000 || &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; instead of the group&amp;#039;s &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; || The program runs with the effective GID of the group of the file. On a directory: new files get the group of the directory&lt;br /&gt;
|-&lt;br /&gt;
| Sticky, &amp;lt;code&amp;gt;S_ISVTX&amp;lt;/code&amp;gt; || 01000 || &amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt; instead of the others&amp;#039; &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; || On a directory: an entry can only be deleted or renamed by its owner, the owner of the directory, or root&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
If the &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; underneath is not set, the letter is uppercase: &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== statx(2) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// In: &amp;lt;fcntl.h&amp;gt; and &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
int statx(int dirfd, const char *pathname, int flags, unsigned int mask, struct statx *statxbuf);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Parameter !! Description&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;dirfd&amp;lt;/code&amp;gt; || A directory to which a relative &amp;lt;code&amp;gt;pathname&amp;lt;/code&amp;gt; is relative, or &amp;lt;code&amp;gt;AT_FDCWD&amp;lt;/code&amp;gt; for the current directory&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;pathname&amp;lt;/code&amp;gt; || The file&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;flags&amp;lt;/code&amp;gt; || 0 to follow a symbolic link and describe its target, or &amp;lt;code&amp;gt;AT_SYMLINK_NOFOLLOW&amp;lt;/code&amp;gt; to describe the link itself&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;mask&amp;lt;/code&amp;gt; || Which fields we want. &amp;lt;code&amp;gt;STATX_BASIC_STATS&amp;lt;/code&amp;gt; asks for the usual ones&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;statxbuf&amp;lt;/code&amp;gt; || Where to store the result&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// In: &amp;lt;sys/stat.h&amp;gt; (simplified)&lt;br /&gt;
struct statx {&lt;br /&gt;
    uint16_t stx_mode;  // The type, special bits and permission bits&lt;br /&gt;
    uint32_t stx_uid;   // The owner&lt;br /&gt;
    uint32_t stx_gid;   // The group&lt;br /&gt;
    uint32_t stx_nlink; // The number of hard links&lt;br /&gt;
    uint64_t stx_size;  // The size in bytes&lt;br /&gt;
    struct statx_timestamp stx_atime; // Last access&lt;br /&gt;
    struct statx_timestamp stx_mtime; // Last modification&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
struct statx_timestamp {&lt;br /&gt;
   int64_t tv_sec;    /* Seconds since the Epoch (00:00 1st Jan 1970 UTC) */&lt;br /&gt;
   uint32_t tv_nsec;  /* Nanoseconds since tv_sec */&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The type is tested with macros: &amp;lt;code&amp;gt;S_ISREG(mode)&amp;lt;/code&amp;gt; (regular file), &amp;lt;code&amp;gt;S_ISDIR&amp;lt;/code&amp;gt; (directory), &amp;lt;code&amp;gt;S_ISLNK&amp;lt;/code&amp;gt; (symbolic link), &amp;lt;code&amp;gt;S_ISCHR&amp;lt;/code&amp;gt; (character device), &amp;lt;code&amp;gt;S_ISBLK&amp;lt;/code&amp;gt; (block device), &amp;lt;code&amp;gt;S_ISFIFO&amp;lt;/code&amp;gt; (pipe) and &amp;lt;code&amp;gt;S_ISSOCK&amp;lt;/code&amp;gt; (socket).&lt;br /&gt;
&lt;br /&gt;
[https://man7.org/linux/man-pages/man2/statx.2.html &amp;lt;code&amp;gt;statx(2)&amp;lt;/code&amp;gt;] only needs the execute permission on the directories of the path, and no permission on the file itself. The following program describes any file we can reach:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot; line&amp;gt;&lt;br /&gt;
#include &amp;lt;errno.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
#include &amp;lt;grp.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pwd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
static void print_error(const char *what)&lt;br /&gt;
{&lt;br /&gt;
    char msg[256];&lt;br /&gt;
    snprintf(msg, sizeof msg, &amp;quot;%s: %s\n&amp;quot;, what, strerror(errno));&lt;br /&gt;
    write(STDERR_FILENO, msg, strlen(msg));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
    if (argc != 2) {&lt;br /&gt;
        const char *usage = &amp;quot;usage: app &amp;lt;path&amp;gt;\n&amp;quot;;&lt;br /&gt;
        write(STDERR_FILENO, usage, strlen(usage));&lt;br /&gt;
        return EXIT_FAILURE;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    struct statx st;&lt;br /&gt;
    if (statx(AT_FDCWD, argv[1], 0, STATX_BASIC_STATS, &amp;amp;st) == -1) {&lt;br /&gt;
        print_error(argv[1]);&lt;br /&gt;
        return EXIT_FAILURE;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    const char *type = &amp;quot;other&amp;quot;;&lt;br /&gt;
    if (S_ISREG(st.stx_mode)) {&lt;br /&gt;
        type = &amp;quot;regular file&amp;quot;;&lt;br /&gt;
    } else if (S_ISDIR(st.stx_mode)) {&lt;br /&gt;
        type = &amp;quot;directory&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    const struct passwd *pw = getpwuid(st.stx_uid);&lt;br /&gt;
    const struct group *gr = getgrgid(st.stx_gid);&lt;br /&gt;
&lt;br /&gt;
    char msg[512];&lt;br /&gt;
    snprintf(msg, sizeof msg, &amp;quot;%s, owner %u (%s), group %u (%s), mode %04o\n&amp;quot;, type,&lt;br /&gt;
             st.stx_uid, pw != nullptr ? pw-&amp;gt;pw_name : &amp;quot;?&amp;quot;,&lt;br /&gt;
             st.stx_gid, gr != nullptr ? gr-&amp;gt;gr_name : &amp;quot;?&amp;quot;,&lt;br /&gt;
             st.stx_mode &amp;amp; 07777);&lt;br /&gt;
    write(STDOUT_FILENO, msg, strlen(msg));&lt;br /&gt;
    return EXIT_SUCCESS;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./build/debug/app /etc/shadow&lt;br /&gt;
regular file, owner 0 (root), group 42 (shadow), mode 0640&lt;br /&gt;
$ ./build/debug/app /tmp&lt;br /&gt;
directory, owner 0 (root), group 0 (root), mode 1777&lt;br /&gt;
$ ./build/debug/app /usr/bin/passwd&lt;br /&gt;
regular file, owner 0 (root), group 0 (root), mode 4755&lt;br /&gt;
$ ./build/debug/app /nope&lt;br /&gt;
/nope: No such file or directory&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;stx_mode&amp;lt;/code&amp;gt; also holds the type of the file, and &amp;lt;code&amp;gt;&amp;amp; 07777&amp;lt;/code&amp;gt; keeps only the special and permission bits. In the examples above:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;/etc/shadow&amp;lt;/code&amp;gt; holds the password hashes, and the others class has no permissions at all.&lt;br /&gt;
* &amp;lt;code&amp;gt;/tmp&amp;lt;/code&amp;gt; can be written by anyone, but the sticky bit (the leading 1) stops users from deleting each other&amp;#039;s files.&lt;br /&gt;
* &amp;lt;code&amp;gt;/usr/bin/passwd&amp;lt;/code&amp;gt; is set-user-ID (the leading 4) and belongs to root. This is how a normal user can change their own password: the program runs with effective UID 0, and it knows who started it from the real UID.&lt;br /&gt;
&lt;br /&gt;
=== chmod(2) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// In: &amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
int chmod(const char *pathname, mode_t mode);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://man7.org/linux/man-pages/man2/chmod.2.html &amp;lt;code&amp;gt;chmod(2)&amp;lt;/code&amp;gt;] replaces the permission and special bits of a file. Only the owner of the file, or root, may call it. The permission bits themselves do not matter: an owner with &amp;lt;code&amp;gt;---&amp;lt;/code&amp;gt; can still give itself &amp;lt;code&amp;gt;rw-&amp;lt;/code&amp;gt;. Since it replaces all the bits, changing just some of them means starting from the old mode, which [https://man7.org/linux/man-pages/man2/statx.2.html &amp;lt;code&amp;gt;statx(2)&amp;lt;/code&amp;gt;] gives us. For example, to remove the write permission of the group and of the others:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
mode_t new_mode = (st.stx_mode &amp;amp; 07777) &amp;amp; ~(S_IWGRP | S_IWOTH);&lt;br /&gt;
chmod(path, new_mode);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039; [https://man7.org/linux/man-pages/man2/chmod.2.html &amp;lt;code&amp;gt;chmod(2)&amp;lt;/code&amp;gt;] follows symbolic links: called on a link, it changes the file the link points to.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Trying it out ===&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;chmod&amp;lt;/code&amp;gt; command takes the same octal numbers. Try the following on your own files:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ echo hello &amp;gt; notes.txt&lt;br /&gt;
$ chmod 044 notes.txt&lt;br /&gt;
$ cat notes.txt&lt;br /&gt;
cat: notes.txt: Permission denied&lt;br /&gt;
$ chmod 644 notes.txt&lt;br /&gt;
$ cat notes.txt&lt;br /&gt;
hello&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With mode 044, everybody else can read the file, but its owner cannot: only the owner class is checked. On a directory, the three permissions mean something else:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Permission !! For a directory&lt;br /&gt;
|-&lt;br /&gt;
| Read || List the names&lt;br /&gt;
|-&lt;br /&gt;
| Write || Create, delete and rename entries&lt;br /&gt;
|-&lt;br /&gt;
| Execute || Go through it, to use the names inside&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ mkdir box&lt;br /&gt;
$ echo hi &amp;gt; box/a.txt&lt;br /&gt;
$ chmod 600 box&lt;br /&gt;
$ ls box&lt;br /&gt;
a.txt&lt;br /&gt;
$ cat box/a.txt&lt;br /&gt;
cat: box/a.txt: Permission denied&lt;br /&gt;
$ chmod 300 box&lt;br /&gt;
$ ls box&lt;br /&gt;
ls: cannot open directory &amp;#039;box&amp;#039;: Permission denied&lt;br /&gt;
$ cat box/a.txt&lt;br /&gt;
hi&lt;br /&gt;
$ chmod 500 box&lt;br /&gt;
$ touch box/b.txt&lt;br /&gt;
touch: cannot touch &amp;#039;box/b.txt&amp;#039;: Permission denied&lt;br /&gt;
$ rm box/a.txt&lt;br /&gt;
rm: cannot remove &amp;#039;box/a.txt&amp;#039;: Permission denied&lt;br /&gt;
$ chmod 700 box&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Without execute, we can see the names, but we cannot use them. Without read, we can use a name, if we already know it. Deleting a file needs write permission on its &amp;#039;&amp;#039;&amp;#039;directory&amp;#039;&amp;#039;&amp;#039;. The permissions of the file itself are not checked. Depending on its version, &amp;lt;code&amp;gt;ls box&amp;lt;/code&amp;gt; may also complain about each name in the first case.&lt;br /&gt;
&lt;br /&gt;
== Exercises ==&lt;br /&gt;
&lt;br /&gt;
Write each program in its own copy of the project template. As a reminder from [[OS_Lab_1_-_Linux_setup._Bash_primer._C_primer#getting-the-template|the first lab]]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mkdir -p ~/oslab/lab06 &amp;amp;&amp;amp; cd ~/oslab/lab06&lt;br /&gt;
curl -OL https://wiki.dcae.pub.ro/images/9/93/Os_lab_skeleton.zip&lt;br /&gt;
unzip -d ls Os_lab_skeleton.zip&lt;br /&gt;
unzip -d doctor Os_lab_skeleton.zip&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For this lab:&lt;br /&gt;
&lt;br /&gt;
* As in the last labs, do not use the standard I/O library for output: build the text with [https://man7.org/linux/man-pages/man3/snprintf.3.html &amp;lt;code&amp;gt;snprintf(3)&amp;lt;/code&amp;gt;] and [https://man7.org/linux/man-pages/man2/write.2.html &amp;lt;code&amp;gt;write(2)&amp;lt;/code&amp;gt;] it.&lt;br /&gt;
* Do not run other programs: make the system calls yourself.&lt;br /&gt;
* If a file cannot be reached, print the error on standard error, and continue with the next one.&lt;br /&gt;
&lt;br /&gt;
=== A. Your own ls -l ===&lt;br /&gt;
&lt;br /&gt;
Write a command which lists a directory like &amp;lt;code&amp;gt;ls -la&amp;lt;/code&amp;gt;. It takes the directory as an optional parameter, and uses the current directory if there is none. Build it in these steps:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Names and sizes.&amp;#039;&amp;#039;&amp;#039; Read the directory with [https://man7.org/linux/man-pages/man3/readdir.3.html &amp;lt;code&amp;gt;readdir(3)&amp;lt;/code&amp;gt;], as in [[OS_Lab_3_-_Files._Memory_mapping#readdir()|Lab 3]], and call [https://man7.org/linux/man-pages/man2/statx.2.html &amp;lt;code&amp;gt;statx(2)&amp;lt;/code&amp;gt;] for each entry. Print the size and the name.&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;The mode.&amp;#039;&amp;#039;&amp;#039; Print the mode as 10 characters: the type (&amp;lt;code&amp;gt;-&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;d&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;l&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;p&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;), then the 9 permission bits. Write a function which fills in a string.&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;The other columns.&amp;#039;&amp;#039;&amp;#039; Add the number of hard links, the owner, the group and the date. If a number has no name, print the number. For a symbolic link, also print &amp;lt;code&amp;gt;-&amp;gt; target&amp;lt;/code&amp;gt;.&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;The special bits.&amp;#039;&amp;#039;&amp;#039; Show &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; for the set-user-ID and set-group-ID bits, as described [[#The permissions of a file|above]]. You can ignore the sticky bit.&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;What can I do?&amp;#039;&amp;#039;&amp;#039; Add a first column with your own rights over the file, such as &amp;lt;code&amp;gt;r-x&amp;lt;/code&amp;gt;, by picking the class as the kernel does: compare with your effective UID, then with your effective GID and supplementary groups, or else use the others class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./build/debug/app&lt;br /&gt;
rwx drwxr-xr-x   1 john     john           44 2026-11-09 15:54 .&lt;br /&gt;
rwx drwxr-xr-x   1 john     john           50 2026-11-09 15:54 ..&lt;br /&gt;
rw- -rw-r-S---   1 john     john            6 2026-11-09 15:54 notes.txt&lt;br /&gt;
rwx lrwxrwxrwx   1 john     john            9 2026-11-09 15:54 link -&amp;gt; notes.txt&lt;br /&gt;
rwx drwxr-x--x   1 john     john            0 2026-11-09 15:54 sub&lt;br /&gt;
rwx -rwsr-xr-x   1 john     john        68064 2026-11-09 15:54 tool&lt;br /&gt;
$ ./build/debug/app /etc | grep shadow&lt;br /&gt;
--- -rw-r-----   1 root     shadow        840 2026-11-09 15:54 shadow&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The names do not have to be sorted. The widths of the columns are up to you.&lt;br /&gt;
&lt;br /&gt;
* You do not need to build a path for each entry: give [https://man7.org/linux/man-pages/man2/statx.2.html &amp;lt;code&amp;gt;statx(2)&amp;lt;/code&amp;gt;] the directory through &amp;lt;code&amp;gt;dirfd(dir)&amp;lt;/code&amp;gt;, which returns the file descriptor of an open &amp;lt;code&amp;gt;DIR&amp;lt;/code&amp;gt;, and the name of the entry as &amp;lt;code&amp;gt;pathname&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Use &amp;lt;code&amp;gt;AT_SYMLINK_NOFOLLOW&amp;lt;/code&amp;gt;, so that links are listed as links.&lt;br /&gt;
* &amp;lt;code&amp;gt;readlinkat(dirfd(dir), name, buffer, size)&amp;lt;/code&amp;gt; reads the target of a link. It returns the length, and does not add a &amp;lt;code&amp;gt;&amp;#039;\0&amp;#039;&amp;lt;/code&amp;gt;.&lt;br /&gt;
* For the date: &amp;lt;code&amp;gt;time_t when = st.stx_mtime.tv_sec;&amp;lt;/code&amp;gt;, then &amp;lt;code&amp;gt;localtime_r(&amp;amp;when, &amp;amp;tm)&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;strftime(date, sizeof date, &amp;quot;%Y-%m-%d %H:%M&amp;quot;, &amp;amp;tm)&amp;lt;/code&amp;gt;, from &amp;lt;code&amp;gt;&amp;amp;lt;time.h&amp;amp;gt;&amp;lt;/code&amp;gt;.&lt;br /&gt;
* [https://man7.org/linux/man-pages/man3/getpwuid.3.html &amp;lt;code&amp;gt;getpwuid(3)&amp;lt;/code&amp;gt;] and [https://man7.org/linux/man-pages/man3/getgrgid.3.html &amp;lt;code&amp;gt;getgrgid(3)&amp;lt;/code&amp;gt;] use different buffers, but copy the names anyway if you want to look up anything else before printing them.&lt;br /&gt;
&lt;br /&gt;
After step 4, compare your output with the real &amp;lt;code&amp;gt;ls&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== B. Permission doctor ===&lt;br /&gt;
&lt;br /&gt;
Write a command which walks a directory and everything below it, and reports dangerous permissions. With &amp;lt;code&amp;gt;--fix&amp;lt;/code&amp;gt;, it also repairs them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
./build/debug/app [--fix] &amp;lt;directory&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It checks only regular files and directories, including the starting directory, against three rules:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Applies to !! Problem !! Repair&lt;br /&gt;
|-&lt;br /&gt;
| Regular files and directories || Writable by others, except for directories with the sticky bit || Remove the write permission of the others&lt;br /&gt;
|-&lt;br /&gt;
| Regular files whose name ends in &amp;lt;code&amp;gt;.key&amp;lt;/code&amp;gt; || Any permission for the group or the others || Remove all of them&lt;br /&gt;
|-&lt;br /&gt;
| Regular files || Set-user-ID or set-group-ID || Remove those bits&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Create a test directory inside the project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mkdir -p demo/sub demo/locked&lt;br /&gt;
echo hello &amp;gt; demo/notes.txt&lt;br /&gt;
echo hello &amp;gt; demo/shared.txt &amp;amp;&amp;amp; chmod 666 demo/shared.txt&lt;br /&gt;
echo secret &amp;gt; demo/sub/server.key &amp;amp;&amp;amp; chmod 644 demo/sub/server.key&lt;br /&gt;
cp /bin/true demo/sub/tool &amp;amp;&amp;amp; chmod 4755 demo/sub/tool&lt;br /&gt;
chmod 777 demo/sub&lt;br /&gt;
ln -s /etc/shadow demo/link&lt;br /&gt;
chmod 000 demo/locked&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./build/debug/app demo&lt;br /&gt;
demo/sub: writable by others (0777)&lt;br /&gt;
demo/sub/server.key: private key open to group or others (0644)&lt;br /&gt;
demo/sub/tool: runs with the rights of its owner or group (4755)&lt;br /&gt;
demo/locked: Permission denied&lt;br /&gt;
demo/shared.txt: writable by others (0666)&lt;br /&gt;
problems found: 4, fixed: 0&lt;br /&gt;
$ ./build/debug/app --fix demo&lt;br /&gt;
demo/sub: writable by others (0777)&lt;br /&gt;
  fixed: 0777 -&amp;gt; 0775&lt;br /&gt;
demo/sub/server.key: private key open to group or others (0644)&lt;br /&gt;
  fixed: 0644 -&amp;gt; 0600&lt;br /&gt;
demo/sub/tool: runs with the rights of its owner or group (4755)&lt;br /&gt;
  fixed: 4755 -&amp;gt; 0755&lt;br /&gt;
demo/locked: Permission denied&lt;br /&gt;
demo/shared.txt: writable by others (0666)&lt;br /&gt;
  fixed: 0666 -&amp;gt; 0664&lt;br /&gt;
problems found: 4, fixed: 4&lt;br /&gt;
$ ./build/debug/app demo&lt;br /&gt;
demo/locked: Permission denied&lt;br /&gt;
problems found: 0, fixed: 0&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The lines may come in a different order. The exit status is 0 if every problem found was fixed, and 1 otherwise.&lt;br /&gt;
&lt;br /&gt;
* Write a recursive function which takes the path of a directory. Build the path of each entry with [https://man7.org/linux/man-pages/man3/snprintf.3.html &amp;lt;code&amp;gt;snprintf(3)&amp;lt;/code&amp;gt;], into a buffer of &amp;lt;code&amp;gt;PATH_MAX&amp;lt;/code&amp;gt; bytes (from &amp;lt;code&amp;gt;&amp;amp;lt;limits.h&amp;amp;gt;&amp;lt;/code&amp;gt;).&lt;br /&gt;
* Skip &amp;lt;code&amp;gt;.&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;..&amp;lt;/code&amp;gt;, or the walk will never end.&lt;br /&gt;
* Use &amp;lt;code&amp;gt;AT_SYMLINK_NOFOLLOW&amp;lt;/code&amp;gt;, and leave symbolic links alone.&lt;br /&gt;
* Set &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; to 0 before &amp;#039;&amp;#039;&amp;#039;each&amp;#039;&amp;#039;&amp;#039; call to [https://man7.org/linux/man-pages/man3/readdir.3.html &amp;lt;code&amp;gt;readdir(3)&amp;lt;/code&amp;gt;]. The recursive calls change it.&lt;br /&gt;
* A file can break several rules. Work out the new mode from all of them, and call [https://man7.org/linux/man-pages/man2/chmod.2.html &amp;lt;code&amp;gt;chmod(2)&amp;lt;/code&amp;gt;] once.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039; The set-user-ID bit on &amp;lt;code&amp;gt;demo/sub/tool&amp;lt;/code&amp;gt; is harmless here, because that program is a copy of &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;. Never set it on a shell or on anything which runs commands: whoever starts the program would get your rights.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some things to look at:&lt;br /&gt;
&lt;br /&gt;
* Run &amp;lt;code&amp;gt;./build/debug/app --fix /usr/bin&amp;lt;/code&amp;gt;. Why does it fail for every file?&lt;br /&gt;
* Why is &amp;lt;code&amp;gt;/tmp&amp;lt;/code&amp;gt; not reported, even though everybody can write to it?&lt;br /&gt;
* &amp;lt;code&amp;gt;demo/link&amp;lt;/code&amp;gt; has mode 0777. What would &amp;lt;code&amp;gt;--fix&amp;lt;/code&amp;gt; try to do if the program treated it like a regular file? Why would that fail here, and what would happen if the link pointed to a file of yours?&lt;/div&gt;</description>
			<pubDate>Fri, 13 Nov 2026 14:14:22 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie:OS_Lab_6_-_Users._Permissions</comments>
		</item>
		<item>
			<title>Operating Systems</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Operating_Systems&amp;diff=8447&amp;oldid=8436</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Operating_Systems&amp;diff=8447&amp;oldid=8436</guid>
			<description>&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Lab Sessions&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left diff-editfont-monospace&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ro&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Versiunea anterioară&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Versiunea de la data 13 noiembrie 2026 14:13&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l6&quot; &gt;Linia 6:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Linia 6:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab 4 - [[OS Lab 4 - Processes. Signals]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab 4 - [[OS Lab 4 - Processes. Signals]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab 5 - [[OS Lab 5 - Threads. Mutexes]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab 5 - [[OS Lab 5 - Threads. Mutexes]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* Lab 6 - [[OS Lab 6 - Users. Permissions]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=== Old Lab Sessions (2025) ===&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=== Old Lab Sessions (2025) ===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</description>
			<pubDate>Fri, 13 Nov 2026 14:13:54 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie:Operating_Systems</comments>
		</item>
		<item>
			<title>Arhitectura Sistemelor de Calcul (curs)</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Arhitectura_Sistemelor_de_Calcul_(curs)&amp;diff=8446&amp;oldid=8430</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Arhitectura_Sistemelor_de_Calcul_(curs)&amp;diff=8446&amp;oldid=8430</guid>
			<description>&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left diff-editfont-monospace&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ro&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Versiunea anterioară&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Versiunea de la data 9 noiembrie 2026 20:27&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l40&quot; &gt;Linia 40:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Linia 40:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://www.youtube.com/watch?v=MUWAbpg1xLo CPU vs GPU vs TPU]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://www.youtube.com/watch?v=MUWAbpg1xLo CPU vs GPU vs TPU]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://www.youtube.com/watch?v=KVOc6369-Lo Flynn&amp;#039;s Taxonomy: SISD, SIMD, MISD &amp;amp; MIMD]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://www.youtube.com/watch?v=KVOc6369-Lo Flynn&amp;#039;s Taxonomy: SISD, SIMD, MISD &amp;amp; MIMD]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* [https://www.youtube.com/watch?v=SWec3n9Jzh8 SISD, SIMD, MISD, MIMD Explained: Flynn&amp;#039;s Taxonomy &amp;amp; Shared vs Distributed Memory]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://www.youtube.com/watch?v=PgDaJEjlBuI Multithreading vs Multiprocessing]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://www.youtube.com/watch?v=PgDaJEjlBuI Multithreading vs Multiprocessing]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://www.youtube.com/watch?v=0ttx0AuNezo Superscalar vs Superpipelined]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://www.youtube.com/watch?v=0ttx0AuNezo Superscalar vs Superpipelined]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://www.youtube.com/watch?v=6tbNew87fZU All CPU Architectures from von Neumann to Hybrid Multicores]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [https://www.youtube.com/watch?v=6tbNew87fZU All CPU Architectures from von Neumann to Hybrid Multicores]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</description>
			<pubDate>Mon, 09 Nov 2026 20:27:08 GMT</pubDate>
			<dc:creator>Zhascsi</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie:Arhitectura_Sistemelor_de_Calcul_(curs)</comments>
		</item>
		<item>
			<title>Fișier:CID2 Platforma APB II.pdf</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:CID2_Platforma_APB_II.pdf&amp;diff=8445&amp;oldid=0</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:CID2_Platforma_APB_II.pdf&amp;diff=8445&amp;oldid=0</guid>
			<description>&lt;p&gt;&lt;a href=&quot;/index.php?title=Utilizator:Gvpopescu&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new mw-userlink&quot; title=&quot;Utilizator:Gvpopescu (pagină inexistentă)&quot;&gt;&lt;bdi&gt;Gvpopescu&lt;/bdi&gt;&lt;/a&gt; a încărcat &lt;a href=&quot;/index.php/Fi%C8%99ier:CID2_Platforma_APB_II.pdf&quot; title=&quot;Fișier:CID2 Platforma APB II.pdf&quot;&gt;Fișier:CID2 Platforma APB II.pdf&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pagină nouă&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;/div&gt;</description>
			<pubDate>Mon, 09 Nov 2026 09:33:33 GMT</pubDate>
			<dc:creator>Gvpopescu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie_Fi%C8%99ier:CID2_Platforma_APB_II.pdf</comments>
		</item>
		<item>
			<title>Circuite Integrate Digitale 2</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Circuite_Integrate_Digitale_2&amp;diff=8444&amp;oldid=8409</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Circuite_Integrate_Digitale_2&amp;diff=8444&amp;oldid=8409</guid>
			<description>&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Platforme laborator&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left diff-editfont-monospace&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ro&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Versiunea anterioară&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Versiunea de la data 9 noiembrie 2026 09:33&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;diff-multi&quot; lang=&quot;ro&quot;&gt;(Nu s-a afișat o versiune intermediară efectuată de același utilizator)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot; &gt;Linia 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Linia 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Platforme laborator ==&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Platforme laborator ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [[Media:CID2_lab_1_2.pdf|&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;Platformă lab &lt;/del&gt;1+2]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* [[Media:CID2_lab_1_2.pdf|&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;Lucrarea &lt;/ins&gt;1 + 2&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;* [[Media:CID2_Platforma_APB_I.pdf|Lucrarea 3]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;* [[Media:CID2_Platforma_APB_II.pdf|Lucrarea 4&lt;/ins&gt;]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</description>
			<pubDate>Mon, 09 Nov 2026 09:33:18 GMT</pubDate>
			<dc:creator>Gvpopescu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie:Circuite_Integrate_Digitale_2</comments>
		</item>
		<item>
			<title>Fișier:CID2 Platforma APB I.pdf</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:CID2_Platforma_APB_I.pdf&amp;diff=8442&amp;oldid=0</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:CID2_Platforma_APB_I.pdf&amp;diff=8442&amp;oldid=0</guid>
			<description>&lt;p&gt;&lt;a href=&quot;/index.php?title=Utilizator:Gvpopescu&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new mw-userlink&quot; title=&quot;Utilizator:Gvpopescu (pagină inexistentă)&quot;&gt;&lt;bdi&gt;Gvpopescu&lt;/bdi&gt;&lt;/a&gt; a încărcat &lt;a href=&quot;/index.php/Fi%C8%99ier:CID2_Platforma_APB_I.pdf&quot; title=&quot;Fișier:CID2 Platforma APB I.pdf&quot;&gt;Fișier:CID2 Platforma APB I.pdf&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pagină nouă&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;/div&gt;</description>
			<pubDate>Mon, 09 Nov 2026 09:31:03 GMT</pubDate>
			<dc:creator>Gvpopescu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie_Fi%C8%99ier:CID2_Platforma_APB_I.pdf</comments>
		</item>
		<item>
			<title>Fișier:OS Lab 5 - deadlock.png</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:OS_Lab_5_-_deadlock.png&amp;diff=8441&amp;oldid=0</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:OS_Lab_5_-_deadlock.png&amp;diff=8441&amp;oldid=0</guid>
			<description>&lt;p&gt;&lt;a href=&quot;/index.php?title=Utilizator:Vserbu&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new mw-userlink&quot; title=&quot;Utilizator:Vserbu (pagină inexistentă)&quot;&gt;&lt;bdi&gt;Vserbu&lt;/bdi&gt;&lt;/a&gt; a încărcat &lt;a href=&quot;/index.php/Fi%C8%99ier:OS_Lab_5_-_deadlock.png&quot; title=&quot;Fișier:OS Lab 5 - deadlock.png&quot;&gt;Fișier:OS Lab 5 - deadlock.png&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pagină nouă&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;/div&gt;</description>
			<pubDate>Fri, 06 Nov 2026 14:15:12 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie_Fi%C8%99ier:OS_Lab_5_-_deadlock.png</comments>
		</item>
		<item>
			<title>Fișier:OS Lab 5 - futex.png</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:OS_Lab_5_-_futex.png&amp;diff=8440&amp;oldid=0</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:OS_Lab_5_-_futex.png&amp;diff=8440&amp;oldid=0</guid>
			<description>&lt;p&gt;&lt;a href=&quot;/index.php?title=Utilizator:Vserbu&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new mw-userlink&quot; title=&quot;Utilizator:Vserbu (pagină inexistentă)&quot;&gt;&lt;bdi&gt;Vserbu&lt;/bdi&gt;&lt;/a&gt; a încărcat &lt;a href=&quot;/index.php/Fi%C8%99ier:OS_Lab_5_-_futex.png&quot; title=&quot;Fișier:OS Lab 5 - futex.png&quot;&gt;Fișier:OS Lab 5 - futex.png&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pagină nouă&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;/div&gt;</description>
			<pubDate>Fri, 06 Nov 2026 14:14:29 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie_Fi%C8%99ier:OS_Lab_5_-_futex.png</comments>
		</item>
		<item>
			<title>Fișier:OS Lab 5 - lost update.png</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:OS_Lab_5_-_lost_update.png&amp;diff=8439&amp;oldid=0</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:OS_Lab_5_-_lost_update.png&amp;diff=8439&amp;oldid=0</guid>
			<description>&lt;p&gt;&lt;a href=&quot;/index.php?title=Utilizator:Vserbu&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new mw-userlink&quot; title=&quot;Utilizator:Vserbu (pagină inexistentă)&quot;&gt;&lt;bdi&gt;Vserbu&lt;/bdi&gt;&lt;/a&gt; a încărcat &lt;a href=&quot;/index.php/Fi%C8%99ier:OS_Lab_5_-_lost_update.png&quot; title=&quot;Fișier:OS Lab 5 - lost update.png&quot;&gt;Fișier:OS Lab 5 - lost update.png&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pagină nouă&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;/div&gt;</description>
			<pubDate>Fri, 06 Nov 2026 14:13:58 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie_Fi%C8%99ier:OS_Lab_5_-_lost_update.png</comments>
		</item>
		<item>
			<title>Fișier:OS Lab 5 - threads.png</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:OS_Lab_5_-_threads.png&amp;diff=8438&amp;oldid=0</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Fi%C8%99ier:OS_Lab_5_-_threads.png&amp;diff=8438&amp;oldid=0</guid>
			<description>&lt;p&gt;&lt;a href=&quot;/index.php?title=Utilizator:Vserbu&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new mw-userlink&quot; title=&quot;Utilizator:Vserbu (pagină inexistentă)&quot;&gt;&lt;bdi&gt;Vserbu&lt;/bdi&gt;&lt;/a&gt; a încărcat &lt;a href=&quot;/index.php/Fi%C8%99ier:OS_Lab_5_-_threads.png&quot; title=&quot;Fișier:OS Lab 5 - threads.png&quot;&gt;Fișier:OS Lab 5 - threads.png&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pagină nouă&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;/div&gt;</description>
			<pubDate>Fri, 06 Nov 2026 14:07:56 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie_Fi%C8%99ier:OS_Lab_5_-_threads.png</comments>
		</item>
		<item>
			<title>OS Lab 5 - Threads. Mutexes</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=OS_Lab_5_-_Threads._Mutexes&amp;diff=8437&amp;oldid=0</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=OS_Lab_5_-_Threads._Mutexes&amp;diff=8437&amp;oldid=0</guid>
			<description>&lt;p&gt;Pagină nouă: In &lt;a href=&quot;/index.php/OS_Lab_4_-_Processes._Signals&quot; title=&quot;OS Lab 4 - Processes. Signals&quot;&gt;the last lab&lt;/a&gt;, we created new processes with [https://man7.org/linux/man-pages/man2/fork.2.html &amp;lt;code&amp;gt;fork(2)&amp;lt;/code&amp;gt;], and each of them had a si...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pagină nouă&lt;/b&gt;&lt;/p&gt;&lt;div&gt;In [[OS_Lab_4_-_Processes._Signals|the last lab]], we created new processes with [https://man7.org/linux/man-pages/man2/fork.2.html &amp;lt;code&amp;gt;fork(2)&amp;lt;/code&amp;gt;], and each of them had a single task. In this lab, we will give one process several tasks, its &amp;#039;&amp;#039;&amp;#039;threads&amp;#039;&amp;#039;&amp;#039;, which share its memory. We will create them with [https://man7.org/linux/man-pages/man3/pthread_create.3.html &amp;lt;code&amp;gt;pthread_create(3)&amp;lt;/code&amp;gt;], wait for them with [https://man7.org/linux/man-pages/man3/pthread_join.3.html &amp;lt;code&amp;gt;pthread_join(3)&amp;lt;/code&amp;gt;], and protect the data they share with &amp;#039;&amp;#039;&amp;#039;mutexes&amp;#039;&amp;#039;&amp;#039;. We will also see what these functions ask of the kernel: [https://man7.org/linux/man-pages/man2/clone.2.html &amp;lt;code&amp;gt;clone(2)&amp;lt;/code&amp;gt;] and [https://man7.org/linux/man-pages/man2/futex.2.html &amp;lt;code&amp;gt;futex(2)&amp;lt;/code&amp;gt;]. As an application, we will simulate a small bank.&lt;br /&gt;
&lt;br /&gt;
== Learning objectives ==&lt;br /&gt;
&lt;br /&gt;
By the end of this lab you should be able to:&lt;br /&gt;
&lt;br /&gt;
# create threads with [https://man7.org/linux/man-pages/man3/pthread_create.3.html &amp;lt;code&amp;gt;pthread_create(3)&amp;lt;/code&amp;gt;], and wait for them with [https://man7.org/linux/man-pages/man3/pthread_join.3.html &amp;lt;code&amp;gt;pthread_join(3)&amp;lt;/code&amp;gt;];&lt;br /&gt;
# explain which [https://man7.org/linux/man-pages/man2/clone.2.html &amp;lt;code&amp;gt;clone(2)&amp;lt;/code&amp;gt;] flags make a new task a thread of the same process;&lt;br /&gt;
# recognize a &amp;#039;&amp;#039;&amp;#039;race&amp;#039;&amp;#039;&amp;#039; on shared data, and fix it with a mutex;&lt;br /&gt;
# explain how a mutex uses [https://man7.org/linux/man-pages/man2/futex.2.html &amp;lt;code&amp;gt;futex(2)&amp;lt;/code&amp;gt;], and why an uncontended lock needs no system call;&lt;br /&gt;
# recognize a &amp;#039;&amp;#039;&amp;#039;deadlock&amp;#039;&amp;#039;&amp;#039;, and avoid it by always locking in the same order.&lt;br /&gt;
&lt;br /&gt;
== Threads ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;&amp;#039;thread&amp;#039;&amp;#039;&amp;#039; is a task which shares memory with other tasks in a task group (process). All the threads of a process run the same program and see the same global variables and heap, but each has its own registers and its own stack, so each can be at a different point of the program, in a different function.&lt;br /&gt;
&lt;br /&gt;
On Linux, threads are created and managed with the &amp;#039;&amp;#039;POSIX threads&amp;#039;&amp;#039; (&amp;#039;&amp;#039;pthreads&amp;#039;&amp;#039;) functions, which are part of &amp;#039;&amp;#039;glibc&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== pthread_create(3) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// In: &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,&lt;br /&gt;
                   void *(*start_routine)(void *), void *arg);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Parameter !! Description&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;thread&amp;lt;/code&amp;gt; || Where to store the ID of the new thread, used later to wait for it&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;attr&amp;lt;/code&amp;gt; || Options, such as the size of the stack, or &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt; for the defaults&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;start_routine&amp;lt;/code&amp;gt; || The function the new thread runs. When it returns, the thread ends&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;arg&amp;lt;/code&amp;gt; || The argument passed to &amp;lt;code&amp;gt;start_routine&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The new thread starts running &amp;lt;code&amp;gt;start_routine(arg)&amp;lt;/code&amp;gt;, while the thread that created it continues after [https://man7.org/linux/man-pages/man3/pthread_create.3.html &amp;lt;code&amp;gt;pthread_create(3)&amp;lt;/code&amp;gt;]. Both &amp;lt;code&amp;gt;arg&amp;lt;/code&amp;gt; and the return value of &amp;lt;code&amp;gt;start_routine&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;void *&amp;lt;/code&amp;gt;, so a thread can be given, and can give back, a pointer to anything.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039; The pthreads functions do &amp;#039;&amp;#039;&amp;#039;not&amp;#039;&amp;#039;&amp;#039; set &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt;. They return 0 on success, or the error number itself (for example, &amp;lt;code&amp;gt;EAGAIN&amp;lt;/code&amp;gt;) on error. The examples below use a &amp;lt;code&amp;gt;print_error()&amp;lt;/code&amp;gt; which takes that number as a parameter.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following program starts three threads, gives each one its own number, and each of them prints its PID and its &amp;#039;&amp;#039;&amp;#039;TID&amp;#039;&amp;#039;&amp;#039; (&amp;#039;&amp;#039;thread ID&amp;#039;&amp;#039;), the ID of the task:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot; line&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define N_THREADS 3&lt;br /&gt;
&lt;br /&gt;
static void print_error(const char *what, int err)&lt;br /&gt;
{&lt;br /&gt;
    char msg[256];&lt;br /&gt;
    snprintf(msg, sizeof msg, &amp;quot;%s: %s\n&amp;quot;, what, strerror(err));&lt;br /&gt;
    write(STDERR_FILENO, msg, strlen(msg));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void *worker(void *arg)&lt;br /&gt;
{&lt;br /&gt;
    int id = *(int *) arg;&lt;br /&gt;
    char msg[256];&lt;br /&gt;
    snprintf(msg, sizeof msg, &amp;quot;thread %d: PID %d, TID %d\n&amp;quot;, id, getpid(), gettid());&lt;br /&gt;
    write(STDOUT_FILENO, msg, strlen(msg));&lt;br /&gt;
    return nullptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
    pthread_t threads[N_THREADS];&lt;br /&gt;
    int ids[N_THREADS];&lt;br /&gt;
&lt;br /&gt;
    for (int i = 0; i &amp;lt; N_THREADS; i++) {&lt;br /&gt;
        ids[i] = i;&lt;br /&gt;
        int err = pthread_create(&amp;amp;threads[i], nullptr, worker, &amp;amp;ids[i]);&lt;br /&gt;
        if (err != 0) {&lt;br /&gt;
            print_error(&amp;quot;pthread_create&amp;quot;, err);&lt;br /&gt;
            return EXIT_FAILURE;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    for (int i = 0; i &amp;lt; N_THREADS; i++) {&lt;br /&gt;
        pthread_join(threads[i], nullptr);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    char msg[256];&lt;br /&gt;
    snprintf(msg, sizeof msg, &amp;quot;main:     PID %d, TID %d\n&amp;quot;, getpid(), gettid());&lt;br /&gt;
    write(STDOUT_FILENO, msg, strlen(msg));&lt;br /&gt;
    return EXIT_SUCCESS;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./build/debug/app&lt;br /&gt;
thread 0: PID 4307, TID 4308&lt;br /&gt;
thread 1: PID 4307, TID 4309&lt;br /&gt;
thread 2: PID 4307, TID 4310&lt;br /&gt;
main:     PID 4307, TID 4307&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the threads have the same PID: they are one process. Each has its own TID, and the first thread of the process, the one which runs &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt;, has a TID equal to the PID. The three lines may come in any order: the threads run independently.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039; Each thread is given a pointer to its own element of &amp;lt;code&amp;gt;ids&amp;lt;/code&amp;gt;. Passing &amp;lt;code&amp;gt;&amp;amp;i&amp;lt;/code&amp;gt; instead would be a bug: all the threads would get a pointer to the same variable, which the loop keeps changing, leading to a race condition on access.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== pthread_join(3) ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// In: &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
int pthread_join(pthread_t thread, void **retval);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://man7.org/linux/man-pages/man3/pthread_join.3.html &amp;lt;code&amp;gt;pthread_join(3)&amp;lt;/code&amp;gt;] waits for a thread to end, the way [https://man7.org/linux/man-pages/man2/waitpid.2.html &amp;lt;code&amp;gt;waitpid(2)&amp;lt;/code&amp;gt;] waits for a child process. If &amp;lt;code&amp;gt;retval&amp;lt;/code&amp;gt; is not &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;, it stores there the value returned by the thread&amp;#039;s function.&lt;br /&gt;
&lt;br /&gt;
A simpler way to get results back is to give each thread a structure, with both its input and a place for its output. The following program adds up the numbers from 0 to 99,999,999 with four threads, each adding a quarter of them:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot; line&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define N_THREADS 4&lt;br /&gt;
#define N 100000000&lt;br /&gt;
&lt;br /&gt;
static void print_error(const char *what, int err)&lt;br /&gt;
{&lt;br /&gt;
    char msg[256];&lt;br /&gt;
    snprintf(msg, sizeof msg, &amp;quot;%s: %s\n&amp;quot;, what, strerror(err));&lt;br /&gt;
    write(STDERR_FILENO, msg, strlen(msg));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// What one thread works on: its input, and a place for its result.&lt;br /&gt;
struct part {&lt;br /&gt;
    long from, to; // Add the numbers in [from, to)&lt;br /&gt;
    long sum;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
static void *add_range(void *arg)&lt;br /&gt;
{&lt;br /&gt;
    struct part *p = arg;&lt;br /&gt;
    p-&amp;gt;sum = 0;&lt;br /&gt;
    for (long i = p-&amp;gt;from; i &amp;lt; p-&amp;gt;to; i++) {&lt;br /&gt;
        p-&amp;gt;sum += i;&lt;br /&gt;
    }&lt;br /&gt;
    return nullptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
    pthread_t threads[N_THREADS];&lt;br /&gt;
    struct part parts[N_THREADS];&lt;br /&gt;
&lt;br /&gt;
    for (int i = 0; i &amp;lt; N_THREADS; i++) {&lt;br /&gt;
        parts[i].from = (long) N / N_THREADS * i;&lt;br /&gt;
        parts[i].to = (long) N / N_THREADS * (i + 1);&lt;br /&gt;
        int err = pthread_create(&amp;amp;threads[i], nullptr, add_range, &amp;amp;parts[i]);&lt;br /&gt;
        if (err != 0) {&lt;br /&gt;
            print_error(&amp;quot;pthread_create&amp;quot;, err);&lt;br /&gt;
            return EXIT_FAILURE;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    long sum = 0;&lt;br /&gt;
    for (int i = 0; i &amp;lt; N_THREADS; i++) {&lt;br /&gt;
        pthread_join(threads[i], nullptr);&lt;br /&gt;
        sum += parts[i].sum;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    char msg[256];&lt;br /&gt;
    snprintf(msg, sizeof msg, &amp;quot;sum of 0..%d: %ld\n&amp;quot;, N - 1, sum);&lt;br /&gt;
    write(STDOUT_FILENO, msg, strlen(msg));&lt;br /&gt;
    return EXIT_SUCCESS;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./build/debug/app&lt;br /&gt;
sum of 0..99999999: 4999999950000000&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each thread writes only to its own &amp;lt;code&amp;gt;struct part&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; reads it only after [https://man7.org/linux/man-pages/man3/pthread_join.3.html &amp;lt;code&amp;gt;pthread_join(3)&amp;lt;/code&amp;gt;], when the thread has ended. The threads never touch the same data at the same time, so nothing more is needed in this case.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039; Returning from &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt;, or calling [https://man7.org/linux/man-pages/man3/exit.3.html &amp;lt;code&amp;gt;exit(3)&amp;lt;/code&amp;gt;] from any thread, ends the &amp;#039;&amp;#039;&amp;#039;whole process&amp;#039;&amp;#039;&amp;#039;, with all its threads (it uses the &amp;lt;code&amp;gt;exit_group&amp;lt;/code&amp;gt; system call). To end only the calling thread, return from its function, or call [https://man7.org/linux/man-pages/man3/pthread_exit.3.html &amp;lt;code&amp;gt;pthread_exit(3)&amp;lt;/code&amp;gt;]. This is why &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; must join the threads before it returns.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Under the hood: clone(2) ===&lt;br /&gt;
&lt;br /&gt;
[https://man7.org/linux/man-pages/man3/pthread_create.3.html &amp;lt;code&amp;gt;pthread_create(3)&amp;lt;/code&amp;gt;] creates the new task with the [https://man7.org/linux/man-pages/man2/clone.2.html &amp;lt;code&amp;gt;clone(2)&amp;lt;/code&amp;gt;] system call, the same one [https://man7.org/linux/man-pages/man2/fork.2.html &amp;lt;code&amp;gt;fork(2)&amp;lt;/code&amp;gt;] uses. The difference is in the flags, which say what the new task shares with the one that created it:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Flag !! The new task shares&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;CLONE_VM&amp;lt;/code&amp;gt; || The memory: the code, the global variables, the heap, and the stacks of the other threads&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;CLONE_FILES&amp;lt;/code&amp;gt; || The file descriptor table: a file opened by one thread can be used by all of them&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;CLONE_SIGHAND&amp;lt;/code&amp;gt; || The signal handlers&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;CLONE_THREAD&amp;lt;/code&amp;gt; || The process: the new task gets a new TID, but the same PID, and is not a child to be waited for with [https://man7.org/linux/man-pages/man2/waitpid.2.html &amp;lt;code&amp;gt;waitpid(2)&amp;lt;/code&amp;gt;]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
[https://man7.org/linux/man-pages/man2/fork.2.html &amp;lt;code&amp;gt;fork(2)&amp;lt;/code&amp;gt;] calls [https://man7.org/linux/man-pages/man2/clone.2.html &amp;lt;code&amp;gt;clone(2)&amp;lt;/code&amp;gt;] with none of these, so the child gets a copy of everything instead. Since the memory is shared, the new thread cannot use the stack of the thread which created it. Before calling [https://man7.org/linux/man-pages/man2/clone.2.html &amp;lt;code&amp;gt;clone(2)&amp;lt;/code&amp;gt;], glibc allocates a new stack (8 MiB by default) with [https://man7.org/linux/man-pages/man2/mmap.2.html &amp;lt;code&amp;gt;mmap(2)&amp;lt;/code&amp;gt;], and gives its address to [https://man7.org/linux/man-pages/man2/clone.2.html &amp;lt;code&amp;gt;clone(2)&amp;lt;/code&amp;gt;].&lt;br /&gt;
&lt;br /&gt;
[[File:OS_Lab_5_-_threads.png|center|thumb|500px|One process with three threads. They share all the memory of the process, but each one has its own registers, and its own stack, which glibc maps with [https://man7.org/linux/man-pages/man2/mmap.2.html &amp;lt;code&amp;gt;mmap(2)&amp;lt;/code&amp;gt;] before calling [https://man7.org/linux/man-pages/man2/clone.2.html &amp;lt;code&amp;gt;clone(2)&amp;lt;/code&amp;gt;].]]&lt;br /&gt;
&lt;br /&gt;
== Races ==&lt;br /&gt;
&lt;br /&gt;
Since threads share their memory, two threads can change the same variable at the same time. The following program starts two threads, and each of them increments a global counter one million times:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot; line&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define N 1000000&lt;br /&gt;
&lt;br /&gt;
static long counter = 0;&lt;br /&gt;
&lt;br /&gt;
static void *increment(void *arg)&lt;br /&gt;
{&lt;br /&gt;
    (void) arg;&lt;br /&gt;
    for (int i = 0; i &amp;lt; N; i++) {&lt;br /&gt;
        counter++;&lt;br /&gt;
    }&lt;br /&gt;
    return nullptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
    pthread_t a, b;&lt;br /&gt;
    pthread_create(&amp;amp;a, nullptr, increment, nullptr);&lt;br /&gt;
    pthread_create(&amp;amp;b, nullptr, increment, nullptr);&lt;br /&gt;
    pthread_join(a, nullptr);&lt;br /&gt;
    pthread_join(b, nullptr);&lt;br /&gt;
&lt;br /&gt;
    char msg[256];&lt;br /&gt;
    snprintf(msg, sizeof msg, &amp;quot;counter = %ld (expected %d)\n&amp;quot;, counter, 2 * N);&lt;br /&gt;
    write(STDOUT_FILENO, msg, strlen(msg));&lt;br /&gt;
    return EXIT_SUCCESS;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./build/debug/app&lt;br /&gt;
counter = 1018661 (expected 2000000)&lt;br /&gt;
$ ./build/debug/app&lt;br /&gt;
counter = 1008721 (expected 2000000)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The result is wrong, and different every time. &amp;lt;code&amp;gt;counter++&amp;lt;/code&amp;gt; looks like a single operation, but the processor does it in three steps: &amp;#039;&amp;#039;&amp;#039;load&amp;#039;&amp;#039;&amp;#039; the value from memory into a register, &amp;#039;&amp;#039;&amp;#039;add&amp;#039;&amp;#039;&amp;#039; 1 to the register, and &amp;#039;&amp;#039;&amp;#039;store&amp;#039;&amp;#039;&amp;#039; the register back to memory. When two threads do these steps at the same time, they can both load the same value, and one of the increments is lost.&lt;br /&gt;
&lt;br /&gt;
[[File:OS_Lab_5_-_lost_update.png|center|thumb|600px|&amp;lt;code&amp;gt;counter++&amp;lt;/code&amp;gt; is three steps. If two threads interleave them, both load 5 and both store 6: one of the two increments is lost.]]&lt;br /&gt;
&lt;br /&gt;
This is a &amp;#039;&amp;#039;&amp;#039;race&amp;#039;&amp;#039;&amp;#039;: the result depends on how the steps of the threads happen to interleave. A part of the program which uses shared data, and must not be run by two threads at the same time, is called a &amp;#039;&amp;#039;&amp;#039;critical section&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Mutexes ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;&amp;#039;mutex&amp;#039;&amp;#039;&amp;#039; (&amp;#039;&amp;#039;mutual exclusion&amp;#039;&amp;#039;) is a lock which only one thread can hold at a time. A thread &amp;#039;&amp;#039;&amp;#039;locks&amp;#039;&amp;#039;&amp;#039; the mutex before the critical section, and &amp;#039;&amp;#039;&amp;#039;unlocks&amp;#039;&amp;#039;&amp;#039; it after. If another thread already holds it, [https://man7.org/linux/man-pages/man3/pthread_mutex_lock.3.html &amp;lt;code&amp;gt;pthread_mutex_lock(3)&amp;lt;/code&amp;gt;] waits until it is unlocked.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// In: &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
int pthread_mutex_init(pthread_mutex_t *m, const pthread_mutexattr_t *attr);&lt;br /&gt;
int pthread_mutex_lock(pthread_mutex_t *m);&lt;br /&gt;
int pthread_mutex_unlock(pthread_mutex_t *m);&lt;br /&gt;
int pthread_mutex_destroy(pthread_mutex_t *m);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A mutex must be set up with [https://man7.org/linux/man-pages/man3/pthread_mutex_init.3.html &amp;lt;code&amp;gt;pthread_mutex_init(3)&amp;lt;/code&amp;gt;] before it is used, usually before the threads which use it are created. &amp;lt;code&amp;gt;attr&amp;lt;/code&amp;gt; can choose other kinds of mutexes, or be &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt; for a normal one, which starts unlocked. When the mutex is no longer needed, [https://man7.org/linux/man-pages/man3/pthread_mutex_destroy.3.html &amp;lt;code&amp;gt;pthread_mutex_destroy(3)&amp;lt;/code&amp;gt;] frees anything the mutex may hold.&lt;br /&gt;
&lt;br /&gt;
To fix the counter, we add a mutex, and lock it around the increment:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot; line&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;pthread.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define N 1000000&lt;br /&gt;
&lt;br /&gt;
static long counter = 0;&lt;br /&gt;
static pthread_mutex_t lock;&lt;br /&gt;
&lt;br /&gt;
static void *increment(void *arg)&lt;br /&gt;
{&lt;br /&gt;
    (void) arg;&lt;br /&gt;
    for (int i = 0; i &amp;lt; N; i++) {&lt;br /&gt;
        pthread_mutex_lock(&amp;amp;lock);&lt;br /&gt;
        counter++;&lt;br /&gt;
        pthread_mutex_unlock(&amp;amp;lock);&lt;br /&gt;
    }&lt;br /&gt;
    return nullptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
    pthread_mutex_init(&amp;amp;lock, nullptr);&lt;br /&gt;
&lt;br /&gt;
    pthread_t a, b;&lt;br /&gt;
    pthread_create(&amp;amp;a, nullptr, increment, nullptr);&lt;br /&gt;
    pthread_create(&amp;amp;b, nullptr, increment, nullptr);&lt;br /&gt;
    pthread_join(a, nullptr);&lt;br /&gt;
    pthread_join(b, nullptr);&lt;br /&gt;
&lt;br /&gt;
    pthread_mutex_destroy(&amp;amp;lock);&lt;br /&gt;
&lt;br /&gt;
    char msg[256];&lt;br /&gt;
    snprintf(msg, sizeof msg, &amp;quot;counter = %ld (expected %d)\n&amp;quot;, counter, 2 * N);&lt;br /&gt;
    write(STDOUT_FILENO, msg, strlen(msg));&lt;br /&gt;
    return EXIT_SUCCESS;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./build/debug/app&lt;br /&gt;
counter = 2000000 (expected 2000000)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The result is now correct, but locking has a cost: on our machine, the program became as much as 100 times slower. Every increment now also locks and unlocks the mutex, and while one thread holds it, the other has to wait. Critical sections should be kept as short as possible.&lt;br /&gt;
&lt;br /&gt;
=== Under the hood: futex(2) ===&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;code&amp;gt;pthread_mutex_t&amp;lt;/code&amp;gt; is, at its core, just an integer in the memory of the process. glibc uses three values:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Value !! Meaning&lt;br /&gt;
|-&lt;br /&gt;
| 0 || Unlocked&lt;br /&gt;
|-&lt;br /&gt;
| 1 || Locked, and no other thread is waiting&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Locked, and other threads may be waiting&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The processor has &amp;#039;&amp;#039;&amp;#039;atomic&amp;#039;&amp;#039;&amp;#039; instructions, which read and change a value in memory in a single step, so that no other thread can come in between. C provides them in &amp;lt;code&amp;gt;&amp;amp;lt;stdatomic.h&amp;amp;gt;&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// In: &amp;lt;stdatomic.h&amp;gt;. A is an atomic type, such as _Atomic uint32_t, and C the plain one, uint32_t.&lt;br /&gt;
// If *obj == *expected, sets *obj to desired and returns true.&lt;br /&gt;
// Otherwise, copies *obj into *expected and returns false.&lt;br /&gt;
bool atomic_compare_exchange_strong(volatile A *obj, C *expected, C desired);&lt;br /&gt;
// Sets *obj to desired, and returns its old value.&lt;br /&gt;
C atomic_exchange(volatile A *obj, C desired);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Locking a free mutex is a single atomic instruction, which changes it from 0 to 1. This is the &amp;#039;&amp;#039;&amp;#039;fast path&amp;#039;&amp;#039;&amp;#039;, and it needs &amp;#039;&amp;#039;&amp;#039;no system call&amp;#039;&amp;#039;&amp;#039;. The kernel is only needed when a thread has to wait, since only the kernel can put a thread to sleep and wake it up. For this, Linux provides the [https://man7.org/linux/man-pages/man2/futex.2.html &amp;lt;code&amp;gt;futex(2)&amp;lt;/code&amp;gt;] (&amp;#039;&amp;#039;fast user-space mutex&amp;#039;&amp;#039;) system call:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// In: &amp;lt;linux/futex.h&amp;gt; and &amp;lt;sys/syscall.h&amp;gt;. glibc has no wrapper for it.&lt;br /&gt;
long syscall(SYS_futex, uint32_t *addr, int op, uint32_t val, ...);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; !! What the kernel does&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;FUTEX_WAIT&amp;lt;/code&amp;gt; || If &amp;lt;code&amp;gt;*addr&amp;lt;/code&amp;gt; is still equal to &amp;lt;code&amp;gt;val&amp;lt;/code&amp;gt;, puts the thread to sleep, until another thread wakes it. Otherwise, returns right away&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;FUTEX_WAKE&amp;lt;/code&amp;gt; || Wakes up to &amp;lt;code&amp;gt;val&amp;lt;/code&amp;gt; threads sleeping on &amp;lt;code&amp;gt;addr&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
For the threads of a single process, the &amp;lt;code&amp;gt;FUTEX_WAIT_PRIVATE&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;FUTEX_WAKE_PRIVATE&amp;lt;/code&amp;gt; versions are a little faster. The kernel does not know anything about mutexes: it only keeps a list of sleeping threads for each address. Using the three values above:&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;lock&amp;#039;&amp;#039;&amp;#039;: try to change the mutex from 0 to 1. If that worked, we hold it. Otherwise, set it to 2, to say that someone is waiting, and call &amp;lt;code&amp;gt;FUTEX_WAIT&amp;lt;/code&amp;gt; with 2. When woken, try again.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;unlock&amp;#039;&amp;#039;&amp;#039;: set the mutex to 0. If it was 2, someone may be waiting, so call &amp;lt;code&amp;gt;FUTEX_WAKE&amp;lt;/code&amp;gt; to wake one of them.&lt;br /&gt;
&lt;br /&gt;
The check done by &amp;lt;code&amp;gt;FUTEX_WAIT&amp;lt;/code&amp;gt; is what makes this work: if the mutex was unlocked between our failed attempt and the system call, the value is no longer 2, and the thread does not go to sleep waiting for a wake-up which already happened.&lt;br /&gt;
&lt;br /&gt;
[[File:OS_Lab_5_-_futex.png|center|thumb|750px|Thread A locks a free mutex in user space, with no system call. Thread B finds it taken, marks it with 2 and sleeps in the kernel. When A unlocks it, it sees the 2, and asks the kernel to wake B, which then takes the mutex.]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;regula&amp;quot;&amp;gt;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039; [https://man7.org/linux/man-pages/man3/pthread_join.3.html &amp;lt;code&amp;gt;pthread_join(3)&amp;lt;/code&amp;gt;] also uses [https://man7.org/linux/man-pages/man2/futex.2.html &amp;lt;code&amp;gt;futex(2)&amp;lt;/code&amp;gt;]. When [https://man7.org/linux/man-pages/man3/pthread_create.3.html &amp;lt;code&amp;gt;pthread_create(3)&amp;lt;/code&amp;gt;] calls [https://man7.org/linux/man-pages/man2/clone.2.html &amp;lt;code&amp;gt;clone(2)&amp;lt;/code&amp;gt;], it asks the kernel (with &amp;lt;code&amp;gt;CLONE_CHILD_CLEARTID&amp;lt;/code&amp;gt;) to set a variable holding the TID of the thread to 0 when the thread ends, and to do a &amp;lt;code&amp;gt;FUTEX_WAKE&amp;lt;/code&amp;gt; on it. [https://man7.org/linux/man-pages/man3/pthread_join.3.html &amp;lt;code&amp;gt;pthread_join(3)&amp;lt;/code&amp;gt;] waits on that variable with &amp;lt;code&amp;gt;FUTEX_WAIT&amp;lt;/code&amp;gt;.&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Deadlock ===&lt;br /&gt;
&lt;br /&gt;
A program may need to hold several mutexes at once. If thread A locks mutex 5 and then waits for mutex 7, while thread B has locked mutex 7 and waits for mutex 5, neither of them can ever continue. This is a &amp;#039;&amp;#039;&amp;#039;deadlock&amp;#039;&amp;#039;&amp;#039;. The program does not crash, it just stops, using no CPU time at all.&lt;br /&gt;
&lt;br /&gt;
[[File:OS_Lab_5_-_deadlock.png|center|thumb|550px|Thread A holds one lock and waits for the other, while thread B holds the other and waits for the first. Each waits for the other, forever.]]&lt;br /&gt;
&lt;br /&gt;
The simplest way to avoid it is to always take the mutexes &amp;#039;&amp;#039;&amp;#039;in the same order&amp;#039;&amp;#039;&amp;#039;, for example in increasing order of their number. Then no thread can hold a mutex while waiting for one that comes before it, so the threads can never wait for each other in a circle.&lt;br /&gt;
&lt;br /&gt;
== Exercises ==&lt;br /&gt;
&lt;br /&gt;
Write the program in a new copy of the project template. As a reminder from [[OS_Lab_1_-_Linux_setup._Bash_primer._C_primer#getting-the-template|the first lab]]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
mkdir -p ~/oslab &amp;amp;&amp;amp; cd ~/oslab&lt;br /&gt;
curl -OL https://wiki.dcae.pub.ro/images/9/93/Os_lab_skeleton.zip&lt;br /&gt;
unzip -d lab05 Os_lab_skeleton.zip&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For this lab:&lt;br /&gt;
&lt;br /&gt;
* As in the last labs, do not use the standard I/O library for output: build the text with [https://man7.org/linux/man-pages/man3/snprintf.3.html &amp;lt;code&amp;gt;snprintf(3)&amp;lt;/code&amp;gt;] and [https://man7.org/linux/man-pages/man2/write.2.html &amp;lt;code&amp;gt;write(2)&amp;lt;/code&amp;gt;] it.&lt;br /&gt;
* Check the value returned by [https://man7.org/linux/man-pages/man3/pthread_create.3.html &amp;lt;code&amp;gt;pthread_create(3)&amp;lt;/code&amp;gt;], and remember that it is an error number, not -1.&lt;br /&gt;
* Use &amp;lt;code&amp;gt;time ./build/debug/app&amp;lt;/code&amp;gt; to measure how long the program runs.&lt;br /&gt;
&lt;br /&gt;
=== Bank transfers ===&lt;br /&gt;
&lt;br /&gt;
A bank has 100 accounts, each starting with 1000. 8 threads, the &amp;#039;&amp;#039;&amp;#039;tellers&amp;#039;&amp;#039;&amp;#039;, each make 1,000,000 random transfers between the accounts. Money only moves from one account to another, so at the end, the total must still be 100,000.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define N_ACCOUNTS 100&lt;br /&gt;
#define N_THREADS 8&lt;br /&gt;
#define N_TRANSFERS 1000000&lt;br /&gt;
#define INITIAL_BALANCE 1000&lt;br /&gt;
&lt;br /&gt;
struct account {&lt;br /&gt;
    pthread_mutex_t lock; // Not used until step 3&lt;br /&gt;
    int balance;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
static struct account accounts[N_ACCOUNTS];&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each transfer picks two &amp;#039;&amp;#039;&amp;#039;different&amp;#039;&amp;#039;&amp;#039; accounts, &amp;lt;code&amp;gt;from&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;to&amp;lt;/code&amp;gt;, and an amount between 1 and 100. If &amp;lt;code&amp;gt;from&amp;lt;/code&amp;gt; has enough money, the amount moves from &amp;lt;code&amp;gt;from&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;to&amp;lt;/code&amp;gt;. Otherwise, nothing happens. Use [https://man7.org/linux/man-pages/man3/rand_r.3.html &amp;lt;code&amp;gt;rand_r(3)&amp;lt;/code&amp;gt;] for the random numbers, with a different seed for each thread: [https://man7.org/linux/man-pages/man3/rand.3.html &amp;lt;code&amp;gt;rand(3)&amp;lt;/code&amp;gt;] keeps its state in a global variable, shared by all the threads.&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;No locks.&amp;#039;&amp;#039;&amp;#039; Write the tellers, and print the total after joining them. Run the program a few times: what happens to the total?&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;One mutex for the whole bank.&amp;#039;&amp;#039;&amp;#039; Lock a single global mutex around each transfer. The total is now correct. How long does the program take? Why is the &amp;lt;code&amp;gt;sys&amp;lt;/code&amp;gt; time so large?&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;One mutex per account.&amp;#039;&amp;#039;&amp;#039; Instead of the global mutex, lock &amp;lt;code&amp;gt;accounts[from].lock&amp;lt;/code&amp;gt;, then &amp;lt;code&amp;gt;accounts[to].lock&amp;lt;/code&amp;gt;, do the transfer, and unlock them. Run the program: it will probably never end (Ctrl+C stops it). Explain why, in a comment.&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Lock ordering.&amp;#039;&amp;#039;&amp;#039; Fix step 3 by always locking the account with the lower number first. The total must be correct, and the program should now be faster than in step 2. Why?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./build/debug/app&lt;br /&gt;
total: 766978 (expected 100000)&lt;br /&gt;
$ ./build/debug/app&lt;br /&gt;
total: 751625 (expected 100000)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ time ./build/debug/app&lt;br /&gt;
total: 100000 (expected 100000)&lt;br /&gt;
&lt;br /&gt;
real	0m0.304s&lt;br /&gt;
user	0m0.427s&lt;br /&gt;
sys	0m0.081s&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first output is from step 1, and the second from step 4. The times depend on the machine, but step 4 should be clearly faster than step 2.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Bonus:&amp;#039;&amp;#039;&amp;#039; Write your own mutex, on top of [https://man7.org/linux/man-pages/man2/futex.2.html &amp;lt;code&amp;gt;futex(2)&amp;lt;/code&amp;gt;], as described in [[#Under the hood: futex(2)|the futex section]]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
typedef _Atomic uint32_t mutex_t; // 0, 1 or 2, as above&lt;br /&gt;
&lt;br /&gt;
void mutex_lock(mutex_t *m);&lt;br /&gt;
void mutex_unlock(mutex_t *m);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use it instead of &amp;lt;code&amp;gt;pthread_mutex_t&amp;lt;/code&amp;gt; in step 4. The total must still be correct, and the time should be close to that of step 4.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Extra:&amp;#039;&amp;#039;&amp;#039; Add an &amp;#039;&amp;#039;&amp;#039;auditor&amp;#039;&amp;#039;&amp;#039; thread, which checks the bank while the tellers work. Every 50 ms ([https://man7.org/linux/man-pages/man2/nanosleep.2.html &amp;lt;code&amp;gt;nanosleep(2)&amp;lt;/code&amp;gt;]), it locks all the accounts, adds up their balances, unlocks them, and prints the total, which must always be 100,000. When the tellers are done, &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; tells the auditor to stop through a global &amp;lt;code&amp;gt;atomic_bool&amp;lt;/code&amp;gt; (from &amp;lt;code&amp;gt;&amp;amp;lt;stdatomic.h&amp;amp;gt;&amp;lt;/code&amp;gt;): a variable which one thread writes while another reads it must be atomic.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
$ ./build/debug/app&lt;br /&gt;
audit: 100000&lt;br /&gt;
audit: 100000&lt;br /&gt;
audit: 100000&lt;br /&gt;
audit: 100000&lt;br /&gt;
audit: 100000&lt;br /&gt;
audit: 100000&lt;br /&gt;
total: 100000 (expected 100000)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* In which order must the auditor lock the accounts?&lt;br /&gt;
* Why must it hold all the locks at once? Try locking, reading and unlocking one account at a time instead: what totals do you get, and why?&lt;/div&gt;</description>
			<pubDate>Fri, 06 Nov 2026 14:04:59 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie:OS_Lab_5_-_Threads._Mutexes</comments>
		</item>
		<item>
			<title>Operating Systems</title>
			<link>http://wiki.dcae.pub.ro/index.php?title=Operating_Systems&amp;diff=8436&amp;oldid=8434</link>
			<guid isPermaLink="false">http://wiki.dcae.pub.ro/index.php?title=Operating_Systems&amp;diff=8436&amp;oldid=8434</guid>
			<description>&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Lab Sessions&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left diff-editfont-monospace&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ro&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Versiunea anterioară&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Versiunea de la data 6 noiembrie 2026 14:04&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l4&quot; &gt;Linia 4:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Linia 4:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab 2 - [[OS Lab 2 - System calls. The initial stack]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab 2 - [[OS Lab 2 - System calls. The initial stack]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab 3 - [[OS Lab 3 - Files. Memory mapping]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab 3 - [[OS Lab 3 - Files. Memory mapping]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;−&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;3 &lt;/del&gt;- [[OS Lab 4 - Processes. Signals]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Lab &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;4 &lt;/ins&gt;- [[OS Lab 4 - Processes. Signals&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;* Lab 5 - [[OS Lab 5 - Threads. Mutexes&lt;/ins&gt;]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=== Old Lab Sessions (2025) ===&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=== Old Lab Sessions (2025) ===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</description>
			<pubDate>Fri, 06 Nov 2026 14:04:44 GMT</pubDate>
			<dc:creator>Vserbu</dc:creator>
			<comments>http://wiki.dcae.pub.ro/index.php/Discu%C8%9Bie:Operating_Systems</comments>
		</item>
</channel></rss>