일반 시스템 정보

#include <sys/utsname.h>

int uname(struct utsname *uname);

현재의 호스트와 운영체제에 관한 정보를 알아낸다. 커널에 대한 이름과 버젼정보등을 얻어온다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <sys/utsname.h>
#include <sys/param.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
    struct utsname name;
    char hostname[1024];
 
    uname(&name);
 
    printf("sysname : %s\n",name.sysname);
    printf("nodename: %s\n",name.nodename);
    printf("release: %s\n",name.release);
    printf("version: %s\n",name.version);
    printf("machine: %s\n",name.machine);
 
    gethostname(hostname,MAXPATHLEN);
    printf("hostname : %s\n",hostname);
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

#include <unistd.h>

long sysinfo(char *command, char *buf, long count);

전체적인 시스템 통계 정보를 가져오기 위해 사용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <sys/sysinfo.h>
 
int main()
{
    struct sysinfo info;
    sysinfo(&info);
 
    printf("load: %d %d %d \n",info.loads[0],info.loads[1],info.loads[2]);
 
    printf("mem : %d %d %d\n",info.totalram,info.freeram,info.bufferram);
 
 
    return 0;
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

시스템 자원 제한 

#include <unistd.h>

long sysconf(int name);

파일과 관련 없는 사항을 알려주며, _SC_XXX를 인자로 사용

long pathconf(const char *pathname, int name);

long fpathconf(int filedes, int name);

파일과 관련 있는 사항을 알려 주며, _PC_XXX 를 인자로 사용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
static void pr_sysconf(char *,int);
static void pr_pathconf(char *,char *,int);
 
int main(int argc, char *argv[])
    
{
    if(argc!=2){
        fprintf(stderr,"usage:a.out<dirname>\n");
        return -1;
    }
 
    pr_sysconf("ARG_MAX =",_SC_ARG_MAX);
    pr_sysconf("CHILD =",_SC_CHILD_MAX);
    pr_sysconf("clock ticks/secnod =",_SC_CLK_TCK);
    pr_sysconf("NGROUPS_MAX =",_SC_NGROUPS_MAX);
    pr_sysconf("OPEN_MAX =",_SC_OPEN_MAX);
 
#ifdef _SC_STREAM_MAX
        pr_sysconf("STREAM_MAX =",_SC_STREAM_MAX);
#endif
#ifdef _SC_TZNAME_MAX
            pr_sysconf("TZNAME_MAX =",_SC_TZNAME_MAX);
#endif
            pr_sysconf("_POSIX_JOB_CONTROL =",_SC_JOB_CONTROL);
            pr_sysconf("_POSIX_SAVED_IDS =",_SC_SAVED_IDS);
            pr_sysconf("_POSIX_VERSION =",_SC_VERSION);
            pr_pathconf("MAX_CANON =","/dev/tty" ,_PC_MAX_CANON);
            pr_pathconf("MAX_INPUT =","/dev/tty" ,_PC_MAX_INPUT);
            pr_pathconf("_POSIX_VDISABLE =","/dev/tty" ,_PC_VDISABLE);
            pr_pathconf("LINK_MAX =",argv[1] ,_PC_LINK_MAX);
            pr_pathconf("NAME_MAX =",argv[1] ,_PC_NAME_MAX);
            pr_pathconf("PATH_MAX =",argv[1] ,_PC_PATH_MAX);
            pr_pathconf("PIPE_BUF =",argv[1] ,_PC_PIPE_BUF);
            pr_pathconf("_POSIX_NO_TRUNC =",argv[1] ,_PC_NO_TRUNC);
            pr_pathconf("_POSIX_CHOWN_RESTRICTED =",argv[1],_PC_CHOWN_RESTRICTED);
            return 0;
}
 
static void pr_sysconf(char *mesg, int name)
{
    long val;
    fputs(mesg,stdout);
    errno=0;
    if((val=sysconf(name))<0){
        if(errno!=0){
            fprintf(stderr,"sysconf error\n");
            exit(1);
        }
        fputs("(not defined)\n",stdout);
    }else printf(" %ld\n",val);
}
 
static void pr_pathconf(char *mesg,char *path, int name)
{
    long val;
 
    fputs(mesg,stdout);
    errno=0;
 
    if((val=pathconf(path,name))<0){
        if(errno!=0){
            fprintf(stderr,
                    "pathconf error, path=%s\n",path);
            exit(1);
        }
        fputs("(no limit)\n",stdout);
    }else
        printf(" %ld\n",val);
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

프로세스 자원

#include <ulimit.h>

long ulimit(int cmd, long newlimit);

프로세스의 제한을 가져오거나 설정한다.

 

#include <sys/types.h>

#include <sys/resource.h>

#include <unistd.h>

int getrlimit(int resource, struct rlimit *rlim);

int getrusage(int who, strcut rusage *usage);

int setrlimit(int resource, const struct rlimit *rlim);

자원(resource)의 값을 얻어오거나 값을 설정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
 
int main()
{
    struct rlimit rlim;
 
    getrlimit(RLIMIT_NPROC, &rlim);
    printf("PROC MAX : %lu : %lu\n",rlim.rlim_cur,rlim.rlim_max);
 
    getrlimit(RLIMIT_NOFILE, &rlim);
    printf("FILE mAX : %lu : %lu\n",rlim.rlim_cur,rlim.rlim_max);
 
    getrlimit(RLIMIT_CPU,&rlim);
    if(rlim.rlim_cur == RLIM_INFINITY){
        printf("UNLIMIT\n");
    }
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

+ Recent posts