我正在寻找将 PID 值从用户空间复制到内核空间,这是我的代码快照。
内核模块:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/siginfo.h>
#include <linux/rcupdate.h>
#include <linux/sched.h>
#include <linux/uaccess.h>
#include <linux/signal.h>
MODULE_LICENSE ("GPL");
struct siginfo sinfo;
pid_t pid;
struct task_struct *task;
int init_module()
{
memset(&sinfo, 0, sizeof(struct siginfo));
sinfo.si_signo = SIGIO;
sinfo.si_code = SI_USER;
pid = 5218; // Everytime a new PID
// task = find_task_by_vpid(pid); I am also working on new and old version of UBUNTU so thats why this is here
task = pid_task(find_vpid(pid), PIDTYPE_PID);
printk("%d .\n", task);
if(task …Run Code Online (Sandbox Code Playgroud) 我想将进程 pid 转换为 const char* 但下面不起作用:
std::ostringstream str_pid;
str_pid << getpid();
const char * cstr_pid = str_pid.str().c_str();
Run Code Online (Sandbox Code Playgroud)
它在大多数情况下都有效,但有时会产生错误的结果。显然我做错了什么。任何想法?
我有一段代码在这里漂浮了一段时间(代码对我有用):
- (NSArray *) runningProcesses {
//CTL_KERN?KERN_PROC,KERN_PROC_ALL
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL ,0};
size_t miblen = 4;
size_t size;
int st = sysctl(mib, miblen, NULL, &size, NULL, 0);
struct kinfo_proc * process = NULL;
struct kinfo_proc * newprocess = NULL;
do {
size += size / 10;
newprocess = realloc(process, size);
if (!newprocess) {
if (process) {
free(process);
process = NULL;
}
return nil;
}
process = newprocess;
st = sysctl(mib, miblen, process, &size, NULL, 0);
} while (st …Run Code Online (Sandbox Code Playgroud) 我需要从我知道标题的窗口提供 PID。
这是一个安装程序,当在我的代码中以编程方式按下第一个“下一步”按钮时,它似乎会更改重要凭据。
我认为这样做是因为窗口逐渐消失然后再次消失,但是当我单击后退按钮并再次单击下一步时,它不会再次执行此操作。
第一个“下一步”按钮,我第一次点击它时,上面有一个盾牌,所以我认为它可能与 UAC 有关。
我正在使用以下代码向窗口发送 ENTER 键盘按键:
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
def setwindowfocus(windowname): # can be the window title or the windows PID
shell.AppActivate(windowname)
def sendkeypresstowindow(windowname, key):
setwindowfocus(windowname)
shell.SendKeys(key)
time.sleep(0.1)
sendkeypresstowindow(u'Some Known Window Title', '{ENTER}')
time.sleep(5) # Wait for next window
sendkeypresstowindow(u'Some Known Window Title', '{ENTER}')
time.sleep(5) # Wait for next window
Run Code Online (Sandbox Code Playgroud)
在shell.AppActivate()可采取的pid也所以我想知道我怎么会得到,如果我拥有的唯一信息是窗口标题。
我尝试使用 pywinauto 并遇到了同样的问题,除了许多不应该在 pywinauto 中访问的成员之外,它对于我需要做什么来使用它非常不直观,所以我宁愿避开它,直到代码被清理了。。
我还注意到窗口的句柄发生了变化,所以如果我能以某种方式从窗口标题中获取句柄,然后从句柄中获取 PID 也可以使用。
我在MacBook上遇到MySQL问题.在安装MySQL服务器之前,我已将我的操作系统从Yosemite更新到El Capitan.
现在,当我尝试在终端上运行MySQL时,我得到了这样的错误
ERROR! The server quit without updating PID file
Run Code Online (Sandbox Code Playgroud)
有人可以帮我吗?非常感谢!
我需要使用 C++ 在 Mac OSX 上找到某个程序的 PID 并将其保存为变量。我一直在寻找这个问题的答案,但找不到详细的或有效的答案。如果有人对如何执行此操作有任何想法,请回复。谢谢!
考虑到我们有some_function ,它打印结果stdout而不是返回它。改变它的定义超出了我们的范围,没有其他选择。我们可以选择从stdout. 所以问题。
如何读取 C++ 程序本身的标准输出。
pid如果我们可以获得fd相同的程序,则可以搜索我,但我找不到任何东西。
#include <unistd.h>
#include <sys/types.h>
#include <iostream>
void some_function(){
std::cout<<"Hello World";
}
int main(){
int pid = ::getpid();
string s = //What to write here.
cout<<"Printing";
some_function(); //This function prints "Hello World" to screen
cout<<s; //"PrintingHello World"
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何将管道附加到同一个进程,而不是创建子进程。
有些人可能会想到创建子进程并调用some_function它,以便能够stdout在父进程中读取它,但是不,some_function取决于调用它的进程,因此我们希望将其称为进程而不是创建子进程。
这个答案解释了如何在使用Perl时获取新进程的pid exec().pid甚至没有改变,所以你需要做的就是获得原始脚本的pid.但是,如果我将输出重定向到文件作为命令的一部分,它就无法工作,我需要这样做.
say "my pid is $$";
exec("childscript.pl"); # same pid
Run Code Online (Sandbox Code Playgroud)
但是如果我将输出重定向为命令的一部分:
say "my pid is $$";
exec("childscript.pl > log.txt"); # different pid, usually old pid + 1
exec("childscript.pl > log.txt 2>&1 &"); # same
Run Code Online (Sandbox Code Playgroud)
那么新的pid比旧的pid高一个(这可能只是因为它们是连续产生的并且不可靠).我通过查看输出和插入sleep 30"childscript.pl"来测试这一点,以便我可以看到它ps -e.
我的猜测是,重定向输出会导致一个新进程进行写入.但是我需要程序的pid,除了我可以执行它之外我无法控制程序.(它也需要在后台运行.)
我做节目,如果页面错误发生在一些流程,可以看到,我的方法做,这是获得所有进程的PID和看到rss,maj_flt寻求在每一个等/proc/[PID],检查是否有在总的变化maj_flt。
但为了得到所有正在运行的进程的PID的,我需要直接从我的C程序得到这些,而无需使用像现有的shell命令ps,top等等。
有谁知道正在运行的 PID 数据存在于/proc何处或其他地方?或者如果有另一种方法可以做到这一点,比如通过我的 C 程序中的系统调用函数来获取它?
CreateProcess包含PROCESS_INFORMATION结构,该结构指定为创建的进程选择的进程ID.
如何在创建进程时指定进程使用哪个进程ID?