系统和进程相关信息

查看进程号为1的进程信息

[root@bogon code]# cat /proc/1/status   //proc目录下的数字就是进程号
]Name:  systemd
State:  S (sleeping)
Tgid:   1
Ngid:   0
Pid:    1
PPid:   0
TracerPid:  0
Uid:    0   0   0   0
Gid:    0   0   0   0
FDSize: 64
Groups: 
VmPeak:   193628 kB
VmSize:   128092 kB
VmLck:         0 kB
VmPin:         0 kB
VmHWM:     40592 kB
VmRSS:      6704 kB
RssAnon:        2756 kB
RssFile:        3948 kB
RssShmem:          0 kB
VmData:    84492 kB
VmStk:       136 kB
VmExe:      1296 kB
VmLib:      3636 kB
VmPTE:       116 kB
VmSwap:        0 kB
Threads:    1
SigQ:   0/3791
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 7be3c0fe28014a03
SigIgn: 0000000000001000
SigCgt: 00000001800004ec
CapInh: 0000000000000000
CapPrm: 0000001fffffffff
CapEff: 0000001fffffffff
CapBnd: 0000001fffffffff
CapAmb: 0000000000000000
Seccomp:    0
Cpus_allowed:   ffffffff,ffffffff,ffffffff,ffffffff
Cpus_allowed_list:  0-127
Mems_allowed:   00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
Mems_allowed_list:  0
voluntary_ctxt_switches:    3129
nonvoluntary_ctxt_switches: 4021
[root@bogon code]# ]

proc/PID下的列表内容
系统和进程相关信息_系统编程

如何在程序中运行shell命令
与execve()不同,system()会继续执行该函数

[root@bogon code]# cat d.c
#include<stdio.h>
int main()
{
    system("ls /root;echo 'echo linux'");
    printf("still running\n");
    return 0;
}

[root@bogon code]# gcc d.c
[root@bogon code]# ./a.out
anaconda-ks.cfg  Documents         Music        Public
code         Downloads         original-ks.cfg  Templates
Desktop      initial-setup-ks.cfg  Pictures     Videos
echo linux
still running
[root@bogon code]# 

获取系统相关信息

[root@bogon code]# cat uname.c 
#include<stdio.h>
#include<error.h>
#include<sys/utsname.h>
int main()
{
    struct utsname uts;//utsname结构体的属性主要有下面几个
    if((uname(&uts))==-1)
        perror("uname fail");
    printf("nodename:%s\n",uts.nodename);//网络节点名称
    printf("release:%s\n",uts.release);//内核版本
    printf("version:%s\n",uts.version);//系统版本
    printf("machine:%s\n",uts.machine);//系统位数
    printf("sysname:%s\n",uts.sysname);//系统名称
    return 0;
}
[root@bogon code]# gcc uname.c 
[root@bogon code]# ./a.out
nodename:bogon
release:3.10.0-514.el7.x86_64
version:#1 SMP Tue Nov 22 16:42:41 UTC 2016
machine:x86_64
sysname:Linux
[root@bogon code]#