summaryrefslogtreecommitdiff
path: root/application-devel/signals/pres_signals_en.tex
blob: 9111298f5f4a1dff9c19e5ae4dd71b2790290bb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
\input{configpres}

\def\lximg{/usr/share/lx/icons/fueller.png}

\subsection{Linux Signals}

\title{Linux Signals}
\maketitle

% stop displaying 'fueller.png' on the following slides
\def\lximg{none}

\begin{frame}
\tableofcontents
\end{frame}

\subsubsection{Introduction}
\begin{frame}
\frametitle{Signal Introduction}
What is a signal:
\begin{itemize}
\item Used to control processes
\item Easy communication instrument between processes
\item A signal is an asynchronous event
\item "Interrupt at process level"
\end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitle{available signals}
kill -l shows all available signals on the system
\begin{lstlisting}
kill -l
 1) SIGHUP       2) SIGINT      3) SIGQUIT     4) SIGILL      5) SIGTRAP
 6) SIGABRT      7) SIGBUS      8) SIGFPE      9) SIGKILL    10) SIGUSR1
11) SIGSEGV     12) SIGUSR2    13) SIGPIPE    14) SIGALRM    15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD    18) SIGCONT    19) SIGSTOP    20) SIGTSTP
21) SIGTTIN     22) SIGTTOU    23) SIGURG     24) SIGXCPU    25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF    28) SIGWINCH   29) SIGIO      30) SIGPWR
...
\end{lstlisting}

General description:
\begin{lstlisting}
man 7 signal
\end{lstlisting}
\end{frame}

\begin{frame}
\frametitle{process a signal}
If the process receives a signal, the process has to tell the Kernel what to do with it.
Three possibilities are available how a signal can be disposed:
\begin{itemize}
\item ignore it: we do nothing. Most of the signals can be ignored but signals generated by hardware exceptions like divide by zero, if ignored can have weird consequences.
\item we take care: The process has to registers a function. This function is called by kernel when that signal occurs.
\item we call the default disposition: Every signal has a default action. This could be process terminate, ignore etc.
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{special signals}
Two signals can not be caught:
\begin{itemize}
\item SIGKILL
\item SIGSTOP
\end{itemize}
\end{frame}

\subsubsection{Example}
\begin{frame}[fragile]
\frametitle{simple signal example (2)}
Necessary include files:
\begin{lstlisting}
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\frametitle{simple signal example (2)}
Signal handler:
\begin{lstlisting}
void sig_handler(int signo)
{
  if (signo == SIGINT)
    printf("received SIGINT\n");
}
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\frametitle{simple signal example (3)}
main() function of our example:
\begin{lstlisting}
int main(void)
{
  if (signal(SIGINT, sig_handler) == SIG_ERR)
    printf("\ncan't catch SIGINT\n");
  // A long long wait so that we can easily issue a signal to this process
  while(1)
    sleep(1);
  return 0;
}
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]
\frametitle{simple signal example (4)}
run our small example
\begin{lstlisting}
./sigfunc
^Creceived SIGINT
^Creceived SIGINT
^Creceived SIGINT
^Creceived SIGINT
\end{lstlisting}
We tried the key combination ctrl+c several times but each time the process didn't terminate. This is because the signal was handled in the code and this was confirmed from the print we got on each line.
\end{frame}

\begin{frame}
\frametitle{send signals}
Two possibilities for sending a signal:
\begin{itemize}
\item kill() - send a signal to a process
\item raise() - send a signal to the caller
\end{itemize}
\end{frame}

\subsubsection{Pitfalls}
\begin{frame}
\frametitle{signal handler must be re-entrant}
Re-entrant means: The function can be stopped in between due to any reason (like due to interrupt or signal) and then can be reentered again safely before its previous invocations complete the execution.

\bigskip
Reason: Signals are asynchronous events. It could be possible the signal occurs again before the signal handler is still called.
\end{frame}

\begin{frame}
\frametitle{system calls return if a signal occur}
If a signal is send to a process, all system calls return. The complete application must be able to handle the return values and check error codes, if the system call was interrupted:
\begin{itemize}
\item EINTR
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{signal handler should not block}
The signal handler is called by the Kernel. It is not allowed to block or wait for a event in the signal handler.
\end{frame}

\begin{frame}
\frametitle{signals and threads}
pthreads(7) describes that POSIX.1 requires all threads in a process share attributes, including: signal dispositions

\bigskip
This mean: it is not clear from which thread the signal handler is called!
\end{frame}

\subsubsection{Alternative}
\begin{frame}
\frametitle{Signals should not be used}
Use other mechanisms like:
\begin{itemize}
\item files
\item pipes
\item pthreads
\end{itemize}
\end{frame}

\input{tailpres}