Linux C's signal and sigaction differences

Source: Internet
Author: User

http://blog.csdn.net/muge0913/article/details/7331129

To process a signal, you need to give the processing function called by the system when this signal occurs. The corresponding processing function can be registered for a specific signal (except for the Sigkill and Sigstop signals). After registering a signal's handler function, when the process receives this signal, no matter what state the process is in, it will stop the current task to execute the signal processing function.

1, register the signal function.

[CPP]View Plaincopyprint?
    1. #include <signal.h>
    2. void (*signal (int signumber,void (*FUNC) (int)) (int)

The signumber represents the signal that the signal processing function corresponds to. Func is a function pointer. This function has an integer parameter and returns a void type. In fact, Func can also take other fixed values such as: SIG_IGN,SIG_DFL.

Sig_ign said: Ignore the signal indicated by Signumber. The SIG_DFL represents a call to the system's default handler function. The return value type of the signal function is the same as the parameter func, which is a pointer to a function that has a null return value with an integer argument. Its correct return value should be the last time the signal was processed. Error returning Sig_err

The signal example is as follows:

[CPP]View Plaincopyprint?
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <stdlib.h>
  4. #include <signal.h>
  5. void func (int sig)
  6. {
  7. printf ("I get asignal!\n");
  8. }
  9. int main ()
  10. {charbuffer[100];
  11. if (Signal (SIGINT, func) = = Sig_err)
  12. {
  13. printf ("Signalerror exit now\n");
  14. Exit (0);
  15. }
  16. printf ("pid:%ld\n", (long) getpid ());
  17. for (;;)
  18. {
  19. Fgets (buffer,sizeof (buffer), stdin);
  20. printf ("bufferis:%s\n", buffer);
  21. }
  22. return 0;
  23. }

Typically, a user process needs to process multiple signals. You can register multiple signal processing functions in one program. A signal can correspond to a processing function, while multiple signals can correspond to a processing function.

For SIGINT signals we can use CTRL + C or CTRL + Z to interrupt the process to execute SIGINT registered functions.

2, advanced signal processing.

A more powerful system call is provided on the Linux system.

[CPP]View Plaincopyprint?
    1. #include <signal.h>
    2. int sigaction (int signumbet,const structsigaction *act,struct sigaction *oldact)

This function, in addition to registering the signal function, provides more detailed information about the exact details of the process receiving the signal.

The struct sigaction is defined as follows: implemented in Linux2.6.39/include/asm-generic/signal.h

[CPP]View Plaincopyprint?
    1. struct sigaction
    2. {
    3. void (*sa_handler) (int);
    4. void (*sa_sigaction) (int,siginfo_t *,void *);
    5. Sigset_tsa_mask;
    6. Intsa_flags;
    7. }

Siginfo_t is implemented in Linux2.6.39/include/asm-generic/siginfo.h:

The value of Sa_flags is the following table, with 0 indicating all default options are selected.

Sa_nocldstop: Used to represent the signal sigchld, which does not produce this signal when the child process is interrupted, and only when the child process has ended.

Sa_nocldwati: When the signal is SIGCHLD, the child process can be avoided zombie.

Sa_nodefer: When the signal processing function is in progress, do not block the signal processing function itself signal function.

Sa_nomask: With Sa_nodefer

Sa_oneshot: When the signal processing function of the user registration is executed once, the processing function of the signal is set to the system default handler function.

Sa_resethand: With Sa_oneshot

Sa_restart: A system call that could not be rerun is automatically rerun.

Sa_siginfo: Indicates that the signal processing function is specified by sa_sigaction and not by Sa_handler, it will display more signal processing function information.

Actually, sinaction can replace the signal function completely.

[CPP]View Plaincopyprint?
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <stdlib.h>
  4. #include <signal.h>
  5. void func (int sig)
  6. {
  7. printf ("I get a signal!\n");
  8. }
  9. int main ()
  10. {char buffer[100];
  11. struct Sigaction Act;
  12. Act.sa_handler=func;
  13. Sigemptyset (&act.sa_mask);
  14. act.sa_flags = 0;
  15. if (Sigaction (sigint,&act, NULL) = =-1)
  16. {
  17. printf ("Sigaction Error exit now\n");
  18. Exit (0);
  19. }
  20. printf ("pid:%ld\n", (long) getpid ());
  21. for (;;)
  22. {
  23. Fgets (buffer,sizeof (buffer), stdin);
  24. printf ("Buffer is:%s\n", buffer);
  25. }
  26. return 0;
  27. }



Linux C's signal and sigaction differences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.