如何用C遍历unixlinux下的所有进程,取得PID,名称等等

如何用C遍历unixlinux下的所有进程,取得PID,名称等等,第1张

用管道:

通过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 CSTIME TTYTIME 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 tty0100:00:00 /bin/login ccb

root43 1 0 Oct-08 ?00:00:02 /etc/syslogd

root47 1 0 Oct-08 ?00:00:00 /etc/ifor_pmd

root4847 0 Oct-08 ?00:00:13 /etc/ifor_pmd

root36 1 0 Oct-08 ?00:00:00

void PreOrder(BiTree T)//先序

{

if(T!=NULL)

{

printf("%c",T->data)

PreOrder(T->lchild)

PreOrder(T->rchild)

}

}

void InOrder(BiTree T)//中序

{

if(T!=NULL)

{

InOrder(T->lchild)

printf("%c",T->data)

InOrder(T->rchild)

}

}

void PostOrder(BiTree T)//后序

{

if(T!=NULL)

{

PostOrder(T->lchild)

PostOrder(T->rchild)

printf("%c",T->data)

}

}

全部程序

void PreOrder(BiTree T)//先序

{

if(T!=NULL)

{

printf("%c",T->data)

PreOrder(T->lchild)

PreOrder(T->rchild)

}

}

void InOrder(BiTree T)//中序

{

if(T!=NULL)

{

InOrder(T->lchild)

printf("%c",T->data)

InOrder(T->rchild)

}

}

void PostOrder(BiTree T)//后序

{

if(T!=NULL)

{

PostOrder(T->lchild)

PostOrder(T->rchild)

printf("%c",T->data)

}

}

图的遍历是指按某条搜索路径访问图中每个结点,使得每个结点均被访问一次,而且仅被访问一次。图的遍历有深度遍历算法和广度遍历算法,最近阿杰做了关于图的遍历的算法,下面是图的遍历深度优先的算法(C语言程序):

#include<stdio.h>

#include<malloc.h>

#define MaxVertexNum 5

#define m 5

#define TRUE 1

#define NULL 0

typedef struct node

{

int adjvex

struct node *next

}JD

typedef struct EdgeNode

{

int vexdata

JD *firstarc

}TD

typedef struct

{

TD ag[m]

int n

}ALGRAPH

void DFS(ALGRAPH *G,int i)

{

JD *p

int visited[80]

printf("visit vertex:%d->",G->ag[i].vexdata)

visited[i]=1

p=G->ag[i].firstarc

while(p)

{

if (!visited[p->adjvex])

DFS(G,p->adjvex)

p=p->next

}

}

void creat(ALGRAPH *G)

{

int i,m1,j

JD *p,*p1

printf("please input the number of graph\n")

scanf("%d",&G->n)

for(i=0i<G->ni++)

{

printf("please input the info of node %d",i)

scanf("%d",&G->ag[i].vexdata)

printf("please input the number of arcs which adj to %d",i)

scanf("%d",&m1)

printf("please input the adjvex position of the first arc\n")

p=(JD *)malloc(sizeof(JD))

scanf("%d",&p->adjvex)

p->next=NULL

G->ag[i].firstarc=p

p1=p

for(j=2 j<=m1j++)

{

printf("please input the position of the next arc vexdata\n")

p=(JD *)malloc(sizeof(JD))

scanf("%d",&p->adjvex)

p->next=NULL

p1->next=p

p1=p

}

}

}

int visited[MaxVertexNum]

void DFSTraverse(ALGRAPH *G)

{

int i

for(i=0i<G->ni++)

visited[i]=0

for(i=0i<G->ni++)

if(!visited[i])

DFS(G,i)

}

int main()

{

ALGRAPH *G

printf("下面以临接表存储一个图;\n")

creat(G)

printf("下面以深度优先遍历该图 \n")

DFSTraverse(G)

getchar()

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12052810.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-20
下一篇2023-05-20

发表评论

登录后才能评论

评论列表(0条)

    保存