首页 \ 问答 \ O_ASYNC停止生成SIGIO(O_ASYNC stops generating SIGIO)

O_ASYNC停止生成SIGIO(O_ASYNC stops generating SIGIO)

这有点长...对于初学者我在Linux 2.6.33,gcc 4.4.4。

我写了一个小程序,它创建一个命名管道并读取它,直到它看到某个字符串,然后它取消了FIFO,并重新执行自己。

#include<unistd.h>
#include<fcntl.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/stat.h>
int fifo;
#define put(x) write(1, x, (sizeof x)-1)
void reader(int a)
{
      char buf[26];
      int n;
      while((n=read(fifo, buf, 25))>0){
            buf[25] = '\0';
            if(!strncmp(buf, "moo", 3)){
                    put("exec()-ing\n");
                    close(fifo);
                    unlink("lefifo");
                    execl("/home/dave/a.out", "a.out", 0);
            }
            write(1, buf, n);
      }
}

main()
{
      signal(SIGIO, reader);
      mknod("lefifo", 0600|S_IFIFO,0);
      fifo = open("lefifo", O_RDONLY|O_NONBLOCK );
      fcntl(fifo, F_SETOWN, getpid());
      fcntl(fifo, F_SETFL, O_ASYNC);

      for(;;)
            pause();
}

编译后,在后台运行时,我可以lefifo并按预期工作,直到我输入以“moo”开头的字符串。 以下示例会话:

$ gcc fifo.c 
$ ./a.out&
$ echo klar > lefifo
klar
$ echo moo > lefifo
exec()-ing
$ echo klar2 > lefifo
$ echo where did you go > lefifo
$ echo moo > lefifo
$ pkill a.out

生成此痕迹(一些脂肪修剪):

execve("./a.out", ["./a.out"], [/* 36 vars */]) = 0
mknod("lefifo", S_IFIFO|0600)           = 0
open("lefifo", O_RDONLY|O_NONBLOCK)     = 3
getpid()                                = 3945
fcntl(3, F_SETOWN, 3945)                = 0
fcntl(3, F_SETFL, O_RDONLY|O_ASYNC)     = 0
pause()                                 = ? ERESTARTNOHAND (To be restarted)
--- SIGIO (I/O possible) @ 0 (0) ---
read(3, "klar\n"..., 25)                = 5
write(1, "klar\n"..., 5)                = 5
read(3, ""..., 25)                      = 0
sigreturn()                             = ? (mask now [])
pause()                                 = ? ERESTARTNOHAND (To be restarted)
--- SIGIO (I/O possible) @ 0 (0) ---
read(3, "moo\n"..., 25)                 = 4
write(1, "exec()-ing\n"..., 13)         = 13
close(3)                                = 0
unlink("lefifo")                        = 0
execve("/home/dave/a.out", ["a.out"], [/* 36 vars */]) = 0
mknod("lefifo", S_IFIFO|0600)           = 0
open("lefifo", O_RDONLY|O_NONBLOCK)     = 3
getpid()                                = 3945
fcntl(3, F_SETOWN, 3945)                = 0
fcntl(3, F_SETFL, O_RDONLY|O_ASYNC)     = 0
pause()                                 = ? ERESTARTNOHAND (To be restarted)
--- SIGTERM (Terminated) @ 0 (0) ---

正如您所看到的,第一次,创建FIFO没有问题,SIGIO生成就好了; 但是在exec() ,新的FIFO将不会产生任何信号。 旧的显示成功关闭,似乎成功删除。

我很难过为什么它会这样做。 有任何想法吗?


This is a little long... For starters I'm on Linux 2.6.33, gcc 4.4.4.

I've written a small program, which creates a named pipe and reads it, until it sees a certain string, whereupon it gets rid of the FIFO, and re-executes itself.

#include<unistd.h>
#include<fcntl.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/stat.h>
int fifo;
#define put(x) write(1, x, (sizeof x)-1)
void reader(int a)
{
      char buf[26];
      int n;
      while((n=read(fifo, buf, 25))>0){
            buf[25] = '\0';
            if(!strncmp(buf, "moo", 3)){
                    put("exec()-ing\n");
                    close(fifo);
                    unlink("lefifo");
                    execl("/home/dave/a.out", "a.out", 0);
            }
            write(1, buf, n);
      }
}

main()
{
      signal(SIGIO, reader);
      mknod("lefifo", 0600|S_IFIFO,0);
      fifo = open("lefifo", O_RDONLY|O_NONBLOCK );
      fcntl(fifo, F_SETOWN, getpid());
      fcntl(fifo, F_SETFL, O_ASYNC);

      for(;;)
            pause();
}

When compiled, and run in the background, I can echo to lefifo and it works as expected, until I enter a string beginning with "moo". The following example session:

$ gcc fifo.c 
$ ./a.out&
$ echo klar > lefifo
klar
$ echo moo > lefifo
exec()-ing
$ echo klar2 > lefifo
$ echo where did you go > lefifo
$ echo moo > lefifo
$ pkill a.out

Generates this trace (some fat trimmed):

execve("./a.out", ["./a.out"], [/* 36 vars */]) = 0
mknod("lefifo", S_IFIFO|0600)           = 0
open("lefifo", O_RDONLY|O_NONBLOCK)     = 3
getpid()                                = 3945
fcntl(3, F_SETOWN, 3945)                = 0
fcntl(3, F_SETFL, O_RDONLY|O_ASYNC)     = 0
pause()                                 = ? ERESTARTNOHAND (To be restarted)
--- SIGIO (I/O possible) @ 0 (0) ---
read(3, "klar\n"..., 25)                = 5
write(1, "klar\n"..., 5)                = 5
read(3, ""..., 25)                      = 0
sigreturn()                             = ? (mask now [])
pause()                                 = ? ERESTARTNOHAND (To be restarted)
--- SIGIO (I/O possible) @ 0 (0) ---
read(3, "moo\n"..., 25)                 = 4
write(1, "exec()-ing\n"..., 13)         = 13
close(3)                                = 0
unlink("lefifo")                        = 0
execve("/home/dave/a.out", ["a.out"], [/* 36 vars */]) = 0
mknod("lefifo", S_IFIFO|0600)           = 0
open("lefifo", O_RDONLY|O_NONBLOCK)     = 3
getpid()                                = 3945
fcntl(3, F_SETOWN, 3945)                = 0
fcntl(3, F_SETFL, O_RDONLY|O_ASYNC)     = 0
pause()                                 = ? ERESTARTNOHAND (To be restarted)
--- SIGTERM (Terminated) @ 0 (0) ---

As you can see, the first time around, there is no trouble making the FIFO, and SIGIO is generated just fine; but after the exec() the new FIFO won't generate any signals. The old one shows a successful close and seems to get deleted successfully.

I'm quite stumped as to why it might behave this way. Any ideas?


原文:https://stackoverflow.com/questions/7034175
更新时间:2023-09-22 19:09

最满意答案

使用usort过滤低位:

$array = //your array

function cmp($a, $b) {
    if ($a['po'] == $b['po'] && $a['isbn'] == $b['isbn']) {
       return ($a['low'] < $b['low']) ? -1 : 1;
    }else{
        return 0;
    }

}

usort($array, 'cmp');

Foreach循环重新填充数组。

$po = null;
$isbn = null;
$result = [];
foreach($array as $key =>$val){    
    if($val['po']==$po && $val['isbn']==$isbn){
    }else{
        $result[] = $val; 
    }
    $isbn = $val['isbn'];
    $po = $val['po'];
}
echo '<pre>';
print_r($result);
echo '</pre>';

Use usort to filter through the low:

$array = //your array

function cmp($a, $b) {
    if ($a['po'] == $b['po'] && $a['isbn'] == $b['isbn']) {
       return ($a['low'] < $b['low']) ? -1 : 1;
    }else{
        return 0;
    }

}

usort($array, 'cmp');

Foreach loop to repopulate the array.

$po = null;
$isbn = null;
$result = [];
foreach($array as $key =>$val){    
    if($val['po']==$po && $val['isbn']==$isbn){
    }else{
        $result[] = $val; 
    }
    $isbn = $val['isbn'];
    $po = $val['po'];
}
echo '<pre>';
print_r($result);
echo '</pre>';

相关问答

更多

相关文章

更多

最新问答

更多
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • 电脑高中毕业学习去哪里培训
  • 电脑系统专业就业状况如何啊?
  • IEnumerable linq表达式(IEnumerable linq expressions)
  • 如何在Spring测试中连接依赖关系(How to wire dependencies in Spring tests)
  • Solr可以在没有Lucene的情况下运行吗?(Can Solr run without Lucene?)
  • 如何保证Task在当前线程上同步运行?(How to guarantee that a Task runs synchronously on the current thread?)
  • 在保持每列的类的同时向数据框添加行(Adding row to data frame while maintaining the class of each column)
  • 的?(The ? marks in emacs/haskell and ghc mode)
  • 一个线程可以调用SuspendThread传递自己的线程ID吗?(Can a thread call SuspendThread passing its own thread ID?)
  • 延迟socket.io响应,并“警告 - websocket连接无效”(Delayed socket.io response, and “warn - websocket connection invalid”)
  • 悬停时的图像转换(Image transition on hover)
  • IIS 7.5仅显示homecontroller(IIS 7.5 only shows homecontroller)
  • 没有JavaScript的复选框“关闭”值(Checkbox 'off' value without JavaScript)
  • java分布式框架有哪些
  • Python:填写表单并点击按钮确认[关闭](Python: fill out a form and confirm with a button click [closed])
  • PHP将文件链接到根文件目录(PHP Linking Files to Root File Directory)
  • 我如何删除ListView中的项目?(How I can remove a item in my ListView?)
  • 您是否必须为TFS(云)中的每个BUG创建一个TASK以跟踪时间?(Do you have to create a TASK for every BUG in TFS (Cloud) to track time?)
  • typoscript TMENU ATagParams小写(typoscript TMENU ATagParams lowercase)
  • 武陟会计培训类的学校哪个好点?
  • 从链接中删除文本修饰(Remove text decoration from links)