[英]Sending a variable changed by a signal to a process created by exec in the child process.[C]
So I'm creating a signal handler to modify the speed at which output is printed to the screen. 因此,我正在创建一个信号处理程序,以修改将输出打印到屏幕上的速度。 The output is done by a process that prints to another terminal open while still allowing the user to put in input such as '+' to increase the speed its read and '-' to decrease the speed.
输出通过打印到另一个打开的终端的过程来完成,同时仍允许用户输入输入,例如“ +”以提高其读取速度,而“-”以降低速度。
The code looks something like this. 该代码看起来像这样。
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;
}
In the going state( going.c
) my thought process was to create volatile variables seconds and nanoseconds and use those variables to update the nanosleep()
function that outputTerminal is using to read the lines 1 line at a time like so in the code snippet below. 在运行状态(
going.c
)中,我的思维过程是创建易失性变量(秒和纳秒),并使用这些变量更新outputTerminal用于一次读取第1行的nanosleep()
函数,如代码片段中所示下面。 Is this possible? 这可能吗? This is a homework question btw.
顺便说一句,这是一个作业问题。 I have all the functions working like they are supposed to I just need to figure out how to send the variables changed by the signal over to that process.
我的所有功能都按预期运行,我只需要弄清楚如何将信号更改的变量发送到该进程即可。 I tried doing
kill(baby_pid,SIGUSR1)
in the going state after I set the signal handler because I saved the pid but that just kills the process outputTerminal for some reason. 设置信号处理程序后
kill(baby_pid,SIGUSR1)
我尝试在进行中的状态下执行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);
This is my signal handler 这是我的信号处理器
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;
}
}
}
What you're looking for is called IPC, short for Inter-process Communication and there are many ways to do what you want. 您正在寻找的被称为IPC,它是进程间通信的缩写,并且有很多方法可以实现您想要的。 Your approach doesn't work because your processes have distinct address spaces so changing a variable in one has no effect on the other.
您的方法行不通,因为您的进程具有不同的地址空间,因此更改一个变量不会影响另一个变量。
A few ways in which you could communicate information from one process to another: 可以将信息从一个过程传递到另一个过程的几种方法:
mmap
with MAP_SHARED
) mmap
与MAP_SHARED
) sigqueue
and sigval
. Or maybe check this old answer https://stackoverflow.com/a/7270506 ) sigqueue
和sigval
。或者检查这个旧答案https://stackoverflow.com/a/7270506 ) There are quite a few more mechanisms but this should get you started. 还有很多其他机制,但这应该可以帮助您入门。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.