所以我创建了一个信号处理程序来修改输出打印到屏幕的速度。输出通过打印到另一个打开的终端的过程来完成,同时仍然允许用户输入诸如'+'的输入以提高其读取的速度,' - '以降低速度。发送一个由信号改变的变量给exec在子进程中创建的进程[C]
代码看起来像这样。
static state* start_button(void)
{
state *destination;
pid_t pid;
pid = fork();
switch(pid)
{
case -1:
destination = &ready;
break;
case 0:
execl("./outputTerminal",
"outputTerminal",file_number,"/dev/ttys001",NULL);
break;
default:
destination = &going;
break;
}
return destination;
}
在去状态(going.c
)我的思维过程是创建易失性变量秒和纳秒,并使用这些变量来更新outputTerminal使用在像这样在一个时间读取线1线nanosleep()
功能下面的代码片段。这可能吗?这是一个家庭作业问题btw。我拥有所有功能,就像我们应该只需要弄清楚如何将由信号更改的变量发送到该过程一样。在设置信号处理程序之后,我试图在kill(baby_pid,SIGUSR1)
处进行处理,因为我保存了pid,但由于某种原因,它只是杀死了进程outputTerminal。
//going.c
volatile sig_atomic_t seconds;
volatile sig_atomic_t nanoseconds; //Update these in the going state
//Then pass them to the process outputTerminal like so
//outputTerminal.c
struct timespec tm1,tm2;
tm1.tv_sec = seconds;
tm2.tv_nsec = nanoseconds;
nanosleep(&tm1,&tm2);
这是我的信号处理
static void speed_handler(int signal)
{
long max_nano = 1000000000L;
long incrementer = 250000000L;
if(speed_control == 0)
{
if(nanoseconds == 0L && seconds > 0)
{
seconds -= 1;
nanoseconds = max_nano;
}
if(nanoseconds != 0L)
nanoseconds -= incrementer;
}
if(speed_control == 1)
{
nanoseconds += incrementer;
if(nanoseconds >= max_nano)
{
nanoseconds = 0L;
seconds += 1;
}
}
}
因此,我修改了它实际上假设的代码。只需创建一个孩子并使用execl。我们显然只是假设使用这种信号,这对我来说没有意义。 Sigqueue看起来好像很不错,但显然它不支持mac sierra – WizKiz
好吧,所以我解决我的问题的方法是将信号处理程序放在打印出另一个终端上的文本的过程中,然后使用我的父进程发送信号给它,我用两个信号来提高速度和降低速度。 – WizKiz
@WizKiz“古典”信号的一个问题是,人们不确定目标进程是否接收到信号(并且人们不确定如果信号快速连续发送多次会发生什么情况)。 – cnicutar