-1

Okay, so I want to write a c++ program that can end a specific process currently running. I have searched the internet and none of the solutions i have come across make sense to me. is there a simple way to end a process?

Geekman
  • 2,513
  • 3
  • 16
  • 10

2 Answers2

5

On POSIX you call kill(3) to send SIGTERM to the process. On Windows you call TerminateProcess().

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

Assuming that you're on a *nix platform and that you have the process ID (i.e. you spawned the process yourself used some other method to infer its pid), using kill(2) should work:

#include <sys/types.h>
#include <signal.h>

void main() {
    /* ... */
    pid_t pid = ???;
    kill(pid, SIGTERM);
}

It will only work under certain conditions, though:

For a process to have permission to send a signal it must either be privileged (under Linux: have the CAP_KILL capability), or the real or effective user ID of the sending process must equal the real or saved set-user-ID of the target process. In the case of SIGCONT it suffices when the sending and receiving processes belong to the same session.

You
  • 22,800
  • 3
  • 51
  • 64