如何同步调用shell命令?

How to call shell command synchronously?

我正在尝试教 dwm 在每个标签(www、文件、音乐...)中打开适当的应用程序。在dwm.c中有一个叫做view的函数,负责标签的切换。

void
view(const Arg *arg)
{
    if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
        return;
    /* toggle sel tagset */
    selmon->seltags ^= 1;
    if (arg->ui & TAGMASK)
        selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;

    // user specific edit
    prepareTag(arg->ui);

    focus(NULL);
    arrange(selmon);
}

我在调用 prepareTag 的地方添加了一行。这个函数逻辑简单,除了一些验证(应用程序是否已经打开?它是什么标签?)和应用程序本身生成之外什么都不做。

void
spawn(const Arg *arg)
{
    if (arg->v == dmenucmd)
        dmenumon[0] = '0' + selmon->num;
    if (fork() == 0) {
        if (dpy)
            close(ConnectionNumber(dpy));
        setsid();
        execvp(((char **)arg->v)[0], (char **)arg->v);
        fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
        perror(" failed");
        exit(EXIT_SUCCESS);
    }
}

它有效,但代码是异步运行的。标签已更改,我看到了我的墙纸,然后在约 20-50 毫秒后应用程序启动。它会导致明显的闪烁。问题是我从未使用过 C,也不知道代码异步工作的原因。我已经尝试 system 函数而不是内置 spawn 但 dwm 不会捕获以这种方式打开的应用程序。我可能会使用密钥绑定器和一些 BASHing,但方式很脏。更不用说,我希望能够使用鼠标按钮更改标签。

以防有人需要代码库。

git clone https://git.suckless.org/dwm

如果您阅读 fork() 的手册,您会发现它创建了 运行 进程的副本。

在 fork 之后,这两个进程相互独立,并且可以按任何顺序进行调度。这是您看到的异步行为。

要获得同步行为,您的父进程需要等到分叉进程完成(退出)。这是使用 wait() 系统调用实现的。

您可以将 spawn 函数修改为 -

void
spawn(const Arg *arg) 
{
    if (arg->v == dmenucmd)
        dmenumon[0] = '0' + selmon->num;
    if (fork() == 0) {
        if (dpy)
            close(ConnectionNumber(dpy));
        setsid();
        execvp(((char **)arg->v)[0], (char **)arg->v);
        fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
        perror(" failed");
        exit(EXIT_SUCCESS);
    } else { // fork returns a non zero pid in the parent process. So the else branch will be taken only in the parent. 
        wait(NULL); // Wait for the child process to change state. In this case exit
    }
}