Trap command from C program? [duplicate]












0
















This question already has an answer here:




  • Catch Ctrl-C in C

    9 answers




I'd like to run a trap '' 2 command from a C program to prevent ctrl-c when the a.out is run.



#define TRAP "trap '' 2"

int main()
{
system(TRAP);

...
}


I can get it to work from a .sh file that also runs the program but I'd like everything to be in one .c file.



trap '' 2
cd /Users/me
./a.out


I then tried to make another .c file that runs the script then launch the first a.out as I thought that it was a timing issue the first time without success either...



How can I get it to work within a single a.out or is that even possible?










share|improve this question













marked as duplicate by Antti Haapala c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 29 '18 at 12:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 3





    The trap command is built into the shell itself, and only affects the currently running shell, and no other process. If you want to prevent Ctrl-C from breaking your program (usually a bad idea) then look into signals and SIGBREAK.

    – Some programmer dude
    Dec 29 '18 at 10:49
















0
















This question already has an answer here:




  • Catch Ctrl-C in C

    9 answers




I'd like to run a trap '' 2 command from a C program to prevent ctrl-c when the a.out is run.



#define TRAP "trap '' 2"

int main()
{
system(TRAP);

...
}


I can get it to work from a .sh file that also runs the program but I'd like everything to be in one .c file.



trap '' 2
cd /Users/me
./a.out


I then tried to make another .c file that runs the script then launch the first a.out as I thought that it was a timing issue the first time without success either...



How can I get it to work within a single a.out or is that even possible?










share|improve this question













marked as duplicate by Antti Haapala c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 29 '18 at 12:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 3





    The trap command is built into the shell itself, and only affects the currently running shell, and no other process. If you want to prevent Ctrl-C from breaking your program (usually a bad idea) then look into signals and SIGBREAK.

    – Some programmer dude
    Dec 29 '18 at 10:49














0












0








0









This question already has an answer here:




  • Catch Ctrl-C in C

    9 answers




I'd like to run a trap '' 2 command from a C program to prevent ctrl-c when the a.out is run.



#define TRAP "trap '' 2"

int main()
{
system(TRAP);

...
}


I can get it to work from a .sh file that also runs the program but I'd like everything to be in one .c file.



trap '' 2
cd /Users/me
./a.out


I then tried to make another .c file that runs the script then launch the first a.out as I thought that it was a timing issue the first time without success either...



How can I get it to work within a single a.out or is that even possible?










share|improve this question















This question already has an answer here:




  • Catch Ctrl-C in C

    9 answers




I'd like to run a trap '' 2 command from a C program to prevent ctrl-c when the a.out is run.



#define TRAP "trap '' 2"

int main()
{
system(TRAP);

...
}


I can get it to work from a .sh file that also runs the program but I'd like everything to be in one .c file.



trap '' 2
cd /Users/me
./a.out


I then tried to make another .c file that runs the script then launch the first a.out as I thought that it was a timing issue the first time without success either...



How can I get it to work within a single a.out or is that even possible?





This question already has an answer here:




  • Catch Ctrl-C in C

    9 answers








c shell trap






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 29 '18 at 10:44









Wizzardzz Wizzardzz

353215




353215




marked as duplicate by Antti Haapala c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 29 '18 at 12:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Antti Haapala c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 29 '18 at 12:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 3





    The trap command is built into the shell itself, and only affects the currently running shell, and no other process. If you want to prevent Ctrl-C from breaking your program (usually a bad idea) then look into signals and SIGBREAK.

    – Some programmer dude
    Dec 29 '18 at 10:49














  • 3





    The trap command is built into the shell itself, and only affects the currently running shell, and no other process. If you want to prevent Ctrl-C from breaking your program (usually a bad idea) then look into signals and SIGBREAK.

    – Some programmer dude
    Dec 29 '18 at 10:49








3




3





The trap command is built into the shell itself, and only affects the currently running shell, and no other process. If you want to prevent Ctrl-C from breaking your program (usually a bad idea) then look into signals and SIGBREAK.

– Some programmer dude
Dec 29 '18 at 10:49





The trap command is built into the shell itself, and only affects the currently running shell, and no other process. If you want to prevent Ctrl-C from breaking your program (usually a bad idea) then look into signals and SIGBREAK.

– Some programmer dude
Dec 29 '18 at 10:49












1 Answer
1






active

oldest

votes


















1














trap '' INT ignores SIGINT. Ignore dispositions are inherited to child processes, so:



trap '' 2
cd /Users/me
./a.out


ignores SIGINT for what follows, but it can't work up the process hierarchy.



Fortunately it's not super difficult to ignore SIGINT from C.



#include <signal.h>
int main()
{
//....
signal(SIGINT,SIG_IGN); // `trap '' INT` in C
//^should never fail unless the args are buggy
//...
}





share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    trap '' INT ignores SIGINT. Ignore dispositions are inherited to child processes, so:



    trap '' 2
    cd /Users/me
    ./a.out


    ignores SIGINT for what follows, but it can't work up the process hierarchy.



    Fortunately it's not super difficult to ignore SIGINT from C.



    #include <signal.h>
    int main()
    {
    //....
    signal(SIGINT,SIG_IGN); // `trap '' INT` in C
    //^should never fail unless the args are buggy
    //...
    }





    share|improve this answer




























      1














      trap '' INT ignores SIGINT. Ignore dispositions are inherited to child processes, so:



      trap '' 2
      cd /Users/me
      ./a.out


      ignores SIGINT for what follows, but it can't work up the process hierarchy.



      Fortunately it's not super difficult to ignore SIGINT from C.



      #include <signal.h>
      int main()
      {
      //....
      signal(SIGINT,SIG_IGN); // `trap '' INT` in C
      //^should never fail unless the args are buggy
      //...
      }





      share|improve this answer


























        1












        1








        1







        trap '' INT ignores SIGINT. Ignore dispositions are inherited to child processes, so:



        trap '' 2
        cd /Users/me
        ./a.out


        ignores SIGINT for what follows, but it can't work up the process hierarchy.



        Fortunately it's not super difficult to ignore SIGINT from C.



        #include <signal.h>
        int main()
        {
        //....
        signal(SIGINT,SIG_IGN); // `trap '' INT` in C
        //^should never fail unless the args are buggy
        //...
        }





        share|improve this answer













        trap '' INT ignores SIGINT. Ignore dispositions are inherited to child processes, so:



        trap '' 2
        cd /Users/me
        ./a.out


        ignores SIGINT for what follows, but it can't work up the process hierarchy.



        Fortunately it's not super difficult to ignore SIGINT from C.



        #include <signal.h>
        int main()
        {
        //....
        signal(SIGINT,SIG_IGN); // `trap '' INT` in C
        //^should never fail unless the args are buggy
        //...
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 29 '18 at 10:55









        PSkocikPSkocik

        32.6k64870




        32.6k64870















            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas