- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
好的,所以我一直在尝试获取 Process Signals
使用 PHP
脚本。我遇到了很多我不确定发生了什么的事情,而且我觉得文档没有很好地解释它。
假设我按照 registering signals 上的示例进行操作:
<?php
// tick use required as of PHP 4.3.0
declare(ticks = 1);
// signal handler function
function sig_handler($signo)
{
switch ($signo) {
case SIGTERM:
// handle shutdown tasks
exit;
break;
case SIGHUP:
// handle restart tasks
break;
case SIGUSR1:
echo "Caught SIGUSR1...\n";
break;
default:
// handle all other signals
}
}
echo "Installing signal handler...\n";
// setup signal handlers
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGHUP, "sig_handler");
pcntl_signal(SIGUSR1, "sig_handler");
// or use an object, available as of PHP 4.3.0
// pcntl_signal(SIGUSR1, array($obj, "do_something"));
echo"Generating signal SIGTERM to self...\n";
// send SIGUSR1 to current process id
posix_kill(posix_getpid(), SIGUSR1);
echo "Done\n";
?>
但是,在我的 sig_handler
函数中,我在末尾添加了一个 exit(1)
。 echo "Done\n";
还会被执行吗?我已经尝试过类似的东西,尽管是在类结构中,而且它只是在 我的 sig_handler
被调用之前。
我的类 __construct
调用函数 registerSignals
:
private function registerSignals(){
if(function_exists("pcntl_signal")){
//call_user_func([$this, "sigintCleanup"]);
pcntl_signal(SIGINT, [$this, "sigintCleanup"]);
pcntl_signal(SIGTERM, [$this, "sigtermCleanup"]);
pcntl_signal(SIGHUP, [$this, "sighupCleanup"]);
pcntl_signal(SIGUSR1, [$this, "sigusr1Cleanup"]);
}
}
protected function sigintCleanup(){
echo "In SIGINT cleanup\n";
$this->cleanup();
}
protected function sigtermCleanup(){
echo "In SIGTERM cleanup\n";
$this->cleanup();
}
protected function sighupCleanup(){
echo "In SIGHUP cleanup\n";
$this->cleanup();
}
protected function sigusr1Cleanup(){
echo "In SIGUSR1 cleanup\n";
$this->cleanup();
}
protected function cleanup(){
echo "Shutting down\n";
exit(1);
}
然后在我对这个过程的测试中,我做了一些简单的 sleep
和 echo
(start
只是一个在之后调用的函数施工和信号注册完成):
function start(){
echo "-- Testing the test script --\n";
sleep(10000);
echo "given how ticks work, this might not be called...\n";
echo "\nAfter sleep... this should not be executed if there was a signal interrupt.\n";
}
所以我在 sleep
期间使用ctl-c
。我的结果?
-- Testing the test script --
^Cgiven how ticks work, this might not be called...
After sleep... this should not be executed if there was a signal interrupt.
In SIGINT cleanup
Shutting down
正如您从我的 echo
中看到的那样,我有点想通了 ticks work 负责在处理信号之前完成 start
函数。我仍然认为可能是这种情况。事实上,我只是不知道。
我确实在我的程序顶部声明了 ticks
:
declare(ticks = 1);
不立即处理信号给我带来了麻烦。例如,如果我的 start
函数中有这段代码(并且我已经测试过了)怎么办?
$count = 0;
while(true){
count++;
echo "count\n";
sleep(5);
}
即使我的cleanup
函数有一个exit
,我也永远不会达到那个,因为SIGINT
只是让我从sleep
,我继续循环并再次开始 sleep
ing。
所以这是我的问题:signal
处理在 PHP
中是如何工作的?如何保证收到信号后立即处理?我不只是想要一些代码来解决这个问题,我想要一个解释(或者至少一个好的解释和摘要的链接)。
PHP 版本:5.4.22
操作系统:Ubuntu 12.10
最佳答案
sleep()
会被一些信号打断,它会返回剩余的 sleep 秒数,你可以做这样的事情让它保持 sleep 。
.....
declare(ticks = 1);
$left = sleep(1000)
while ($left > 0) {
$left = sleep($left);
}
关于PHP sleep 、pcntl_signal 和滴答声,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23001571/
我在 linux (Centos) 上使用 pthread 编程?我想让线程休眠一小段时间以等待某些事情。我正在尝试使用 sleep()、nanosleep() 或 usleep() 或其他可以做到这
此要求出现在我的 Android 应用程序中,但它通常适用于 Java。我的应用程序每隔几秒钟“做某事”。我已经按如下方式实现了这一点(只是相关的 fragment - 不是完整的代码): fragm
我正在使用 esp8266 构建 IR 到 WiFi 桥接器。基本上,我正在构建一个连接到红外 Remote 内的 esp8266 的红外接收器,以通过 wifi 将接收到的红外远程按键转发到服务器。
我想让 Toast 出现,然后让 sleep 运行。 如果我这样做,Toast 会在 sleep 后出现,但我希望反过来。有人有建议吗?这是我的代码 switch (checkedRadioButto
我在做一件简单的事情,打印一条错误消息,延迟执行 5 秒,然后调用另一个函数,这是代码 public void saveAndDisplay() throws InterruptedException
我的 Activity 上有一个按钮,当我单击它时,我希望按钮改变颜色,等待一段时间,然后再次改变颜色。 我尝试过以下两个版本的 sleep : 尝试1: public void buClick(Vi
我尝试过这个: for(int i =0; i1。创建ScheduledExecutorService public static ScheduledExecutorService createSch
我有一个 Winform,需要等待大约 3 - 4 小时。我无法关闭并以某种方式重新打开应用程序,因为它在等待时在后台做的事情很少。 为了实现等待 - 不给 UI 线程造成麻烦和其他原因 - 我有一个
我在网上看到了下面一段关于 Linux 线程的代码。但是当我运行它时,所有线程似乎都在 sleep ,而不仅仅是主线程。为什么?另外,如果没有 sleep(5),“线程创建成功”语句会运行 3 次而不
我有一个 php 脚本,我需要每 5 秒运行一次(运行,等待它完成,等待 5 秒,再次运行) 我有两种方法。要么在脚本中有一个带有 sleep 功能的无限循环,看起来像这样: while (1) {
我有一个图形用户界面,我想显示一些文本,然后稍等一下。 我的代码看起来像这样: //do something (add JTextArea, revalidate, repaint) try{
我想知道安卓手机的 sleep 模式和深度 sleep 模式有什么区别,手机进入休眠模式和深度 sleep 模式会有什么影响。 请提供详细的答案。 问候,皮克斯 最佳答案 深度 sleep 模式与休眠
我正在学习 C。在这个节目中我使用 sleep 功能来减慢倒计时。我的教科书没有指定我应该包含的库来使用 sleep 功能。所以我使用它时没有为它包含任何特殊的库并且它可以工作。但它在代码块中给了我这
我正在尝试模拟按键按下和按键 Action 。 例如:2638 毫秒。 SendMessage(hWnd, WM_KEYDOWN, keyCode, 0); Sleep(2638); SendMess
我在 while 循环中调用一个线程 hibernate 1 秒。当标志为真时,循环将运行(标志为真无限时间)。在循环线程内应该 hibernate 1 秒,唤醒并增加计数器,检查 IF 条件,在 F
有很多文件说“你应该避免使用带警报的 sleep ,因为许多系统使用警报来实现 sleep ”。实际上,我正在为这个问题而苦恼。 那么,当 sleep() 不能很好地处理警报时,是否有人可以帮助我“
我有两个带有图像的jlabel..我尝试在单击另一个标签时更改标签中的图像..(例如游戏)..我有一个问题..当我编写 Thread.sleep 时,图像没有改变..请参阅代码: public cla
我正在研究多线程,我有一个关于线程 sleep 方法的问题。当我在已经处于 sleep 线程(时间 t2)中执行 sleep()(时间 t1)方法时。总 sleep 时间为 t1+t2 或 t2(如果
如果我们不向 sleep( ) 函数传递任何参数,默认 sleep 时间是多少? #include int main() { int pid,dip,cpid; pid = fork(
当我转到 asyncio 页面时,第一个示例是一个 hello world 程序。当我在 python 3.73 上运行它时,我看不出与正常的有什么不同。 谁能告诉我区别并举一个重要的例子? In [
我是一名优秀的程序员,十分优秀!