Use the code below to answer the questions: - Open a second terminal and send it a signal that invalidates the while() loop condition -What command did you use in the second terminal? Here is the code: #include #include #include #include #include volatile sig_atomic_t got_usr1; void sigusr1_handler(int sig) { (void)sig; // silence unused variable warning got_usr1 = 1; } int main(void) { struct sigaction sa; got_usr1 = 0; sa.sa_handler = sigusr1_handler; sa.sa_flags = 0; sigemptyset(&sa.sa_mask); if (sigaction(SIGUSR1, &sa, NULL) == -1) { perror("sigaction"); exit(1); } while (!got_usr1) { printf("PID %d: working hard...\n", getpid()); sleep(1); } printf("Done in by SIGUSR1!\n"); return 0; }
Use the code below to answer the questions:
- Open a second terminal and send it a signal that invalidates the while() loop condition
-What command did you use in the second terminal?
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
volatile sig_atomic_t got_usr1;
void sigusr1_handler(int sig)
{
(void)sig; // silence unused variable warning
got_usr1 = 1;
}
int main(void)
{
struct sigaction sa;
got_usr1 = 0;
sa.sa_handler = sigusr1_handler;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGUSR1, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}
while (!got_usr1) {
printf("PID %d: working hard...\n", getpid());
sleep(1);
}
printf("Done in by SIGUSR1!\n");
return 0;
}
Step by step
Solved in 3 steps with 1 images