【发布时间】:2014-04-03 05:07:03
【问题描述】:
这是我的代码:
extern int errno;
pid_t system1(const char * command)
{
pid_t pid;
pid = fork();
cout<<"PID in child "<<(int)pid<<endl;
if (pid < 0) {
return pid;
} else if (pid == 0) {
execl("/bin/sh", "sh", "-c", command, (char*)NULL);
_exit(1);
}
int stat_val;
pid_t child_pid;
cout << "Hello1" << endl;
child_pid = wait(&stat_val);
cout << "child_pid = " <<(int)child_pid<< endl;//LINE 1
if(WIFEXITED(stat_val))
printf("Child has terminated with exit code %d\n", WIFEXITED(stat_val));
else
printf("Child has existed abnormally\n");
return child_pid;
}
int main( )
{
int pid_1 = system1("setup.csh &");;
struct stat status;
sleep(10);
cout<<"errno = "<<errno<<endl;
int i = kill(pid_1,0);
cout<<"Pid id = "<<pid_1<<endl;
cout<<"i === "<<i<<endl;
cout<<"errno = "<<errno<<endl;
if(errno == ESRCH)
{
cout<<"process does not exist";
}
else
{
cout<<"process exist"<<endl;
}
return 0;
}
在上面的代码中,我在 LINE 1 和 process setup.csh 的 PID 和 PID 处得到不同的 PID 孩子。谁能帮帮我。我想得到我的process setup.csh 中的PID。
我正在控制台中使用ps -u user | grep setup.csh 查找其他PID 值。
【问题讨论】:
-
这不清楚。您在
LINE_1上看到一个值;您看到的其他价值是什么? -
@OliCharlesworth 使用
ps -u | grep setup.csh我已经更新了我的问题。