如何用C遍历unix/linux下的所有进程,取得PID,名称等等
1个回答
2016-12-26
展开全部
用管道:
通过fgets(buf, n, ptr)buf就可以得到命令“ps -ef"一样的信息,
读帮助”man popen":
char *cmd = "ps -ef";
FILE *ptr;
if ((ptr = popen(cmd, "r")) != NULL)
while (fgets(buf, n, ptr) != NULL)
(void) printf("%s ",buf);
UID PID PPID C STIME TTY TIME CMD
root 0 0 0 Sep-30 ? 00:00:01 sched
root 1 0 0 Sep-30 ? 00:00:06 /etc/init -a
root 2 0 0 Sep-30 ? 00:00:00 vhand
root 3 0 0 Sep-30 ? 00:00:27 bdflush
root 4 0 0 Sep-30 ? 00:00:00 kmdaemon
root 5 1 0 Sep-30 ? 00:00:50 htepi_daemon /
root 6 0 0 Sep-30 ? 00:00:00 strd
root 2941 1 0 Oct-08 tty01 00:00:00 /bin/login ccb
root 43 1 0 Oct-08 ? 00:00:02 /etc/syslogd
root 47 1 0 Oct-08 ? 00:00:00 /etc/ifor_pmd
root 48 47 0 Oct-08 ? 00:00:13 /etc/ifor_pmd
root 36 1 0 Oct-08 ? 00:00:00
通过fgets(buf, n, ptr)buf就可以得到命令“ps -ef"一样的信息,
读帮助”man popen":
char *cmd = "ps -ef";
FILE *ptr;
if ((ptr = popen(cmd, "r")) != NULL)
while (fgets(buf, n, ptr) != NULL)
(void) printf("%s ",buf);
UID PID PPID C STIME TTY TIME CMD
root 0 0 0 Sep-30 ? 00:00:01 sched
root 1 0 0 Sep-30 ? 00:00:06 /etc/init -a
root 2 0 0 Sep-30 ? 00:00:00 vhand
root 3 0 0 Sep-30 ? 00:00:27 bdflush
root 4 0 0 Sep-30 ? 00:00:00 kmdaemon
root 5 1 0 Sep-30 ? 00:00:50 htepi_daemon /
root 6 0 0 Sep-30 ? 00:00:00 strd
root 2941 1 0 Oct-08 tty01 00:00:00 /bin/login ccb
root 43 1 0 Oct-08 ? 00:00:02 /etc/syslogd
root 47 1 0 Oct-08 ? 00:00:00 /etc/ifor_pmd
root 48 47 0 Oct-08 ? 00:00:13 /etc/ifor_pmd
root 36 1 0 Oct-08 ? 00:00:00
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询