[c++ & Linux] sending Ctrl-c to started script

Pagina: 1
Acties:

Acties:
  • 0 Henk 'm!

  • liquid_ice
  • Registratie: Februari 2001
  • Laatst online: 08-09 14:43
Vanuit een c++ omgeving wil ik hcidump starten en ook weer stoppen.
Nou heb ik 1 functie waarin de call: system("hcidump -w somefile"); staat.

Maar nu wil ik ook de stop functie maken.

het hcidump commando stop ik normaal gesproken met Ctrl-c, welke een SIGINT signal stuurt.

Maar hoe kan ik het SIGINT signal sturen naar het door mij net gestarte hcidump?

Klus page: http://klusthuis.blogspot.com


Acties:
  • 0 Henk 'm!

  • Woy
  • Registratie: April 2000
  • Niet online

Woy

Moderator Devschuur®
Als ik in google ( [google=send sigint signal linux] ) zoek, en bij de eerste link kijk lossen ze het gewoon op door (char)3 naar de stdin van het process te sturen. ( Al is dat python zou het voor een extern process natuurlijk niet uit moeten maken ).

Maar wat heb je zelf allemaal al geprobeerd?

“Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.”


Acties:
  • 0 Henk 'm!

  • liquid_ice
  • Registratie: Februari 2001
  • Laatst online: 08-09 14:43
Leuk en aardig dat je me de link van google ff geeft, die zoekmachine kende ik nog niet :S

Maar ff alle gekheid op een stokkie, dat SIGINT (char)3 was wist ik zo niet.
Maar dat ik de SIGINT bij dat proces wilde laten uitkomen was me al duidelijk.

Met signal of kill zou ik een signal moeten kunnen sturen volgens mij, maar hoe vind ik dan uit waar dat het signal moet landen?
en hoe geef ik dat op.

Waarschijnlijk is het een hele simpele vraag voor als je veel ervaring hebt met linux, maar daar heb ik nog niet zoveel ervaring in.

en wat ik al geprobeerd hebt??
dit al allemaal redelijk nieuw, zit al heel de tijd te google en vanalles uit te proberen...

Klus page: http://klusthuis.blogspot.com


Acties:
  • 0 Henk 'm!

  • Nvidiot
  • Registratie: Mei 2003
  • Laatst online: 03-06 16:38

Nvidiot

notepad!

Je moet dat signal sturen naar het hcidump process, daarvoor heb je dus het PID nodig van dit proces. Wellicht dat hcidump dit ergens in een bestandje opslaat?

What a caterpillar calls the end, the rest of the world calls a butterfly. (Lao-Tze)


Acties:
  • 0 Henk 'm!

  • Woy
  • Registratie: April 2000
  • Niet online

Woy

Moderator Devschuur®
liquid_ice schreef op vrijdag 08 mei 2009 @ 09:25:
Leuk en aardig dat je me de link van google ff geeft, die zoekmachine kende ik nog niet :S
Ik had een foutje in de tag gemaakt, dus hij linkte niet goed door.
en wat ik al geprobeerd hebt??
dit al allemaal redelijk nieuw, zit al heel de tijd te google en vanalles uit te proberen...
Als ik in google of system function zoek, kom ik hier terecht. http://www.opengroup.org/...399/functions/system.html

Hier sturen ze ook een SIGINT naar een nieuw gestart process.

“Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.”


Acties:
  • 0 Henk 'm!

  • Zoijar
  • Registratie: September 2001
  • Niet online

Zoijar

Because he doesn't row...

Je kan ook gewoon je process forken met fork() en dan in het child process execve() aanroepen.

zo profile ik bv:

C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
   if ((child_pid = fork()) != 0) {
      /* Wait for the profiled process to start.
       * We have to do this to make sure the childs execv has completed
       */
      waitpid(child_pid, &status, 0);

      /* If the childs execv failed, we must exit too */
      if (WIFEXITED(status)) {
         fprintf(stderr, "%s: Failed to execute program '%s'\n", argv[0], cmd_line.program);
         exit(1);
      }

      /* Start profiling the child process */
      if (profile(child_pid, cmd_line.start_addr, cmd_line.end_addr) < 0) {
         /* On error kill the child before it ever runs */
         ptrace(T_EXIT, child_pid, 0, 0);
         fprintf(stderr, "%s: profile() failed. (%d)\n", argv[0], errno);
         exit(1);
      }

      /* Resume/start child until it terminates */
      do {
         ptrace(T_RESUME, child_pid, 0, 0);
         waitpid(child_pid, &status, 0);
      } while (!(WIFEXITED(status) || WIFSIGNALED(status)));

      /* Stop profiling */
      profile(0, 0, 0);

      /* Get profiling data, and display it */
      if (getprof(&profdata) < 0) {
         fprintf(stderr, "%s: getprof() failed. (%d)\n", argv[0], errno);
         exit(1);
      }

      display_profiling_data(&profdata, &cmd_line);

   } else {
      /* Inform the mm this process wants to be traced by its parent */
      ptrace(T_OK, 0, 0, 0);


      /* !!! Execute the program to be profiled !!! */
      execve(cmd_line.program, cmd_line.arguments, envp);

      /* If execv returns there must have been an error, so exit */
      exit(1);


Maar je kan het ook simpeler doen en gewoon het PID opslaan van de fork, en die later killen met een signal.

Dus...
C:
1
2
3
4
5
6
7
8
9
pid_t pid = 0;
if ((pid = fork()) == 0) {
    execl("hcidump", "hcidump", "-w", "somefile", (char*)0);
    exit(1); // on error
}
// continue running here
...
// kill it
kill(pid, SIGEXIT); // SIGKILL

[ Voor 8% gewijzigd door Zoijar op 08-05-2009 10:58 ]


Acties:
  • 0 Henk 'm!

  • liquid_ice
  • Registratie: Februari 2001
  • Laatst online: 08-09 14:43
@Zoijar, Als ik het goed snap dan kan je in dat child process dat je met Fork start de system call doen om hcidump te starten en als je dan het child proces killed, dat hcidump dan automagisch ook word gekilled?

Klus page: http://klusthuis.blogspot.com


Acties:
  • 0 Henk 'm!

  • eamelink
  • Registratie: Juni 2001
  • Niet online

eamelink

Droptikkels

Quick & dirty kan je ook killall gebruiken als er maar één hcidump process is. Dan kan je de naam van de executable gebruiken en zoekt killall zelf het PID op :)

Acties:
  • 0 Henk 'm!

  • Zoijar
  • Registratie: September 2001
  • Niet online

Zoijar

Because he doesn't row...

liquid_ice schreef op vrijdag 08 mei 2009 @ 10:55:
@Zoijar, Als ik het goed snap dan kan je in dat child process dat je met Fork start de system call doen om hcidump te starten en als je dan het child proces killed, dat hcidump dan automagisch ook word gekilled?
Ik heb een duidelijker voorbeeld erbij gezet. Je forked je process eerst, dan heb je 2 identieke processen. Dan in het child process roep je een exec() functie aan, die vervangt het huidige uitvoerende programma in het geheugen compleet. Vandaar dat die call dus ook nooit returned, tenzij er een error is (file not found oid). Je child process is nu het hcidump programma, en daar heb je de PID van na de fork. Die kan je vervolgens makkelijk killen door een SIGKILL te sturen met kill() (kill -9 hcidump zeg maar).
Pagina: 1