因此,我有一个while(1)循环,最后有一个sleep(60)秒。问题是,在此期间,我的程序可以接收指令,但需要等待60秒才能执行。我尝试过使用handler和我的另一个程序发送信号(with kill(daemonid,SIGCONT)),但它不会中断sleep() (即使其返回)。
void handler(){
write(1,"bonjour",7);
alarm(1);
}
/*code blocks irrelevant to the question*/
int main(){
/*reads and opens files*/
struct s
如何在后台运行无限循环,同时继续执行脚本?
示例“脚本”:
while true; do something_in_the_background; done
do_something_while_the_loop_goes_on_in_the_background
for 1 2 3; do somethingelse; done
exit 0
这一点(注意到&)似乎会在一段时间后使整个系统崩溃:
while true; do
something_in_the_background &
done
do_something_while_the_loop_goes_on
我正在Linux中做C程序。我有一个主线程,它连续更新两个变量的值,其他线程每20毫秒将这些变量值写入文件中。我利用睡眠来达到这个时间间隔。示例代码如下。
main()
{
.
.
.
.
.
pthread_create(...write_file..); /* started another thread by passing a function write_file */
while(variable1)
{
updates value of variables
}
return 0;
}
void write_file()
{
我得运行一个循环一定的时间。我知道下面的方法。
get start time;
while(1)
{
getcurrenttime;
if(start time + time to run >= current time)
break;
else
do what ever you want.
}
还好吗?或者其他更好的方法?
我正在编写一个PHP脚本,它是一个作业工作者;它的主要任务是检查数据库表中是否有新作业,如果有新作业,则对其执行操作。但工作机会会大量涌入,其间会有很长的间隔,所以我设计了一个睡眠周期,如下所示:
while(true) {
if ($jobs = get_new_jobs()) {
// Act upon the jobs
} else {
// No new jobs now
sleep(30);
}
}
很好,但在某些情况下,这意味着在执行新作业之前可能会有30秒的延迟。由于这是一个守护程序脚本,我想我应该尝试使用钩子来捕获SIGUSR1信号来推动脚本
在这段代码中父进程结束的是什么?我知道没有看到他离开任何地方..。我的一位朋友说,父级没有结束/运行,代码只是在最后一个出口(0)处停止。如果是这样,那么是什么唤醒了父进程的睡眠呢?提前谢谢你
adghtesd
该方案的结果:
parent: I'm the parent
child: I'm the child
child: I'm exiting
parent: child process (PID=1919) exited with value 123
parent: exiting
test
我有一个脚本,在这个脚本中,我需要以一定的时间间隔运行特定的函数,而脚本的其余部分则正常执行。
例如..。我希望每小时获取一次磁盘数据,而其余的脚本每秒收集进程数据……
在这里我有一个基本的脚本来测试这一点,但它并不像预期的那样工作…
#!/bin/bash
function test () {
echo "This line should print all the time..."
}
function test2 () {
echo "This line should only print every 3 seconds..."
sleep 3
}
我试图用cond变量和信号量来比较产品消费者问题的实现。
使用cond变量的实现:
acquire(m); // Acquire this monitor's lock.
while (!p) { // While the condition/predicate/assertion that we are waiting for is not true...
wait(m, cv); // Wait on this monitor's lock and condition variable.
}
// ... Critical section of code goes
public class Main {
public static void main(String[] args) {
LinkedList<Th> myThreads = new LinkedList<>();
for (int i = 0; i < 100; i++) {
myThreads.add(new Th());
myThreads.get(i).start();
}
}
}
public class Th extends Thread
我需要保证代码的某些部分所需的时间。在一个强大的服务器上执行我的代码时,我得到了10个不同的结果。
我试着用time.time(),time.perf_counter(),time.perf_counter_ns(),time.process_time()和time.process_time_ns()来比较测量的时间。
import time
for _ in range(10):
start = time.perf_counter()
i = 0
while i < 100000:
i = i + 1
time.sleep(1)
我有一个C程序,它每分钟轮询一些硬件传感器一次,并将读数添加到数据库中。为了确保读数更接近分钟(而不是分钟+开销时间),程序创建一个孩子来做实际工作,然后在创建另一个孩子之前休眠60秒。
这就是我一直在尝试的方法:
while(1)
{
/* Create a child to do the work */
if((pid = fork()) < 0)
{
perror("fork");
exit(1);
}
else if(pid > 0)
{
/*parent c
当使用time.sleep()时,是否会阻止一个烧瓶请求?
我的一个Flask端点启动了一个长的处理子任务,在某些情况下,可以等待任务的完成并在相同的请求中返回结果,而不是异步地完成工作。
在这种情况下,我的Flask应用程序启动进程,然后等待它完成,然后返回结果。我在这里的问题是,当调用类似于(简化)的内容时:
while True:
if process_is_done():
break
time.sleep(1)
在完成之前,烧瓶会阻止该请求,还是允许其他请求在此期间到来?
我试着通过使用gdb和“反汇编”来调试它,但是它没有显示代码行,只显示了出现分段错误的函数。
代码如下:
它应该检查玩家是赢了(他是最后一个活着的),还是输了(他不在地图上了)。
int * win_lose_verif(int number_of_player, int your_player);
它是主循环,我需要它在玩家赢或输的时候停止。
int network_fn(int number_of_player, int your_player);
完整的代码:
int * win_lose_verif(int number_of_player,int your_player) {