简单linux小程序,【C语言】【linux】几个简单的小程序

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

程序例子三:进程信息获取+用户信息获取+程序内存访问信息获取。

各个函数是干嘛的就不做注释了,具体见printf部分。

#include

#include

#include

#include

#include

#include

#include

int main(void){

char*login;

struct passwd* pentry;

struct group *grp;

struct rusage usage;

char **members;

//get process id

printf("********************进程信息获取****************\n");

printf("->进程ID:%d\n",getpid());

printf("->父进程ID:%d\n",getppid());

printf("->真实用户ID:%d\n",getuid());

printf("->有效用户ID:%d\n",geteuid());

printf("->真实用户组ID:%d\n",getgid());

printf("->有效用户组ID:%d\n",getegid());

//get user infomation

printf("********************用户信息获取****************\n");

if((login=getlogin())==NULL){

printf("get user name Error!\n");

}else{

if((pentry=getpwnam(login))==NULL){

printf("get password Error!\n");

}else{

printf("->user name:%s\n",pentry->pw_name);

//printf("->user passwd:%s\n",pentry->pw_passwd);

printf("->UID:%d\n",pentry->pw_uid);

printf("->GID:%d\n",pentry->pw_gid);

printf("->gecos:%s\n",pentry->pw_gecos);

printf("->home dir:%s\n",pentry->pw_dir);

printf("->shell : %s\n",pentry->pw_shell);

grp = getgrgid (pentry->pw_gid);

if(!grp){printf("Couldn't find out about group %d.\n",(int)pentry->pw_gid);}

else{

printf ("->用户所在用户组:%s (%d)\n",grp->gr_name, (int) (pentry->pw_gid));

printf ("->该用户组成员还有:\n");

members = grp->gr_mem;

while (*members){

printf ("\t%s\n",*(members));

members++;

}//end of whule

}//end of if !grp

}//end of getpwnam

}//end of getlogin

printf("********************程序内存访问信息获取****************\n");

if((getrusage(RUSAGE_SELF,&usage))==-1){

printf("getrusage Error!\n ");

}else{

printf("获取当前进程的资源利用情况\n");

printf("->执行用户模式代码所用时间:%ld\n",usage.ru_utime.tv_usec);

printf("->执行内核模式代码所用时间:%ld\n",usage.ru_stime.tv_usec);

printf("->内存访问中没有引起磁盘读写的次数:%ld\n",usage.ru_minflt);

printf("->内存访问中引起磁盘读写的次数:%ld\n",usage.ru_majflt);

printf("->从磁盘读取的内存页数:%ld\n",usage.ru_nswap);

}//end of getrusage

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值