Question

I have to create a new pair of tty (master and slave) without using forkpty().

In the man of pts(4), it is written that :

When a process opens /dev/ptmx, it gets a file descriptor for a pseudo-terminal master (PTM), and a pseudo-terminal slave (PTS) device is created in the /dev/pts directory.

With a little program in C, I open /dev/ptmx like that :

open("/dev/ptmx", O_RDWR);

But there is no new pty created in /dev/pts/.

Was it helpful?

Solution

To actually create a usable pty pair, you must also call grantpt(3) and unlockpt(3) on the fd returned by the open call. Its not well-defined exactly where in that process the actual slave pty file node in the filesystem is created -- some systems (those where /dev/pts is a special filesystem, usually) will create it on the open, while others will create it as part of the grantpt or unlockpt call. Its also not guarenteed that the slave will be in /dev/pts -- it might be somewhere else -- so you need to call ptsname(3) to find out where it is.

It also may be slightly more portable to call posix_openpt(3) rather than open directly.

OTHER TIPS

Here is a good tutorial on the topic: Using pseudo-terminals to control interactive programs, pty, pdip

Particularly, look at the sample source at the middle of the page, under title “Inter-process communication through a pseudo-terminal”. That's an example of a process which fork it‑self, then the two processes communicate each‑others via a PTY the parent process priorly opened.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top