Linux系统编程中的终端操作与进程间通信
一、创建伪终端(PTY)程序
1.1 准备工作
要创建一个PTY程序,你需要GCC编译器、Make工具和screen程序。
1.2 编写代码
以下是创建PTY程序的详细步骤及代码:
1. 创建一个名为 my-pty.c 的文件,并添加以下代码:
#define _XOPEN_SOURCE 600
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
char rxbuf[1];
char txbuf[3];
int master; /* for the pts master fd */
int c; /* to catch read's return value */
master = posix_openpt(O_RDWR);
grantpt(master);
unlockpt(master);
printf("Slave: %s\n", ptsname(master));
while(1) /* main loop */
{
c = read(master, rxbuf, 1);
if (c == 1)
{
超级会员免费看
订阅专栏 解锁全文
1270

被折叠的 条评论
为什么被折叠?



