1

I have problem with handling signals. I run program in terminal, I am pressed CTRL + C, but don't see "I am pressed CTRL-C" wasn't printed. But I am trying delete row execl("/usr/bin/gedit", "gedit", "test.c", NULL), "I am pressed CTRL-C" was printed.

Can I help you, how to print "I am pressed CTRL-C" and explain why it is. I am appreciated of your help. Thank you very much.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

int loop_forever = 1;

void on_sigint()
{
    printf("\nI am pressed CTRL-C\n");
    loop_forever = 0;
}

int main()
{
    printf("My homework\n");

    execl("/usr/bin/gedit", "gedit", "test.c", NULL);
    signal(SIGINT, on_sigint);
    while (loop_forever)
    {
    }

    exit(1);
}
that other guy
  • 116,971
  • 11
  • 170
  • 194
Hoang
  • 55
  • 1
  • 2
  • 10

1 Answers1

2

execl executes another program in place of yours.

Basically, it transforms your process into a gedit process. Your code will no longer execute, and gedit will run in its place.

To run another program separate from yours, you can use

if(!fork()) {
  execl("/usr/bin/gedit", "gedit", "test.c", NULL);
}

See this question for a detailed description of how fork and exec works in the Unix process model.

Community
  • 1
  • 1
that other guy
  • 116,971
  • 11
  • 170
  • 194