#include #include #include #include #include #include #include #include int do_poll_proc(int pid) { char path[64]; char bufa[2][256] = { }; int count; sprintf(path, "/proc/%d/stat", pid); for (count=0; count<10000; count++) { int procfd = open(path, O_RDONLY); if (procfd >= 0) { char *buf = bufa[count % 2]; read(procfd, buf, 255); close(procfd); if (memcmp(bufa[0], bufa[1], 255)) printf("%02d: %s", count, buf); } else { perror("open proc"); break; } } return 0; } int main(int argc, char *argv[]) { int ttyfd; int pid, cpid; int pdelay = 100; if (argc > 1) pdelay = atoi(argv[1]); pid = getpid(); cpid = fork(); switch (cpid) { case -1: perror("fork"); break; case 0: /* child */ do_poll_proc(pid); exit(0); default: /* parent */ usleep(pdelay); break; } ttyfd = open("/dev/tty", O_RDWR); if (ttyfd >= 0) { int ret = ioctl(ttyfd, TIOCNOTTY, (char *)0); if (ret) perror("ioctl"); printf("all is fine ...\n"); close(ttyfd); usleep(1000); exit(0); } exit(1); }