Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查c+中是否存在进程+;用线 我试图用线程检查C++中进程的存在。 我最初在没有任何线程的情况下进行测试,并让主线程检查是否存在。 成功了。 但突然,当我把这部分代码放在线程中时,它就不起作用了。 看到它不起作用,我感到困惑。 有人能告诉我为什么在线程中使用代码部分时,代码部分不起作用吗_C++_C_Process_Pthreads - Fatal编程技术网

检查c+中是否存在进程+;用线 我试图用线程检查C++中进程的存在。 我最初在没有任何线程的情况下进行测试,并让主线程检查是否存在。 成功了。 但突然,当我把这部分代码放在线程中时,它就不起作用了。 看到它不起作用,我感到困惑。 有人能告诉我为什么在线程中使用代码部分时,代码部分不起作用吗

检查c+中是否存在进程+;用线 我试图用线程检查C++中进程的存在。 我最初在没有任何线程的情况下进行测试,并让主线程检查是否存在。 成功了。 但突然,当我把这部分代码放在线程中时,它就不起作用了。 看到它不起作用,我感到困惑。 有人能告诉我为什么在线程中使用代码部分时,代码部分不起作用吗,c++,c,process,pthreads,C++,C,Process,Pthreads,我对进程存在性的初始测试程序: 编译如下:CC-2.CC-o-nothr int main(int argc, char **argv) { struct stat sts; string f=string("/proc/")+argv[1]; while(!(stat(f.c_str(), &sts) == -1 && errno == ENOENT)) { cout<<"process exists"<<endl; sleep(1); } c

我对进程存在性的初始测试程序: 编译如下:
CC-2.CC-o-nothr

int main(int argc, char **argv)
{

struct stat sts;
string f=string("/proc/")+argv[1];
while(!(stat(f.c_str(), &sts) == -1 && errno == ENOENT))
{
cout<<"process exists"<<endl;
sleep(1);
}
cout<<"process Does not exist"<<endl;
return 0;

}
extern "C" void *run(void *str){

struct stat sts;
while(!(stat((char *)str, &sts) == -1 && errno == ENOENT))
{
cout<<"process exists"<<endl;
sleep(1);
}
return NULL;

}

int main(int argc,char **argv)
{
string f=string("/proc/")+argv[1];

pthread_t pid;
int res=pthread_create(&pid,NULL,run,(void *)f.c_str());
pthread_join(pid,NULL);
cout<<f<<endl;
cout<<"process Does not exist"<<endl;
return 0;
}
没有线程时的输出:

> ./a.out &
[1] 10128
> ./nothr 10128
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process Does not exist
[1]  + Done                          ./a.out
> 
> ./thr 10458
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
^C
有线程时的输出:

> ./a.out &
[1] 10128
> ./nothr 10128
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process Does not exist
[1]  + Done                          ./a.out
> 
> ./thr 10458
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
process exists
^C

它一直持续到我按下CTRL+C。

非常严格地说,
f.C_str()
(在您的线程代码示例中)的有效生存期完全是在
pthread_create
调用期间。因此,在线程调用
stat
时,传递的地址的内容可能已经发生了足够的更改,从而导致您获得除
enoint
以外的错误。这将使你的测试失败,你将永远循环


顺便说一句,使用
kill(pid,0)
是一种更轻便的测试进程存在性的方法。

在线程函数
中,当你反复调用
循环条件时!(stat((char*)str和sts)==-1
使用相同的参数。因此它会永远循环。但是一旦进程不存在,while循环就应该退出。这就是while循环中的条件所做的。当没有线程时会出现相同的while条件,并且正如预期的那样,while会在进程退出后立即退出。但出于好奇,为什么会出现d这里有自己的投票吗?可能是因为您似乎没有尝试调试您的问题。例如,您可以将传递到线程函数中的字符串cout/printf'd以确保其正确性。您可以将检查分为两行,使用单独的临时bool作为结果,以查看哪个失败/成功。我通过打印线程内的字符串,并显示正确的进程id。该循环将继续,直到stat返回-1且errno设置为enoint。显然不是。因此,要么您查看的pid错误,要么代码未发布。