void dpr(node * u,int dep) { if(u == nil) return; h = max(dep,h); dpr(u->L,dep+1); ff(debug,"now:%d L:%d R:%d color:%d val:%d\n",u->val,u->L->val,u->R->val,u->color,u->val); dpr(u->R,dep+1); }
int yproc_pid_stat(struct yproc_pid_stat *stat, int pid) { int r, fd; char buf[4096]; // this is large enough if (unlikely(!stat)) return -EINVAL; r = snprintf(buf, sizeof(buf), "/proc/%d/stat", pid); if (unlikely(r >= sizeof(buf))) return -EINVAL; // This is ABSOLUTELY UNEXPECTED! if (unlikely(0 > (fd = open(buf, O_RDONLY)))) return -errno; // Read file contents into buffer if (unlikely(0 > (r = read(fd, buf, sizeof(buf) - 1)))) { r = -errno; goto close_fd; } buf[r] = 0; // trailing 0 dpr("raw string:%s", buf); r = sscanf(buf, "%d (%[^)]) %c %d %d %d", &stat->pid, stat->tcomm, &stat->state, &stat->ppid, &stat->pgid, &stat->sid); dpr("sscanf result: %d\n", r); if (6 != r) { // This is ABSOLUTELY UNEXPECTED! Kernel may be updated? r = -EINVAL; goto close_fd; } dpr("%d (%s) %c %d %d %d\n", stat->pid, stat->tcomm, stat->state, stat->ppid, stat->pgid, stat->sid); r = 0; // mark as success close_fd: close(fd); return r; }
int main (int argc, char **argv) { if(argc < 2) return -1; dpr("program started"); dpr("opening LEDs @ DEVICE"); dpr(DEVICE); s = get_sign(); if (s == -1) { printf("LEDs not found @ DEVICE. Failing...\n"); return -1; } tcgetattr(s, &options); cfsetispeed(&options, BAUD); cfsetospeed(&options, BAUD); options.c_cflag &= ~PARENB; options.c_cflag |= CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; options.c_cflag |= (CLOCAL | CREAD); tcsetattr(s, TCSANOW, &options); int i = 0; // Setup unsigned char r; unsigned char g; unsigned char b; for (i = 0 ; i < LEDCOUNT ; i++) { switch (i % 3) { case 0: r = 255; g = 0; b = 0; break; case 1: r = 0; g = 255; b = 0; break; case 2: r = 0; g = 0; b = 255; break; default: break; } colors[(i*3)] = r; colors[((i*3) + 1)] = g; colors[((i*3) + 2)] = b; i++; } // GO int runs = 0; int n = 0; while (runs < 10000) { char buff[1]; ssize_t rc = read(s, buff, 1); if (buff[0] == 'R') { printf("%c %c\n",buff[0], buff[0]); char rc = 0; //rc = (char)(256 * (rand() / (RAND_MAX + 1.0))); for (i = 0 ; i < (LEDCOUNT) ; i++) { int off = runs % LEDCOUNT; if (i != off) { r = colors[(i*3)]; g = colors[((i*3) + 1)]; b = colors[((i*3) + 2)]; } else { r = 255; g = 255; b = 255; } printf("LED %2d: %4d %4d %4d\n",i,r,g,b); p(r); p(g); p(b); } usleep(LINEDELAY); runs++; } } // Stop close(s); return 0; }