[8 进程控制]使用system函数执行命令

1 system介绍

在Unix系统中,system总是可用的。system接口如下:

#include <stdlib.h>

int system(const char* cmdstring);

system在其实现中调用了fork,exec和waitpid函数,因此有3种返回值:
(1)如果fork失败或waitpid返回除EINTR之外的出错,则system返回-1,且设置errno以指示错误类型。
(2)如果exec失败,则其返回值如同shell执行了exit(127)。
(3)如果fork,exec和waitpid都成功,则system的返回值是shell的终止状态。

2 使用system函数执行命令

如下的程序是system函数的一种实现。这个版本的system对信号没有进行处理。

#include    <sys/wait.h>
#include    <errno.h>
#include    <unistd.h>

int system(const char *cmdstring)    /* version without signal handling */
{
    pid_t    pid;
    int        status;

    if (cmdstring == NULL)
        return(1);        /* always a command processor with UNIX */
    if ((pid = fork()) < 0) {
        status = -1;    /* probably out of processes */
    } else if (pid == 0) {                /* child */
        execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);
        _exit(127);        /* execl error */
    } else {                            /* parent */
        while (waitpid(pid, &status, 0) < 0) {
            if (errno != EINTR) {
                status = -1; /* error other than EINTR from waitpid() */
                break;
            }
        }
    }

    return(status);
}

子进程会调用execl起一个新程序(/bin/sh)去执行命令,若execl正常退出,system的返回值是shell的终止状态;若execl异常,则子进程执行_exit(127)。
主进程等待子进程退出。
注意:子进程退出调用_exit而不是exit。这是为了防止任一标准I/O缓冲在子进程中被冲洗(这些缓冲会在fork中由父进程复制到子进程)。
用如下程序对system函数进程测试:

#include <stdlib.h>
#include <sys/wait.h>
#include <stdio.h>

void pr_exit(int status)
{
    if (WIFEXITED(status))
        printf("normal termination, exit status = %d\n",
                WEXITSTATUS(status));
    else if (WIFSIGNALED(status))
        printf("abnormal termination, signal number = %d%s\n",
                WTERMSIG(status),
#ifdef    WCOREDUMP
                WCOREDUMP(status) ? " (core file generated)" : "");
#else
                "");
#endif
    else if (WIFSTOPPED(status))
        printf("child stopped, signal number = %d\n",
                WSTOPSIG(status));
}

int main(void)
{
    int        status;

    if ((status = system("date")) < 0)
        printf("system() error");
    pr_exit(status);

    if ((status = system("nosuchcommand")) < 0)
        printf("system() error");
    pr_exit(status);

    if ((status = system("who; exit 44")) < 0)
        printf("system() error");
    pr_exit(status);

    exit(0);
}

输出:

2022年 05月 01日 星期日 21:08:15 CST
normal termination, exit status = 0
sh: 1: nosuchcommand: not found
normal termination, exit status = 127
xxx      tty7         2022-05-02 05:00 (:0)
yyy      tty8         2022-05-01 21:01 (:1)
normal termination, exit status = 44

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您可以使用subprocess模块中的Popen函数执行命令,并将参数"shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE"传递给它,以隐藏弹出的黑窗口。例如:subprocess.Popen('your_command', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, creationflags=x08000000)。 ### 回答2: 在Windows下,可以通过以下两种方法隐藏在Python中使用os.system函数执行的命令的弹出黑窗口。 方法一:使用os.startfile函数代替os.system函数执行命令。os.startfile函数可以在后台运行命令,不会显示黑窗口。以下是示例代码: ``` import os command = "your_command_here" os.startfile(command) ``` 方法二:使用subprocess模块中的Popen函数执行命令,并设置参数creationflags为CREATE_NO_WINDOW。这个参数可以在创建进程时隐藏子进程的窗口。以下是示例代码: ``` import subprocess command = "your_command_here" subprocess.Popen(command, creationflags=subprocess.CREATE_NO_WINDOW) ``` 需要注意的是,以上两种方法在执行某些命令时可能会有一些限制,尤其是涉及到与控制台交互的命令。如果遇到相关问题,可以尝试使用其他模块如pywin32等来解决。 ### 回答3: 在Windows下,使用Python的os.system函数执行命令时,可以通过创建一个新的进程来隐藏弹出的黑窗口。具体的方法是使用win32process模块的CreateProcess函数来替代os.system函数。 首先,需要导入相应的模块: ``` import win32process import win32con import win32gui import subprocess ``` 然后,定义一个函数来隐藏黑窗口: ``` def hide_console_window(): # 创建进程时的信息 startupinfo = win32process.STARTUPINFO() # 设置窗口显示模式为隐藏 startupinfo.dwFlags |= win32process.STARTF_USESHOWWINDOW startupinfo.wShowWindow = win32con.SW_HIDE # 执行命令,并隐藏窗口 subprocess.Popen('your_command', startupinfo=startupinfo) ``` 在这个函数中,可以通过修改dwFlags和wShowWindow来设置窗口的显示模式。使用SW_HIDE参数可以将窗口隐藏。 最后,调用这个函数执行命令并隐藏黑窗口: ``` hide_console_window() ``` 这样,在使用os.system函数执行命令时,就可以隐藏弹出的黑窗口了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值