What is Importance of Volatile Data in Context with Signals

Question: What is Importance of Volatile Data in Context with Signals?

Answer: Let’s first understand what an atomic operation is? An atomic operation is one that CPU executes in single unit! For ex. consider, if a machine were 16-bit, it would take one unit to execute an integer which is 16-bit and would take two units to execute a 32-bit integer. As we know that signal handler sets one of the variables of type ‘volatile sig_atomic_t’ to be truly safely handle a signal for which it’s installed and returns. Then, rest of the program examines periodically it’s value to ensure if that signal occurred. Restricted the signal handler to access atomic operations avoids the possibility of inconsistent results when another signal occurs in the middle of steps that access a non-atomic variable.

Since signals can’t be coordinated in time that is they can occur any time. Therefore, values which are modified by signal handler may change at any time. Because of this we can’t count on these variables having same value from one program statement to next. ‘Volatile’ keyword informs compiler of this fact, which prevents it from optimizing program so that meaning of such variable may change. Let’s consider an example below

    if (atom_var)
        printf("TRUE");
    else
        printf("FALSE");
    if (atom_var)
        printf("TRUE");
    else
        printf("FALSE");

Notice that what happens when above code fragment is executed, ordinary second test would result in same value as the first . Second test would result differently than first if signal handler modifies its value. Unless ‘atom_var’ isn’t declared to be volatile compiler might optimize the above code as below

    if (atom_var) {
        printf("TRUE");
        printf("TRUE");
    }
    else (atom_var) {
        printf("FALSE");
        printf("FALSE");
    }

which results same as the previous code. Remember to declare variable that the signal handler is all to set to be of type “volatile sig_atomic_t”.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

advertisement
advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.