Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Ze wzgledow beczeństwa zaleca się , aby w ramach funkcji obs ł uguj ą cej sygnał
- wykonywane by ł y tylko proste czynno ś ci, jak np. ustawienie flagi informuj ą cej
- o otrzymaniu sygna ł u, a skomplikowane czynno ś ci ż eby by ł y wykonywane w osobnym kodzie. Przedstaw schemat takiego rozwi ą zania stosuj ą c proces
- macierzysty i potomny
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <signal.h>
- #include <wait.h>
- #include <stdbool.h>
- volatile sig_atomic_t signal_flag = false;
- void signal_handler(int signum)
- {
- signal_flag = true;
- }
- void some_complicated_function()
- {
- printf("\nSygnal obsluzony...\n");
- exit(EXIT_SUCCESS);
- }
- int main(void)
- {
- pid_t pid;
- if(signal(SIGINT, signal_handler) == SIG_ERR){
- perror("signal");
- return EXIT_FAILURE;
- }
- pid = fork();
- if(pid == -1){
- perror("fork");
- return EXIT_FAILURE;
- }
- else if(pid == 0){
- printf("Nacisnij CTRL + C aby wyslac sygnal\n");
- }
- else{
- wait(NULL);
- while(1){
- if(signal_flag)
- some_complicated_function();
- }
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment