将由信号更改的变量发送到由子进程中的exec创建的进程。[C]

时间:2017-02-20 19:58:39

标签: c signals fork exec

所以我正在创建一个信号处理程序来修改输出打印到屏幕的速度。输出由打印到另一个终端打开的进程完成,同时仍允许用户输入诸如“+”之类的输入以提高其读取速度,并使用“ - ”来降低速度。

代码看起来像这样。

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)中,我的思考过程是创建几秒和几纳秒的volatile变量,并使用这些变量来更新outputTerminal用来读取1行的nanosleep()函数。这样的时间在下面的代码片段中。这可能吗?这是一个家庭作业问题顺便说一下。我所有的功能都像他们应该的那样工作我只需要弄清楚如何将信号改变的变量发送到该过程。在我设置信号处理程序之后,我尝试在进入状态中执行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;                                                      
        }                                                                      
    }                                                                          
}

1 个答案:

答案 0 :(得分:0)

您正在寻找的是IPC,即进程间通信的简称,有很多方法可以满足您的需求。 你的方法不起作用,因为你的进程有不同的地址空间,所以改变一个变量对另一个没有影响。

您可以通过几种方式将信息从一个流程传递到另一个流程:

还有更多机制,但这应该让你开始。