LU4 Signal Flashcards

1
Q

What is a signal in UNIX/Linux systems?

A

a software interrupt.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are signals also referred to as?

A

Software interrupts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Name three examples of events that can trigger signals.

A
  • Floating point error (SIGFTE)
  • power failure (hardware)
  • alarm clock ‘ring’ (software)
  • User action (SIGINT/SIGTSTP)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the purpose of signals in UNIX/Linux?

A

To handle asynchronous events.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does asynchronous mean in the context of signals?

A

The event can occur at any time and may be unrelated to the execution of the process.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What happens when UNIX/Linux recognizes an event?

A

It sends the corresponding process a signal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the three actions a process can take when a signal occurs?

A

[ CID ]

  • CATCH the signal
  • IGNORE the signal,
  • let DEFAULT action apply.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which signals cannot be ignored?

A

SIGKILL and SIGSTOP.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does catching a signal mean?

A

Telling the kernel to call a user-defined function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the default action of a signal?

A

Every signal has a default action which could be:

[TIS]

  • terminating the process
  • ignoring the signal
  • suspending the process.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does SIGFPE represent?

A

Floating point error.

Signal Floating-Point Exception

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does SIGSEGV represent?

A

Segment violation.

a signal sent to a process by the OS when it attempts to access a memory location that it is not allowed to access. This typically occurs due to programming errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What signal is sent when Control-C is pressed?

A

SIGINT
(Signal Interrupt)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What signal is sent when Control-Z is pressed?

A

SIGTSTP
(Signal Terminal Stop)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What signal is generated when a child process dies?

A

SIGCHLD
(Signal Child)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What header file must be included to use signals in C?

A

signal.h

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the signal number for a floating point error?

A

8.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the lifetime of a signal?

A

The interval of generation (creation) to delivery (handling or ignoring). Once delivered, it no longer exists.

Create —> Delivered

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What does it mean if a signal is pending?

A

It has been generated but not yet delivered.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How can you send a signal from the command line?

A

Using the kill command, e.g., kill -9 3423.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What does the signal() system call do?

A

It allows a process to specify the action to take when a particular signal is received.

#include <stdio.h>
#include <signal.h>
#include <unistd.h>

int alarmFlag = 0;

void alarmHandler(int signum) { alarmFlag = 1; }

int main() {
    signal( SIGINT, alarmHandler );
    printf("Looping…\n");

    while( !alarmFlag ) {
        pause();
    }

    printf("Loop ends due to alarm signal \n");
    
		return 0;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is the syntax of the signal() function?

A
void ( *signal(int signo, void (*func)(int)) ) (int);

  • Takes a signal number (signo) and a signal handler function (func).
  • Returns the previous signal handler (which is also a function pointer).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What does SIG_IGN indicate in the signal() function?

A

The specified signal should be ignored and discarded.

meaning the program will not take any action when that signal is received

oldHandler = signal(SIGINT, SIG_IGN);
to ignore the CTRL-C/SIGINT signal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What does SIG_DFL indicate in the signal() function?

A

The kernel should use the default handler.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What is the purpose of the `pause()` system call?
It **suspends** the calling process until a signal is received.
26
What does the `alarm()` system call do?
Instructs the **kernel** to send the `SIGALRM` signal to the *calling process* after a specified number of seconds.
27
What does the `kill(pid_t pid, int sig);` system call do?
Sends a **specified signal** to a process with a *given PID*.
28
What happens if `kill()` is called with `pid = 0`?
The signal is sent to **all** processes in the caller’s process group (processes that have the **same group PID** / same shell session).
29
What happens if `kill()` is called with `pid = -1`?
The signal is sent to **all** processes (with *same UID*) that the sender **has permission** to send to. ## Footnote * If the caller has **root** privileges, the signal is sent to **all** processes ***except*** process 1 (init) and **itself**.
30
What signal is sent when a parent’s child process terminates?
SIGCHLD.
31
What system call is typically used in a `SIGCHLD` handler?
`wait()` to accept the child’s termination code.
32
What is the function of SIGSTOP?
Suspends a process.
33
What is the function of SIGCONT?
Resumes a suspended process.
34
What is the **default** action for `SIGALRM`?
**terminates** the process.
35
What is the signal for urgent data from the network?
SIGURG.
36
What signal is sent when a **broken pipe** occurs?
`SIGPIPE`.
37
What happens if a signal is generated but **no** handler is installed?
The **default** action for that signal is performed. ## Footnote [TISC] * terminate process * ignore signal * stop or continue the process
38
Can any process send a signal to any other process?
Yes, as long as it has permission to do so.
39
What happens when a process receives a signal?
[SER] 1. It **suspends** current flow 2. **executes** the signal handler 3. **resumes** the original flow.
40
What is the **symbolic prefix** for signal names?
SIG.
41
What does the signal number represent?
A **unique identifier** for each possible signal/event.
42
How do you ignore Control-C in a program?
Install SIG_IGN handler for SIGINT. ## Footnote ``` void( *oldHandler ) () // declare var oldHandler = signal(SIGINT, SIG_IGN); // ignore signal(SIGINT, oldHandler); // restore ```
43
How do you restore the old signal handler after ignoring a signal?
Use signal(SIGINT, oldHandler) where oldHandler is the previous handler.
44
What is the effect of pressing `CTRL-D` in a terminal?
Sends `SIGQUIT`, which terminates the process and generates a **core dump**. ## Footnote Core dump: allow a user to save a crash for later or off-site analysis, or comparison with other crashes
45
How can a process protect critical code from being interrupted by Control-C?
By temporarily installing SIG_IGN for SIGINT during the critical section.
46
What signal is associated with a timer expiring?
SIGALRM.
47
How do you schedule an alarm signal in C?
Using alarm(seconds) system call.
48
What is the default action for SIGKILL?
It **forcefully terminates** the process and *cannot be caught or ignored*.
49
What system call is used to suspend and wait for a signal in C?
pause().
50
What happens if an alarm is scheduled while another is pending?
The new alarm overwrites the pending one.
51
What is the return value of alarm() if a previous alarm was set?
The number of seconds **remaining** until the previous alarm was set to trigger. ## Footnote **return 0**: if no previous alarm was set ``` signal(SIGALRM, alarm_handler); unsigned int remaining_time1 = alarm(5); // remaining_time1 = 0 because no previous alarm was set sleep(2); // mimic time unsigned int remaining_time2 = alarm(3); // remaining_time2 = 3 because sleep 2 sec // The first alarm (set for 5 seconds) is now canceled. The second alarm will go off in 3 seconds. sleep(5); print("Done"); ```
52
What does SIGUSR1 represent?
A user-defined signal.
53
What signal is generated when a user types `Ctrl-Y`?
`SIGCONT`.
54
Which signal is sent by the terminal driver when Control-C is pressed?
SIGINT.
55
Which system call installs a custom signal handler in C?
signal().
56
What signal would you send to resume a stopped process?
SIGCONT.
57
How do you send SIGUSR1 to process ID 3423 using the command line?
kill -s USR1 3423.
58
What happens when a signal handler is executed?
1. suspend current flow 2. execute the handler 3. resume the original flow
59
What happens if a process doesn't handle SIGCHLD?
The child process may remain as a zombie unless the signal is ignored.
60
Which signals are related to **job control** in UNIX?
* **SIGSTOP**: Can **NOT** be caught or ignored * **SIGCONT** * **SIGTSTP**: Can be caught or ignored