从一个进程中杀死另一个进程中的特定线程。
下面是我的代码:
ptrace(PTRACE_ATTACH,threadID,NULL,NULL);
停止整个进程,但我只想停止一个特定的线程。
你能给我一些关于如何进行的指导吗?
最佳答案
您可以按照以下步骤操作:
1) 创建一个管道并初始化它。
2) 为特定信号创建信号处理程序,信号处理程序应执行最少的操作。在这种情况下,我会将“signum”写入管道的写入端。
3) 创建一个线程将在轮询时被阻塞,并且它还需要从管道的读取端读取数据。
4) 然后我将忽略上面在其他线程中使用的信号。
5) 然后继续主线程,这里我有一个无限循环。
6) 现在您可以运行程序并使用进程 ID 从控制台发送信号。例如,我们假设进程id是24566,那么我将发送信号如下:
kill -n 16 24566
这只会杀死特定的线程而不是整个程序。
你可以在下面找到我写的程序:
#include <pthread.h>
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/poll.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#define ERR -1
int pipefd[2] = {0, 0};
void signal_handler(int signum)
{
printf("signal handler");
if (ERR == write(pipefd[1], &signum, sizeof(signum)))
printf("errno: %d", errno);
}
void initializePipeAndAddSignalHandler()
{
if (ERR == pipe(pipefd))
{
printf("pipe2 returned an error: %s", strerror(errno));
exit(EXIT_FAILURE);
}
else
{
if (SIG_ERR == signal(SIGUSR1, signal_handler))
{
printf("Enabling signal Handling to SIGUSR1 failed: %s", strerror(errno));
exit(EXIT_FAILURE);
}
}
}
void *threadfunc(void *arg)
{
int sig = -1;
const int NO_FDS = 1;
struct pollfd fds[NO_FDS];
nfds_t nfds = NO_FDS;
const int PIPE_FD = 0;
fds[PIPE_FD].events = POLLIN | POLLHUP;
fds[PIPE_FD].fd = pipefd[0];
printf("in threadfunc");
while(1)
{
int ret = poll(fds, nfds, -1);
printf("Poll called");
if (ret != ERR)
{
if (POLLIN == (fds[PIPE_FD].revents & POLLIN))
{
if (-1 != read(fds[PIPE_FD].fd, &sig, sizeof(sig)))
{
switch (sig)
{
case SIGUSR1:
{
printf("received sigusr1");
return NULL;
}
default:
{
printf("UnKnown Signal received = %d", sig);
break;
}
}
}
}
}
}
}
int main()
{
int sig_return;
sigset_t set;
initializePipeAndAddSignalHandler();
pthread_t tid;
if(pthread_create(&tid, NULL, &threadfunc, NULL))
{
fprintf(stderr, "Error creating thread\n");
return 1;
}
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
sig_return = pthread_sigmask(SIG_BLOCK, &set, NULL);
if (sig_return != 0)
printf("Setting sigmask failed");
while (1)
{
printf("\nmain thread running\n");
sleep (1);
}
printf("killed");
}
关于c - 使用 ptrace 停止或杀死进程中的线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55473933/