Re: [Patch] Fix a race condition in pty.c

From: Andrew Morton
Date: Fri Dec 17 2004 - 03:49:50 EST

"Zou, Nanhai" <nanhai.zou@xxxxxxxxx> wrote:
> <<pty_close-race-fix.patch>> There is a race condition int pty.c
> when pty_close wakes up waiter on its pair device before set
> It is possible on SMP or preempt kernel, waiter wakes up too early that
> it will not get TTY_OTHER_CLOSED flag then fall into sleep again.
> Lu hong jiu report this bug will hang some expect scripts on SMP
> machines.

The patch looks good (apart from the application/octet-stream encoding. grr.)

But on my test box it still doesn't fix the testcase which hj attached to
bug 3894:

select (3) returns: 1
Read -1 characters.
errno: Input/output error
select (3) returns: 1
Read -1 characters.
errno: Input/output error

In fact the same happens on a non-preempt uniprocessor kernel, so I must be
seeing something different.

#include <sys/types.h>
#include <sys/select.h>
#include <stdio.h>
#include <unistd.h>
#include <pty.h>

main ()
pid_t pid;
int fd;
char slave_name [20];
char buffer [1000];
int count;
fd_set readfds;
int ret;

pid = forkpty (&fd, slave_name, NULL, NULL);
switch (pid)
case 0: /* Child process. */
if (execlp ("true", "true", NULL))
perror ("execlp");
return 1;

default: /* Parent process. */

FD_ZERO (&readfds);
FD_SET (fd, &readfds);
ret = select (1024, &readfds, NULL, NULL, NULL);
printf ("select (%d) returns: %d\n", fd, ret);
if (ret < 0)
perror ("select");
else if (FD_ISSET (fd, &readfds))
count = read (fd, buffer, 999);
printf ("Read %d characters.\n", count);
return 0;

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at
Please read the FAQ at