2012-03-07 119 views
2

我有一个进程(读写终端类型),已经由后台进程执行。我可以用ps看到它。 试图将其带到前台,这是我尝试:将进程带到前台

int main() 

{ 

    FILE* fd = popen("pidof my_program","r"); 

    // ... 
    // Some code to get the pid of my_program as mpid 
    //... 

    printf("pid of my_program is %d",mpid); 
    signal(SIGTTOU, SIG_IGN); 
    setpgid(mpid,0); // Set program group id to pid of process 
    tcsetpgrp(0,mpid); // Give it terminal stdin access 
    tcsetpgrp(1,mpid); // Give it terminal stdout access 
    return 0; 
} 

它不工作,虽然。有人可以帮助我吗? 谢谢。

回答

2

你可以通过调用shell命令fg并将其传递给pid(adviced)来实现“软”方式。

如果你想它的代码,这是FG/BG是如何编码为庆典(不要不要不要):

static int 
fg_bg (list, foreground) 
    WORD_LIST *list; 
    int foreground; 
{ 
    sigset_t set, oset; 
    int job, status, old_async_pid; 
    JOB *j; 

    BLOCK_CHILD (set, oset); 
    job = get_job_spec (list); 

    if (INVALID_JOB (job)) 
    { 
     if (job != DUP_JOB) 
    sh_badjob (list ? list->word->word : _("current")); 

     goto failure; 
    } 

    j = get_job_by_jid (job); 
    /* Or if j->pgrp == shell_pgrp. */ 
    if (IS_JOBCONTROL (job) == 0) 
    { 
     builtin_error (_("job %d started without job control"), job + 1); 
     goto failure; 
    } 

    if (foreground == 0) 
    { 
     old_async_pid = last_asynchronous_pid; 
     last_asynchronous_pid = j->pgrp; /* As per Posix.2 5.4.2 */ 
    } 

    status = start_job (job, foreground); 

    if (status >= 0) 
    { 
    /* win: */ 
     UNBLOCK_CHILD (oset); 
     return (foreground ? status : EXECUTION_SUCCESS); 
    } 
    else 
    { 
     if (foreground == 0) 
    last_asynchronous_pid = old_async_pid; 

    failure: 
     UNBLOCK_CHILD (oset); 
     return (EXECUTION_FAILURE); 
    } 
} 
+0

但我在嵌入式Linux机器上运行,我只有busybox二进制文件,我没有fg,bg或可用的作业。我该怎么做呢? – Bornfree 2012-03-12 07:04:29

+0

我可以编译上面的代码,就完成了。 – vulkanino 2012-03-13 08:24:31

0

当你有一个过程,是在后台或暂停,您可以使用 命令fg命令将其移动到前台。默认情况下,最近暂停或移动到后台的进程移动到前台的 。你也可以指定它使用哪个pid来使它成为前景。

+0

但我在嵌入式Linux机器上运行,我只有busybox二进制文件,我没有fg,bg或可用的作业。我该怎么做呢? – Bornfree 2012-03-12 07:05:29