如何通过QNXCpp代码中的进程名称将SIGTERM发送到进程
How to send SIGTERM to a process by process name in QNX cpp code?
E.g:我有一个名为foo的进程。
通常在控制台中,我可以键入slay foo
,然后foo终止。此外,在cpp代码中,我可以使用system("slay foo");
发出系统调用
我知道system((是一个很重的fork调用,应该避免。<csignal>
或<cstdlib>
中还有其他功能我可以选择吗?
我读过SignalKill((和SignalKill_r((,两者都需要pid,但我无法提供。
这并不像人们想象的那么简单。Linux没有提供一个syscall
,它通过进程的名称为您提供进程的PID
。
假设QNX
的文件系统类似于标准UNI,您可能需要阅读本文来了解如何使用进程名称查找进程的PID
,然后将该PID
与SignalKill
或SignalKill_r
一起使用
以下是使用C中的名称查找进程的PID
的代码。我不能在QNX
上测试它,但它可以在Ubuntu
上工作。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <dirent.h>
#include <libgen.h>
/* checks if the string is purely an integer
* we can do it with `strtol' also
*/
int check_if_number (char *str)
{
int i;
for (i=0; str[i] != ' '; i++)
{
if (!isdigit (str[i]))
{
return 0;
}
}
return 1;
}
#define MAX_BUF 1024
#define PID_LIST_BLOCK 32
int *pidof (char *pname)
{
DIR *dirp;
FILE *fp;
struct dirent *entry;
int *pidlist, pidlist_index = 0, pidlist_realloc_count = 1;
char path[MAX_BUF], read_buf[MAX_BUF];
dirp = opendir ("/proc/");
if (dirp == NULL)
{
perror ("Fail");
return NULL;
}
pidlist = malloc (sizeof (int) * PID_LIST_BLOCK);
if (pidlist == NULL)
{
return NULL;
}
while ((entry = readdir (dirp)) != NULL)
{
if (check_if_number (entry->d_name))
{
strcpy (path, "/proc/");
strcat (path, entry->d_name);
strcat (path, "/comm");
/* A file may not exist, it may have been removed.
* dut to termination of the process. Actually we need to
* make sure the error is actually file does not exist to
* be accurate.
*/
fp = fopen (path, "r");
if (fp != NULL)
{
fscanf (fp, "%s", read_buf);
if (strcmp (read_buf, pname) == 0)
{
/* add to list and expand list if needed */
pidlist[pidlist_index++] = atoi (entry->d_name);
if (pidlist_index == PID_LIST_BLOCK * pidlist_realloc_count)
{
pidlist_realloc_count++;
pidlist = realloc (pidlist, sizeof (int) * PID_LIST_BLOCK * pidlist_realloc_count); //Error check todo
if (pidlist == NULL)
{
return NULL;
}
}
}
fclose (fp);
}
}
}
closedir (dirp);
pidlist[pidlist_index] = -1; /* indicates end of list */
return pidlist;
}
int main (int argc, char *argv[])
{
int *list, i;
if (argc != 2)
{
printf ("Usage: %s proc_namen", argv[0]);
return 0;
}
list = pidof (argv[1]);
for (i=0; list[i] != -1; i++)
{
printf ("%d ", list[i]);
}
free (list);
if (list[0] != -1)
{
printf ("n");
}
return 0;
}
为了将进程名称转换为pid,您需要挖掘QNX的/proc文件系统。我写了一本名为《QNX食谱》的书,现在可以在QNX的网站上免费获得(http://www.qnx.com/download/feature.html?programid=26184)。转到第222页"遍历进程列表",复制遍历进程列表的代码。这将允许您在所有进程中搜索要杀死的进程(它将为您提供所需的PID(。
void
iterate_processes (void)
{
struct dirent *dirent;
DIR *dir;
int r;
int pid;
// 1) find all processes
if (!(dir = opendir ("/proc"))) {
fprintf (stderr, "%s: couldn't open /proc, errno %dn",
progname, errno);
perror (NULL);
exit (EXIT_FAILURE);
}
while (dirent = readdir (dir)) {
// 2) we are only interested in process IDs
if (isdigit (*dirent -> d_name)) {
pid = atoi (dirent -> d_name);
iterate_process (pid);
}
}
closedir (dir);
}
void
iterate_process (int pid)
{
char paths [PATH_MAX];
int fd;
// 1) set up structure
static struct {
procfs_debuginfo info;
char buff [PATH_MAX];
} name;
sprintf (paths, "/proc/%d/as", pid);
if ((fd = open (paths, O_RDONLY)) == -1) {
return;
}
// 2) ask for the name
if (devctl (fd, DCMD_PROC_MAPDEBUG_BASE, &name,
sizeof (name), 0) != EOK) {
if (pid == 1) {
strcpy (name.info.path, "(procnto)");
} else {
strcpy (name.info.path, "(n/a)");
}
}
// 3) we can compare against name.info.path here...
do_process (pid, fd, name.info.path);
close (fd);
}
通过为"do_process(("提供您想要的任何操作,您可以例如通过名称等进行kill。
相关文章:
- boost::进程间消息队列引发错误
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 是否可以通过C++扩展强制多个python进程共享同一内存
- IPC使用多个管道和分支进程来运行Python程序
- 异常属于C++中的线程还是进程
- WMI检测进程创建事件-c++
- c++多进程编写一个唯一的文件
- 如何在C++中将函数发送到另一个进程
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 终止 QProcess 不会终止子进程
- 将返回值从 exe 传递到 bat,并将其传递给 C# 中的进程
- COM :是否可以查看是否存在对我的某个 COM 对象的进程外引用?我可以释放它吗?
- Windows 进程间同步类似事件?
- 在挂钩启动新线程时解除挂钩进程
- pclose() 不会给我进程退出代码
- 运行代码时,c++ 会终止进程
- 可以读入进程内存的最大块大小是多少?
- 枚举进程模块在有效句柄上返回无效句柄
- 让 Gnu 屏幕将 SIGTERM 信号传递给子进程,允许它们干净地关闭
- 如何通过QNXCpp代码中的进程名称将SIGTERM发送到进程