Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Задание: обработка сигналов SIGQUIT, SIGINT, SIGTTIN.
- Сборка:
- gcc one.c -o one.out
- gcc two.c -o two.out
- ./two.out
- */
- /// two.c:
- // includes:
- #include <stdlib.h> // exit(code)
- #include <stdio.h> // printf(...), scanf(...)
- #include <unistd.h> // execl()
- #include <signal.h> // sig, kill, ...
- int main(int agrc, char *argv[]) {
- int code, input; // return code, input number
- pid_t pid; // process id of child process
- // display sort of menu:
- printf("What will you do?\n");
- printf("1 - SIGQUIT (end process 'on its own')\n");
- printf("2 - SIGINT (end process)\n");
- printf("3 - SIGTTIN (stop process)\n");
- scanf("%i", &input);
- pid = fork(); // fork process
- switch (pid) { // do depending on result of fork
- case -1: // fork fail
- printf(" ~ Failed to fork");
- exit(1);
- break;
- case 0: // child process:
- if ((execl("one.out", "1", NULL)) == -1) break;
- // Error in execl.
- perror("\n ~ Error in execl()");
- printf("\n ~ Did you try 'gcc one.c -o one.out'?");
- exit(1);
- default: // parent process:
- switch(input) { // send code depending on input
- case 1:
- kill(pid, SIGQUIT);
- break;
- case 2:
- kill(pid, SIGINT);
- break;
- case 3:
- kill(pid, SIGTTIN);
- break;
- }
- wait(&code); // wait until child process finishes
- switch (code) { // display text depending on returned code:
- case SIGQUIT:
- printf("Process ended by SIGQUIT.");
- break;
- case SIGINT:
- printf("Process ended by SIGINT.");
- break;
- default:
- printf("Process ended.");
- }
- printf(" Returned code %d\n", code);
- break;
- }
- }
- /// one.c:
- #include <stdlib.h>
- #include <stdio.h>
- #include <signal.h>
- // procedure to make process end 'by itself':
- void do_exit() {
- exit(1);
- }
- int main(int agrc, char *argv[])
- {
- struct sigaction sa;
- sigset_t signal_set;
- // try to set signal
- if(signal(SIGTTIN, do_exit) < 0) {
- perror("Error setting signal");
- }
- for(;;) { // wait until stopped by host.
- sleep(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement