在 C\C++ 中多次等待
multiple waits in C\C++
我有一个程序可以根据用户的判断自行分叉多次。
我想知道 child 何时退出(并且它是 return 状态),同时仍在尝试做其他事情。
我知道我可以用线程解决这个问题;但是,我不知道是否有类似 poll(2)
的函数来检查变量变化(如观察点)。
作为 伪代码 示例:
int counter = 0;
child()
{
cout << "Hello World\n";
return counter++;
}
int main()
{
vector<pid_t> children;
vector<int> status;
char answer = 'y';
while(answer != 'q')
{
cout << "create a new child?\n";
cin >> answer;
if(answer == 'y')
{
pid_t temp;
temp = fork();
if(temp == 0)
{
child();
}
else
{
//save the child's pid
children.push_back(temp);
}
}
int num_ready = 0;
//wait to see if there was an event otherwise go back to user input
if(( num_ready = poll(children, timeout)) > 0)
{
//loop through all ready children
for( int x = 0; x < num_ready)
{
int temp_status = 0;
//some way to know which child is ready
waitpid(children, &temp_status, 0);
status.pushback(temp_status);
}
}
}
}
我知道我 可以 产生一个线程做 wait(&status)
,然后在发生变化时尝试缩小它的范围,然后通知 parent ;但是,我宁愿没有那么多开销。
来自wait(2)
:
The value of options is an OR of zero or more of the following constants:
WNOHANG return immediately if no child has exited.
WUNTRACED also return if a child has stopped (but not traced via ptrace(2)). Status for traced children which have stopped is provided even if this option is not specified.
WCONTINUED (since Linux 2.6.10)
also return if a stopped child has been resumed by delivery of SIGCONT.
因此,我们可以将其应用于上述内容。
int counter = 0;
child()
{
cout << "Hello World\n";
return counter++;
}
int main()
{
vector<pid_t> children;
vector<int> status;
char answer = 'y';
while(answer != 'q')
{
cout << "create a new child?\n";
cin >> answer;
if(answer == 'y')
{
pid_t temp;
temp = fork();
if(temp == 0)
{
child();
}
else
{
//save the child's pid
children.push_back(temp);
}
}
//loop through all ready children
for( auto &x : children)
{
int temp_status = 0;
//some way to know which child is ready
if(waitpid(x, &temp_status, WNOHANG) > 0)
{
status.insert(x, 1, temp_status);
}
}
}
}
如果您在 C 中执行此操作,则只需将向量替换为数组即可。
我有一个程序可以根据用户的判断自行分叉多次。
我想知道 child 何时退出(并且它是 return 状态),同时仍在尝试做其他事情。
我知道我可以用线程解决这个问题;但是,我不知道是否有类似 poll(2)
的函数来检查变量变化(如观察点)。
作为 伪代码 示例:
int counter = 0;
child()
{
cout << "Hello World\n";
return counter++;
}
int main()
{
vector<pid_t> children;
vector<int> status;
char answer = 'y';
while(answer != 'q')
{
cout << "create a new child?\n";
cin >> answer;
if(answer == 'y')
{
pid_t temp;
temp = fork();
if(temp == 0)
{
child();
}
else
{
//save the child's pid
children.push_back(temp);
}
}
int num_ready = 0;
//wait to see if there was an event otherwise go back to user input
if(( num_ready = poll(children, timeout)) > 0)
{
//loop through all ready children
for( int x = 0; x < num_ready)
{
int temp_status = 0;
//some way to know which child is ready
waitpid(children, &temp_status, 0);
status.pushback(temp_status);
}
}
}
}
我知道我 可以 产生一个线程做 wait(&status)
,然后在发生变化时尝试缩小它的范围,然后通知 parent ;但是,我宁愿没有那么多开销。
来自wait(2)
:
The value of options is an OR of zero or more of the following constants: WNOHANG return immediately if no child has exited. WUNTRACED also return if a child has stopped (but not traced via ptrace(2)). Status for traced children which have stopped is provided even if this option is not specified. WCONTINUED (since Linux 2.6.10) also return if a stopped child has been resumed by delivery of SIGCONT.
因此,我们可以将其应用于上述内容。
int counter = 0;
child()
{
cout << "Hello World\n";
return counter++;
}
int main()
{
vector<pid_t> children;
vector<int> status;
char answer = 'y';
while(answer != 'q')
{
cout << "create a new child?\n";
cin >> answer;
if(answer == 'y')
{
pid_t temp;
temp = fork();
if(temp == 0)
{
child();
}
else
{
//save the child's pid
children.push_back(temp);
}
}
//loop through all ready children
for( auto &x : children)
{
int temp_status = 0;
//some way to know which child is ready
if(waitpid(x, &temp_status, WNOHANG) > 0)
{
status.insert(x, 1, temp_status);
}
}
}
}
如果您在 C 中执行此操作,则只需将向量替换为数组即可。