未加星标

process listing consistency

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

POSIX specifies that there is a ps utility to list processes, although it doesn’t describe how the command is implemented. In fact, it’s not possible to implement ps using only POSIX interfaces. However it’s implemented, it’s unlikely to use double buffering, which means on a sufficiently busy system, the results may be inconsistent. If lots of processes are being created and exited while ps runs, some of the output may be “before” and some “after”. Much like a game without vsync.

In order to test for inconsistency, we need to create lots of processes, but in a predictable way. Then we run ps over and over, looking for discrepancies. Enter the chicken and the egg.

#include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <string.h> #include <signal.h> int main(int argc, char **argv) { int fd[2]; char ch; signal(SIGPIPE, SIG_IGN); pipe(fd); switch (fork()) { case -1: printf("fork failed!\n"); exit(1); case 0: break; default: _exit(0); } close(fd[1]); read(fd[0], &ch, 1); switch (argv[0][0]) { case 'e': execl("./chicken", "chicken", NULL); case 'c': default: execl("./egg", "egg", NULL); } return 1; }

We compile this code and copy it to chicken and egg . Then we run it. (A word of caution: neither the chicken nor the egg are particularly well behaved guests. Don’t try this at work.)

Each process will create a pipe (pair of file descriptors) and fork a child. The parent immediately exits. The child closes the write side of the pipe. Then we try reading from the pipe. This will block until all the writers are closed, ensuring the parent has exited. Watching for a closed pipe is a good way for a group of processes to monitor each other when SIGCHLD won’t work, such as when the child wants notification of the parent’s exit.

Satisfied that the parent has exited, the child grows up. Eggs turn into chickens, and chickens lay eggs. We might try running ps a few times to catch a glimpse of what’s happening. Of course, by the time we see the listing, the chicken has already forked off thousands of generations.

This seems like a lot of work to accomplish nothing, although it gives an opportunity to watch a changing system. At any given time, there may be an egg or a chicken, but not both. Immediately after the fork, there may be two eggs or two chickens, but at no time will there ever be a chicken and an egg. Also, for completeness, there will always be a chicken or an egg, never none.

So we run ps | egrep "chicken|egg" in a loop, watching for inconsistencies. If we ever see a process listing with both a chicken and an egg, or one with neither, that means ps is stitching together information. In the first case, ps read some info, saw a chicken, then read some more info, but in the meantime the chicken forked off an egg, which gets noticed in the second read.

Long ago, OpenBSD ps used the kvm interface to read kernel memory to create the process listing. Of course, there’s no guarantee that while ps is running, nothing changes. The system doesn’t stop working while ps is poking about. The kvm interface was replaced by sysctl , offering a few advantages. Userland is no longer coupled to kernel data structures, and the operation can be performed atomically. ps makes a single call to sysctl with KERN_PROC_ALL and the kernel returns info about every process on the system.

Internally, sysctl has a few tricks to keep it fast. The output memory region that will be written to is wired down to prevent page faults. Thus sysctl can iterate over the entire process list, copying out information to ps, without blocking. If we prevent processes from forking or exiting during this time, we get a consistent snapshot. The snapshot may be stale, but it will never show us a viewpoint that never happened.

Is there a way to trick ps on OpenBSD? Not everything is consistent. There’s a separate sysctl, KERN_PROC_ARGV, that reads the command line arguments for a process, but it only works on one process at a time. Processes can modify their own argv at any time. We can cook up a similar test case for this.

#include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <string.h> #include <signal.h> char *myname; pid_t parent, child; int state; void catch(int signum) { switch (state) { case 0: state = 1; memcpy(myname, "state 1", 8); break; case 1: state = 2; memcpy(myname, "state 2", 8); break; case 2: state = 0; memcpy(myname, "state 0", 8); break; } if (child) kill(child, SIGUSR1); else kill(parent, SIGUSR1); } int main(int argc, char **argv) { myname = argv[0]; if (strlen(myname) < 7) { printf("need a bigger name\n"); exit(1); } signal(SIGUSR1, catch); parent = getpid(); switch ((child = fork())) { case -1: printf("fork failed!\n"); exit(1); case 0: state = 1; break; } if (child) { printf("parent %d\nchild %d\n", parent, child); kill(child, SIGUSR1); } while (1) sleep(10); return 1; }

Just two processes, a parent and child, this time. All we want to do is sleep, but instead we’re going to be spending a lot of time playing pass the hot signal. Every time a signal is received, we advance a little state machine. State 0, state 1, state 2, back to zero. Update our name. Then we signal the other process.

What are the invariants? The parent starts in state 0, the child in state 1. The child receives the first signal, advancing to state 2. Then it signals the parent, advancing it to state 1, before signalling back and advancing the child to state 0. At no point will the two processes ever be in the same state. The child will always be a step or two ahead of the parent.

When running ps, sometimes we’ll see both processes in the same state, because ps read argv from one process then the other, but signals were flying all the while.

Consistency is overrated anyway.

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

分页:12
转载请注明
本文标题:process listing consistency
本站链接:http://www.codesec.net/view/479875.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(51)