<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="et">
	<id>http://courses.cs.taltech.ee/w/index.php?action=history&amp;feed=atom&amp;title=Signals_%28ITS8020%29</id>
	<title>Signals (ITS8020) - Redigeerimiste ajalugu</title>
	<link rel="self" type="application/atom+xml" href="http://courses.cs.taltech.ee/w/index.php?action=history&amp;feed=atom&amp;title=Signals_%28ITS8020%29"/>
	<link rel="alternate" type="text/html" href="http://courses.cs.taltech.ee/w/index.php?title=Signals_(ITS8020)&amp;action=history"/>
	<updated>2026-05-21T21:50:14Z</updated>
	<subtitle>Selle lehekülje redigeerimiste ajalugu</subtitle>
	<generator>MediaWiki 1.35.9</generator>
	<entry>
		<id>http://courses.cs.taltech.ee/w/index.php?title=Signals_(ITS8020)&amp;diff=7312&amp;oldid=prev</id>
		<title>Irve: /* Task */</title>
		<link rel="alternate" type="text/html" href="http://courses.cs.taltech.ee/w/index.php?title=Signals_(ITS8020)&amp;diff=7312&amp;oldid=prev"/>
		<updated>2018-10-05T09:08:00Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Task&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;et&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←Vanem redaktsioon&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Redaktsioon: 5. oktoober 2018, kell 09:08&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-l19&quot; &gt;19. rida:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;19. rida:&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;# Update the program to catch ^C and not to terminate with the signal. (Use CTRL+\ to quit (test whether it works; alternative would be to press ^Z, then kill with the process number that ^Z prints).&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;# Update the program to catch ^C and not to terminate with the signal. (Use CTRL+\ to quit (test whether it works; alternative would be to press ^Z, then kill with the process number that ^Z prints).&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;# Update the program to count ^C keypresses and print the number every time the key is pressed. Add a newline to avoid line-buffering issues.&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;# Update the program to count ^C keypresses and print the number every time the key is pressed. Add a newline to avoid line-buffering issues.&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;# In a naïve interpretation (which I hope), you might end up with a situation in which the printf() from main-loop runs while the handler accesses printf(). This is an error for printf to run while running (this is called &amp;#039;&amp;#039;[http://en.wikipedia.org/wiki/Reentrant_%28subroutine%29 re-entrancy]&amp;#039;&amp;#039;). Sadly it&amp;#039;s not feasible to manifest an error here.&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;# In a naïve interpretation (which I hope), you might end up with a situation in which the printf() from main-loop runs while the handler accesses printf(). This is an error for printf to run while running (this is called &amp;#039;&amp;#039;[http://en.wikipedia.org/wiki/Reentrant_%28subroutine%29 re-entrancy]&amp;#039;&amp;#039;). Sadly&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;, &lt;/ins&gt;it&amp;#039;s not feasible to manifest an error here&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;; I wish we had a better method, but we&amp;#039;ll need to believe in this &amp;#039;&amp;#039;hypothetical&amp;#039;&amp;#039; accident this time..&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;# Fix the error by blocking the signals while printf() runs. Use [http://linux.die.net/man/2/sigprocmask sigprocmask()] function for masking (blocking) signals. You might find [http://linux.die.net/man/3/sigemptyset sigemptyset()] useful. Remember to restore the original signal blocking behaviour afterwards. You are &amp;quot;protecting the critical section&amp;quot; by masking signals. &amp;#039;&amp;#039;Show the result here, or save a copy for next step&amp;#039;&amp;#039;&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;# Fix the error by blocking the signals while printf() runs. Use [http://linux.die.net/man/2/sigprocmask sigprocmask()] function for masking (blocking) signals. You might find [http://linux.die.net/man/3/sigemptyset sigemptyset()] useful. Remember to restore the original signal blocking behaviour afterwards. You are &amp;quot;protecting the critical section&amp;quot; by masking signals. &amp;#039;&amp;#039;Show the result here, or save a copy for next step&amp;#039;&amp;#039;&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;# For real-life programs it is not practical to block signals on every printf() call. Re-write the program so, that &amp;#039;&amp;#039;handler&amp;#039;&amp;#039; sets a global variable which holds 1 when signal has arrived, and 0 when it hasn&amp;#039;t. Print the number on signal arrival, otherwise the dot. Remember to re-set the global variable. Note that there might still be a need to block signals when the signal arrives during the checking itself. Did you set the global variable as &amp;#039;volatile&amp;#039;? &amp;#039;&amp;#039;Show the result&amp;#039;&amp;#039;&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;# For real-life programs it is not practical to block signals on every printf() call. Re-write the program so, that &amp;#039;&amp;#039;handler&amp;#039;&amp;#039; sets a global variable which holds 1 when signal has arrived, and 0 when it hasn&amp;#039;t. Print the number on signal arrival, otherwise the dot. Remember to re-set the global variable. Note that there might still be a need to block signals when the signal arrives during the checking itself. Did you set the global variable as &amp;#039;volatile&amp;#039;? &amp;#039;&amp;#039;Show the result&amp;#039;&amp;#039;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Irve</name></author>
	</entry>
	<entry>
		<id>http://courses.cs.taltech.ee/w/index.php?title=Signals_(ITS8020)&amp;diff=7303&amp;oldid=prev</id>
		<title>Irve: Uus lehekülg: &#039;{{its8020}} We will investigate handling of [http://en.wikipedia.org/wiki/Signal_(computing) signals]. &#039;&#039;Signals&#039;&#039; are similar in effect to hardware interrupts, but can be progra...&#039;</title>
		<link rel="alternate" type="text/html" href="http://courses.cs.taltech.ee/w/index.php?title=Signals_(ITS8020)&amp;diff=7303&amp;oldid=prev"/>
		<updated>2018-10-05T08:44:34Z</updated>

		<summary type="html">&lt;p&gt;Uus lehekülg: &amp;#039;{{its8020}} We will investigate handling of [http://en.wikipedia.org/wiki/Signal_(computing) signals]. &amp;#039;&amp;#039;Signals&amp;#039;&amp;#039; are similar in effect to hardware interrupts, but can be progra...&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Uus lehekülg&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{its8020}}&lt;br /&gt;
We will investigate handling of [http://en.wikipedia.org/wiki/Signal_(computing) signals]. &amp;#039;&amp;#039;Signals&amp;#039;&amp;#039; are similar in effect to hardware interrupts, but can be programmed and controlled without entering supervisor mode. Signals have their associated constants such as SIGINT, which hold the signal number. Every type of signal has its own number, SIGALRM for timer, SIGINT for interruption etc.&lt;br /&gt;
&lt;br /&gt;
Signal handling on POSIX is done using either [http://linux.die.net/man/2/signal signal()] or [http://linux.die.net/man/2/sigaction sicaction()] functions. Both of those register a specific &amp;#039;&amp;#039;handler&amp;#039;&amp;#039; function for the arriving signal.&lt;br /&gt;
&lt;br /&gt;
= Sending signals from keyboard =&lt;br /&gt;
&lt;br /&gt;
You can send a signal to a process with [http://linux.die.net/man/2/kill kill()] system call. For sending a signal from command-line you can use the &amp;#039;kill&amp;#039; utility with associated process number and signal number. While interesting, it is inconvenient for this practice.&lt;br /&gt;
&lt;br /&gt;
You can send some signals to running program using only your keyboard:&lt;br /&gt;
# Ctrl-C sends an INT signal (SIGINT), which terminates the process&lt;br /&gt;
# Ctrl-\ sends a QUIT signal (SIGQUIT), which terminates the process (with [http://en.wikipedia.org/wiki/Core_dump coredump]).&lt;br /&gt;
# Ctrl-Z sends a TSTP signal (SIGTSTP), which makes the program to stop and suspend execution. To get it running again, use &amp;#039;fg&amp;#039; (foreground) and &amp;#039;bg&amp;#039; (background) utilities from command-line.&lt;br /&gt;
# Ctrl-S sends TSTP signal, but does not give you the command line. To start the program again, press Ctrl+Q. &amp;#039;&amp;#039;This often happens when one is used to GUI editors where Ctrl-S is Save command. (And it seems that the command has been deprecated for newer Linux versions)&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
= Task =&lt;br /&gt;
&lt;br /&gt;
# Write a small program with an infinite loop, which prints a dot (.)  for every 1/2 seconds (use [http://linux.die.net/man/3/usleep usleep()] and printf() for educational fun), add a newline to escape [http://www.gnu.org/s/libc/manual/html_node/Buffering-Concepts.html line-buffering]. Check its reaction to CTRL+C&lt;br /&gt;
# Update the program to catch ^C and not to terminate with the signal. (Use CTRL+\ to quit (test whether it works; alternative would be to press ^Z, then kill with the process number that ^Z prints).&lt;br /&gt;
# Update the program to count ^C keypresses and print the number every time the key is pressed. Add a newline to avoid line-buffering issues.&lt;br /&gt;
# In a naïve interpretation (which I hope), you might end up with a situation in which the printf() from main-loop runs while the handler accesses printf(). This is an error for printf to run while running (this is called &amp;#039;&amp;#039;[http://en.wikipedia.org/wiki/Reentrant_%28subroutine%29 re-entrancy]&amp;#039;&amp;#039;). Sadly it&amp;#039;s not feasible to manifest an error here.&lt;br /&gt;
# Fix the error by blocking the signals while printf() runs. Use [http://linux.die.net/man/2/sigprocmask sigprocmask()] function for masking (blocking) signals. You might find [http://linux.die.net/man/3/sigemptyset sigemptyset()] useful. Remember to restore the original signal blocking behaviour afterwards. You are &amp;quot;protecting the critical section&amp;quot; by masking signals. &amp;#039;&amp;#039;Show the result here, or save a copy for next step&amp;#039;&amp;#039;&lt;br /&gt;
# For real-life programs it is not practical to block signals on every printf() call. Re-write the program so, that &amp;#039;&amp;#039;handler&amp;#039;&amp;#039; sets a global variable which holds 1 when signal has arrived, and 0 when it hasn&amp;#039;t. Print the number on signal arrival, otherwise the dot. Remember to re-set the global variable. Note that there might still be a need to block signals when the signal arrives during the checking itself. Did you set the global variable as &amp;#039;volatile&amp;#039;? &amp;#039;&amp;#039;Show the result&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Notes ==&lt;br /&gt;
&lt;br /&gt;
The use of signal() is deprecated in favour of sigaction(), but for our educational purposes the signal() function is slightly better. In real life take the effort to use sigaction().&lt;br /&gt;
&lt;br /&gt;
Note that signals are even more compilcated, it is possible for another signal to arrive while you are handling a signal (and it will pre-empt your handler). This can be prevented by masking some (or all) signals for the duration of the handler. &lt;br /&gt;
&lt;br /&gt;
It is customary to write handlers as short as possible for the exact same reason and check their arrival on a convenient occasion in your program itself.&lt;br /&gt;
&lt;br /&gt;
[[Kategooria:RTOS]]&lt;/div&gt;</summary>
		<author><name>Irve</name></author>
	</entry>
</feed>