我有一个小C服务器需要接受连接并分叉子进程.我需要的stderr
子进程的去一个已经存在的命名管道,将stdout
孩子转到stdout
父,和stdin
孩子的TP来自同一个地方作为stdin
母公司.
我最初的尝试涉及popen()
但我似乎永远不会得到我想要的.
最后,这个特定的解决方案只需要在Solaris中工作.谢谢.
编辑:更新了问题,希望能更准确地描绘我想要完成的事情.谢谢你对我耐心.
EDIT2:我还需要父进程获取子进程的返回值,然后对它做一些事情,如果这有任何区别.
我正在使用函数popen2(已在stackoverflow上的其他地方推荐)以编程方式创建一个必须在一段时间后再次被杀死的进程.popen2返回一个PID,我认为这个PID可以用来杀死进程.但是,它不会以这种方式工作.为了杀死它,我必须递增返回的PID,我不明白(见下面的代码)
当各种线程并行执行此操作时,可能会出现另一个问题.在这种情况下,我认为由于竞争条件,PID可能会有不同的差异.
所以我的问题是:如何在多线程场景中可靠地识别popen2创建的进程的PID?
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#define READ 0
#define WRITE 1
pid_t popen2(const char *command, int *infp, int *outfp) {
int p_stdin[2], p_stdout[2];
pid_t pid;
if (pipe(p_stdin) != 0 || pipe(p_stdout) != 0)
return -1;
pid = fork();
if (pid < 0)
return pid;
else if (pid == 0)
{
close(p_stdin[WRITE]);
dup2(p_stdin[READ], READ);
close(p_stdout[READ]);
dup2(p_stdout[WRITE], WRITE);
execl("/bin/sh", "sh", "-c", command, NULL);
perror("execl");
exit(1);
}
if (infp == NULL)
close(p_stdin[WRITE]);
else
*infp = p_stdin[WRITE];
if …
Run Code Online (Sandbox Code Playgroud) 我想通过 subprocess.Popen 从 python 执行一个外部程序。我想知道是否可以为通过命令执行的外部程序设置窗口的大小和位置?
我有一个小问题。我在 ubuntu 16.04 机器上,在 python 脚本中我想启动一个子进程,该子进程应在用户的主目录中启动。我尝试过:
subprocess.Popen('echo "Test"', cwd="~", shell=True,universal_newlines=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, executable="/bin/bash")
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时,我收到以下错误:
proc = subprocess.Popen('echo "test"', cwd="~", shell=True,universal_newlines=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, executable="/bin/bash")
File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: '~'
Run Code Online (Sandbox Code Playgroud)
我不确定我做错了什么,因为当您在 cd 命令中输入 ~ 时,它会将您切换到主目录。我希望有人能找到解决方案,解释为什么它不能以这种方式工作,以及在主目录中启动它的正确方法是什么。
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QProcess>
#include <QFile>
#include <QDebug>
#include <stdio.h>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
FILE* file1 = popen ("make", "r");
char buff[5122];
while(fgets(buff, sizeof(buff), file1)!=NULL)
{
qDebug() << "from here: " << buff;
}
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral ("qrc:/main.qml")));
return app.exec();
}
Run Code Online (Sandbox Code Playgroud)
使用make
命令输出是:
QML debugging is enabled. Only use this in a safe environment.
from here: make: Nothing to be done for
first'.`
使用ping
命令输出是:
QML debugging is enabled. Only use …
Run Code Online (Sandbox Code Playgroud) 我试图使用从类似问题获得的以下代码在Python中打印到终端:
cmd = 'test'
output = subprocess.Popen(cmd, stdout=subprocess.PIPE ).communicate()[0]
print(output)
Run Code Online (Sandbox Code Playgroud)
但是这样做会给我输出:
b''
Run Code Online (Sandbox Code Playgroud)
如何修复我的代码以正确输出到终端?