在Linux中,父过程如何将PTY应用于子过程?
这是父过程的代码:
#include <stdlib.h>
#include <fcntl.h>
#include <pty.h>
#include <stdio.h>
#include <utmp.h>
#include <sys/ioctl.h>
#include <unistd.h>
int main() {
int err = 0, len = 0;
int master, aslave;
char name[BUFSIZ], buffer[BUFSIZ];
FILE *child;
err = openpty(&master, &aslave, &name[0], NULL, NULL);
if (err == -1) {
perror("openpty faild.");
return EXIT_FAILURE;
}
fprintf(stdout, "pty name: %s\n", name);
child = popen("./child", "r");
if (!child) {
perror("popen faild");
return EXIT_FAILURE;
}
len = read(master, &buffer[0], BUFSIZ);
if (len == 0) {
fprintf(stdout, "Unable to read output from subprocess.");
return EXIT_FAILURE;
}
fprintf(stdout, "child message: %s\n", buffer);
return 0;
}
以下是子过程的代码:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
fprintf(stdout, "%s\n", isatty(fileno(stdout)) ? "true": "false");
return EXIT_SUCCESS;
}
我知道,要在子过程中通过ISATTY检查,有必要通过PTY与子过程进行交互,但是需要是完毕?
尽管这可能没有应用程序方案,但我仍然想知道PTY的用法。
Here is the code for the parent process:
#include <stdlib.h>
#include <fcntl.h>
#include <pty.h>
#include <stdio.h>
#include <utmp.h>
#include <sys/ioctl.h>
#include <unistd.h>
int main() {
int err = 0, len = 0;
int master, aslave;
char name[BUFSIZ], buffer[BUFSIZ];
FILE *child;
err = openpty(&master, &aslave, &name[0], NULL, NULL);
if (err == -1) {
perror("openpty faild.");
return EXIT_FAILURE;
}
fprintf(stdout, "pty name: %s\n", name);
child = popen("./child", "r");
if (!child) {
perror("popen faild");
return EXIT_FAILURE;
}
len = read(master, &buffer[0], BUFSIZ);
if (len == 0) {
fprintf(stdout, "Unable to read output from subprocess.");
return EXIT_FAILURE;
}
fprintf(stdout, "child message: %s\n", buffer);
return 0;
}
Here is the code for the child process:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
fprintf(stdout, "%s\n", isatty(fileno(stdout)) ? "true": "false");
return EXIT_SUCCESS;
}
I know that in order to pass the isatty check in the child process, it is necessary to interact with the child process through pty, but what needs to be done?
While this may not have an application scenario, I would still like to know the usage of pty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能您可以使用
posix_openpt
+fork
在主和从属过程之间进行互动以下是一个例子
Probably you could use
posix_openpt
+fork
to have interaction between master and slave processesFollowing is an example