how to let my program responds to CTRL+C (linux machine)

This is a discussion on how to let my program responds to CTRL+C (linux machine) within the C++ Programming forums, part of the General Programming Boards category; The second part of the reason is that sigatomic_t is a type guaranteed to be accessible atomically. A 64-bit value ...

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    The second part of the reason is that sigatomic_t is a type guaranteed to be accessible atomically. A 64-bit value on 32-bit x86, for example, is not; it takes two memory reads to get it, and a signal that modifies that value could be raised between the two instructions.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    The second part of the reason is that sigatomic_t is a type guaranteed to be accessible atomically. A 64-bit value on 32-bit x86, for example, is not; it takes two memory reads to get it, and a signal that modifies that value could be raised between the two instructions.
    Sure, that is a good point. But bool, int or char should not be 64-bit on a 32-bit native machine (one would hope).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    char is defined as one byte, right? are there architectures which accesses bytes in a non-atomic manner?

    also what should prevent me from accessing any type I like and take over the guarantee for atomic access myself (eg use a critical section)?

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by pheres View Post
    char is defined as one byte, right? are there architectures which accesses bytes in a non-atomic manner?

    also what should prevent me from accessing any type I like and take over the guarantee for atomic access myself (eg use a critical section)?
    If you "know what you are doing", I'd say you can access any type you want. But with the caveat that SOME architectures may not allow you to do that atomically. sig_atomic_t is there to be guaranteed to be atomic in all aspects on the target system.

    I'm not aware of any machine that do not allow atomic access to bytes, but I do know that mips doesn't have a 16-bit access, only 8 or 32, so 16-bit words DO get accessed non-atomically, because it's a 32-bit read, mask off the unwanted 16 bits into a temp register, modify the actual data in a register and then "assembly is reverse of disassembly" as they say in the Haynes manuals.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Alpha doesn't have atomic byte access. It's only one memory read, obviously, but the full sequence for a byte read is, "align pointer, load quadword, mask and shift to leave only the byte in".
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #21
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Does the guarantee for sig_atomic_t only hold in context of inter process signal handling or always?

  7. #22
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Always? You shouldn't use it for thread synchronization, if that's what you mean.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #23
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    I meant if it is allowed to write a sig_atomic_t typed variable concurrently from user threads without putting a critical section around it?

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by pheres View Post
    I meant if it is allowed to write a sig_atomic_t typed variable concurrently from user threads without putting a critical section around it?
    No. Neither does it "work" to write sig_atomic_t types from multiple threads that all signal simultaneously without some sort of locking mechanism.

    Obviously, if all you are doing is setting the value to a different constant value, then it's not a problem. If you are trying to keep an accurate count of something, or otherwise using it in a way where another thread accessing reading the value in the middle of an update would cause a problem, then you need something else. Many processors have "locked" operations of some sort, which are used for the primitives of IPC operations - these can be used if you need quick updates to variables in a threaded environment.

    The only guarantee with sig_atomic_t is that it's possible to write the whole value as one operation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #25
    Registered User
    Join Date
    Dec 2008
    Posts
    48
    which headfile has sig_atomic_t ?

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Should be in signal.h, I would have thought.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to program on Linux?
    By dweenigma in forum Tech Board
    Replies: 22
    Last Post: 05-14-2007, 05:20 AM
  2. running program with arguments (linux)
    By N8760 in forum C++ Programming
    Replies: 2
    Last Post: 01-28-2002, 01:08 PM
  3. How can I debug a program in linux?
    By zahid in forum C Programming
    Replies: 5
    Last Post: 12-11-2001, 09:34 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21