Skip to content

Instantly share code, notes, and snippets.

@ob
Created August 4, 2020 05:55
Show Gist options
  • Save ob/b83d004634b9f2bc445a09de750a55bf to your computer and use it in GitHub Desktop.
Save ob/b83d004634b9f2bc445a09de750a55bf to your computer and use it in GitHub Desktop.
/* gcc test.c -lutil */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pty.h>
static int
do_fork(void)
{
int fd = -1;
pid_t pid;
if ((pid = forkpty(&fd, NULL, NULL, NULL)) < 0)
perror("forkpty");
else if (!pid)
{
printf ("0123456789\n");
fflush(stdout);
/* #### Uncommenting this makes it work! */
// sleep(20);
exit (0);
}
return fd;
}
int
main (int argc, char **argv)
{
char s[1024];
int n;
int fd = do_fork();
/* On 10.4, this prints the whole 10 character line, 1 char per second.
On 10.5, it prints 1 character and stops.
*/
do {
n = read (fd, s, 1);
if (n > 0) fprintf (stderr, "%c", *s);
sleep (1);
} while (n > 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment