- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有 2 个进程,父进程和子进程。父进程应该等待用户插入一些字符串,然后它应该通过管道将这个字符串发送给子进程。我已经成功地做到了这一点,但我想要实现的是检查管道是否为空,如果它为空 60 秒,那么子进程应该向其父进程发送一个信号。
到目前为止我的代码:
int main(void)
{
int fd[2], nbytes;
pid_t childpid;
char string[100];
char readbuffer[80];
pipe(fd);
if((childpid = fork()) == -1)
{
perror("fork");
exit(1);
}
else if(childpid > 0) //parent process
{
close(fd[0]);
printf ("Insert a string: ");
scanf("%s",string);
/* Send "string" through the output side of pipe */
write(fd[1], string, (strlen(string)+1));
exit(0);
}
else // child process
{
close(fd[1]);
// check if pipe is empty for 60 seconds
nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
printf("Received string: %s", readbuffer);
}
return(0);
}
最佳答案
这是一个示例,其中父级 sleep()
随机数秒,然后通过管道 write()
s 给子级。如果子进程在5.5秒内没有收到消息,则发送SIGUSR1
给父进程,父进程不写消息。这将重复指定的次数。
请注意, child 等待 5.5 秒,而不是 5 秒,因为我们在父级中使用 sleep()
,这需要整整几秒。这(通常)会留下足够的时间间隔,以避免父 sleep()
和子等待 5 秒,并且它们的电线在中间交叉的情况。我们可以在父级中使用 nanosleep()
,让子级等待整数秒。
注意事项:
我们可以在子进程中使用带有超时的select()
来等待指定的时间量。 select()
将返回就绪文件描述符的数量,因此如果我们只检查管道并返回 0
,我们就知道管道中没有任何内容可供读取。
我们可以在父级中使用sleep()
来测试它。 sleep()
如果它在请求的时间内休眠则返回 0,如果它被信号中断则返回非零,因此我们可以检查返回值以确定是哪种情况。对于您的特定情况,如果您希望父级从用户那里获取输入,read()
将返回 -1
并将 errno
设置为 EINTR
如果它正在等待用户输入并接收到信号,那么您可以通过这种方式检测超时。当然,在这个特定的用例中, parent 更容易在 60 秒超时时在 STDIN_FILENO
上调用 select()
本身,而不是让 child 等待管道并发送信号。
在 child 中,我们必须在每次循环中FD_ZERO
fd_set
并重新填充struct timeval
,因为select()
可能会修改它们。
我们需要为 SIGUSR1
注册一个信号处理程序(它什么都不做)以避免进程在收到信号时被终止。我们不需要信号处理程序做任何事情,因为 sleep()
会在它被信号中断时通过返回值告诉我们,这就是我们需要的所有信息,就在这里。
代码:
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/select.h>
#define READ_END 0
#define WRITE_END 1
#define SEC_THRESHOLD 5
#define LOOP_TIMES 5
#define BUFFER_SIZE 512
void handler(int signum)
{
(void) signum; // Ignore unused argument
}
int main(void)
{
int p[2];
if ( pipe(p) == -1 ) {
perror("pipe() failed");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if ( pid == -1 ) {
perror("fork() failed");
exit(EXIT_FAILURE);
}
else if ( pid == 0 ) {
if ( close(p[WRITE_END]) == -1 ) {
perror("failed to close pipe end in child");
exit(EXIT_FAILURE);
}
for ( int i = 0; i < LOOP_TIMES; ++i ) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(p[READ_END], &fds);
struct timeval timeout;
timeout.tv_sec = SEC_THRESHOLD;
timeout.tv_usec = 500000;
printf("Loop %d: child waiting for %d.5 seconds.\n",
i, SEC_THRESHOLD);
int status = select(p[READ_END] + 1, &fds, NULL, NULL, &timeout);
if ( status == -1 ) {
perror("select() failed in child");
exit(EXIT_FAILURE);
}
else if ( status == 0 ) {
printf("Loop %d: timed out in child, sending signal.\n", i);
kill(getppid(), SIGUSR1);
}
else {
char buffer[BUFFER_SIZE] = {0};
if ( read(p[READ_END], buffer, BUFFER_SIZE - 1) == -1 ) {
perror("read() failed in child");
exit(EXIT_FAILURE);
}
printf("Loop %d: child read: [%s]\n", i, buffer);
}
}
if ( close(p[READ_END]) == -1 ) {
perror("failed to close read end of pipe in child");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
else {
if ( close(p[READ_END]) == -1 ) {
perror("failed to close pipe end in parent");
exit(EXIT_FAILURE);
}
struct sigaction sa;
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGUSR1, &sa, NULL);
srand((unsigned) time(NULL));
for ( int i = 0; i < LOOP_TIMES; ++i ) {
const char * msg = "Message to child";
const int wait_time = rand() % 6 + 3;
printf("Loop %d: parent waiting for %d seconds...\n", i, wait_time);
int status = sleep(wait_time);
if ( status == 0 ) {
printf("Loop %d: parent sending message to child.\n", i);
if ( write(p[WRITE_END], msg, strlen(msg)) == -1 ) {
perror("write() error in parent");
exit(EXIT_FAILURE);
}
}
else {
printf("Loop %d: parent interrupted by signal.\n", i);
}
}
if ( close(p[WRITE_END]) == -1 ) {
perror("failed to close write end of pipe in parent");
exit(EXIT_FAILURE);
}
}
if ( waitpid(pid, NULL, 0) == -1 ) {
perror("waitpid() failed");
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
和示例输出 session :
paul@horus:~/src/sandbox$ ./sigpipe
Loop 0: parent waiting for 7 seconds...
Loop 0: child waiting for 5.5 seconds.
Loop 0: timed out in child, sending signal.
Loop 1: child waiting for 5.5 seconds.
Loop 0: parent interrupted by signal.
Loop 1: parent waiting for 7 seconds...
Loop 1: timed out in child, sending signal.
Loop 2: child waiting for 5.5 seconds.
Loop 1: parent interrupted by signal.
Loop 2: parent waiting for 6 seconds...
Loop 2: timed out in child, sending signal.
Loop 3: child waiting for 5.5 seconds.
Loop 2: parent interrupted by signal.
Loop 3: parent waiting for 5 seconds...
Loop 3: parent sending message to child.
Loop 4: parent waiting for 3 seconds...
Loop 3: child read: [Message to child]
Loop 4: child waiting for 5.5 seconds.
Loop 4: parent sending message to child.
Loop 4: child read: [Message to child]
paul@horus:~/src/sandbox$
因此我们可以看到,如果子级达到 5.5 秒的超时时间,父级的每次迭代都会被中断,而在所有其他情况下成功发送消息。
关于检查管道是否为空,UNIX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34374000/
我已经为使用 JGroups 编写了简单的测试。有两个像这样的简单应用程序 import org.jgroups.*; import org.jgroups.conf.ConfiguratorFact
我有一个通过 ajax 检索的 json 编码数据集。我尝试检索的一些数据点将返回 null 或空。 但是,我不希望将那些 null 或空值显示给最终用户,或传递给其他函数。 我现在正在做的是检查
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Why does one often see “null != variable” instead of “
嗨在我们公司,他们遵循与空值进行比较的严格规则。当我编码 if(variable!=null) 在代码审查中,我收到了对此的评论,将其更改为 if(null!=variable)。上面的代码对性能有影
我正在尝试使用 native Cordova QR 扫描仪插件编译项目,但是我不断收到此错误。据我了解,这是代码编写方式的问题,它向构造函数发送了错误的值,或者根本就没有找到构造函数。那么我该如何解决
我在装有 Java 1.8 的 Windows 10 上使用 Apache Nutch 1.14。我已按照 https://wiki.apache.org/nutch/NutchTutorial 中提
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: what is “=null” and “ IS NULL” Is there any difference bet
Three-EyedRaven 内网渗透初期,我们都希望可以豪无遗漏的尽最大可能打开目标内网攻击面,故,设计该工具的初衷是解决某些工具内网探测速率慢、运行卡死、服务爆破误报率高以及socks流
我想在Scala中像在Java中那样做: public void recv(String from) { recv(from, null); } public void recv(String
我正在尝试从一组图像补丁中创建一个密码本。我已将图像(Caltech 101)分成20 X 20图像块。我想为每个补丁创建一个SIFT描述符。但是对于某些图像补丁,它不返回任何描述符/关键点。我尝试使
我在验证器类中自动连接的两个服务有问题。这些服务工作正常,因为在我的 Controller 中是自动连接的。我有一个 applicationContext.xml 文件和 MyApp-servlet.
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭10 年前。 问题必须表现出对要解决的问题的最低程度的了解。告诉我们您尝试过做什么,为什么不起作用,以
大家好,我正在对数据库进行正常的选择,但是 mysql_num_rowsis 为空,我不知道为什么,我有 7 行选择。 如果您发现问题,请告诉我。 真的谢谢。 代码如下: function get_b
我想以以下格式创建一个字符串:id[]=%@&stringdata[]=%@&id[]=%@&stringdata[]=%@&id[]=%@&stringdata[]=%@&等,在for循环中,我得到
我正在尝试使用以下代码将URL转换为字符串: NSURL *urlOfOpenedFile = _service.myURLRequest.URL; NSString *fileThatWasOpen
我正在尝试将NSNumber传递到正在工作的UInt32中。然后,我试图将UInt32填充到NSData对象中。但是,这在这里变得有些时髦... 当我尝试将NSData对象中的内容写成它返回的字符串(
我正在进行身份验证并收到空 cookie。我想存储这个 cookie,但服务器没有返回给我 cookie。但响应代码是 200 ok。 httpConn.setRequestProperty(
我认为 Button bTutorial1 = (Button) findViewById(R.layout.tutorial1); bTutorial1.setOnClickListener
我的 Controller 中有这样的东西: model.attribute("hiringManagerMap",hiringManagerMap); 我正在访问此 hiringManagerMap
我想知道如何以正确的方式清空列表。在 div 中有一个列表然后清空 div 或列表更好吗? 我知道这是一个蹩脚的问题,但请帮助我理解这个 empty() 函数:) 案例)如果我运行这个脚本会发生什么:
我是一名优秀的程序员,十分优秀!