Linux 常用库函数

持续更新, 日后在补

Linux C多线程编程

 

Linux中查看库函数中结构体定义的方法

在/usr/include/目录下
1. 生成tags文件  命令:sudo ctags -R
2. 搜索(宏、结构体)命令: vi -t 名称
3.然后就看见它们的定义了  ctrl + ] (进入) ctrl + t (返回)

 

进程资源信息 struct rusage

include

利用getrusage可以得到进程的相关资源信息。如:用户开销时间,系统开销时间,接收的信号量等等;

下面是rusage的结构:

struct rusage {
    struct timeval ru_utime; /* user CPU time used */ 用户态下执行总时间
    struct timeval ru_stime; /* system CPU time used */  内核态下执行总时间。
    long   ru_maxrss;        /* maximum resident set size */
    long   ru_ixrss;         /* integral shared memory size */
    long   ru_idrss;         /* integral unshared data size */
    long   ru_isrss;         /* integral unshared stack size */
    long   ru_minflt;        /* page reclaims (soft page faults) */
    long   ru_majflt;        /* page faults (hard page faults) */
    long   ru_nswap;         /* swaps */
    long   ru_inblock;       /* block input operations */
    long   ru_oublock;       /* block output operations */
    long   ru_msgsnd;        /* IPC messages sent */
    long   ru_msgrcv;        /* IPC messages received */
    long   ru_nsignals;      /* signals received */
    long   ru_nvcsw;         /* voluntary context switches */
    long   ru_nivcsw;        /* involuntary context switches */
};

  


如果参数who是RUSAGE_SELF,则得到的是当前进程的资源信息。如果是RUSAGE_CHILDREN,则将返回当进程的终止掉的子进程和等待子进程的资源信息。当调用成功后,返回0,否则-1;
下面是一个例子:

int who = RUSAGE_SELF;
struct rusage usage;
int ret;

ret = getrusage(who, &usage);

这样你就可以用usage获取你想要的东西了;

 

同样wait3()和wait4()两个函数也可以得到进程资源信息

 
 
 
struct itimerval
 
struct itimerval {
    struct timeval it_interval; /* 计时器重启动的间歇值 next_val*/
    struct timeval it_value;    /* 计时器安装后首先启动的初始值 cur_val*/
};

struct timeval {
    long tv_sec;                /* 秒 seconds */
    long tv_usec;               /* 微妙(1/1000000)  microseconds */
};

 

getitimer and setitimer

 
#include <sys/time.h> 
int getitimer(int which, struct itimerval *value); 
int setitimer(int which, const struct itimerval *restrict value, 
struct itimerval *restrict ovalue);

setitimer()将value指向的结构体设为计时器的当前值,如果ovalue不是NULL,将返回计时器原有值。

 

which:间歇计时器类型,有三种选择

ITIMER_REAL      //数值为0,计时器的值实时递减,发送的信号是SIGALRM。
ITIMER_VIRTUAL //数值为1,进程执行时递减计时器的值,发送的信号是SIGVTALRM。
ITIMER_PROF     //数值为2,进程和系统执行时都递减计时器的值,发送的信号是SIGPROF。

返回说明: 
成功执行时,返回0。失败返回-1,errno被设为以下的某个值 
EFAULT:value或ovalue是不有效的指针
EINVAL:其值不是ITIMER_REAL,ITIMER_VIRTUAL 或 ITIMER_PROF之一

  

#include <pwd.h>

struct passwd

 struct passwd  

{  
    char * pw_name; /* Username, POSIX.1 */  
    char * pw_passwd; /* Password */  
    __uid_t pw_uid; /* User ID, POSIX.1 */  
    __gid_t pw_gid; /* Group ID, POSIX.1 */  
    char * pw_gecos; /* Real Name or Comment field */  
    char * pw_dir; /* Home directory, POSIX.1 */  
    char * pw_shell; /* Shell Program, POSIX.1 */  
};  
-------------------------------------------------------------------------------- 

当您需要取得有关某个使用者的资讯时,大致上有以下几个函数可以使用:  

-------------------------------------------------------------------------------- 

struct passwd * getpwuid(uid_t uid);  
当您知道使用者的uid(user id)时,可以透过getpwuid来得知所有关於该使用者的相关资讯。  
-------------------------------------------------------------------------------- 

struct passwd * getpwnam(char * name);  
当您知道使用者名称时,可以透过getpwnam来得知所有关於该使用者的相关资讯。  
-------------------------------------------------------------------------------- 

int getpw(uid_t uid, char *buf);  
当您仅需要取得使用者的密码进行比对时,可以使用getpw。  
-------------------------------------------------------------------------------- 

另外,有存取一系列使用者资讯的方法。  
------------------------------------------------------------------------------- 

FILE * pwdopen(void);  
开启password档案。  
-------------------------------------------------------------------------------- 

struct passwd * pwdread(FILE * stream,struct passwd *p);  
读取一个使用者资讯进来,填到p中,返回p为成功,NULL为失败。  
-------------------------------------------------------------------------------- 

void setpwent(void);  
将读取资料流重设到起点。  
-------------------------------------------------------------------------------- 

void endpwent(void);  
关闭password档案资料流。  
-------------------------------------------------------------------------------- 

struct passwd * getpwent(void);  
读取一个使用者资讯进来,有必要的话,则将进行开档动作。  
-------------------------------------------------------------------------------- 

struct passwd * fgetpwent(FILE * stream);  
从档案中读取一个使用者资讯进来。  
-------------------------------------------------------------------------------- 

int putpwent(struct passwd *p,FILE *f);  
将一个使用者资讯写入档案中。  
-------------------------------------------------------------------------------- 

struct passwd * pwdalloc(void);  
配置一个记忆体区块给passwd用。  
-------------------------------------------------------------------------------- 
 
 
 
 

 

 

NAME
getrlimit, setrlimit, prlimit - get/set resource limits

SYNOPSIS
#include <sys/time.h>
#include <sys/resource.h>

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

int prlimit(pid_t pid, int resource, const struct rlimit *new_limit,
struct rlimit *old_limit);

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

prlimit(): _GNU_SOURCE && _FILE_OFFSET_BITS == 64

DESCRIPTION
The getrlimit() and setrlimit() system calls get and set resource limits respectively. Each resource has an associated soft and hard limit, as
defined by the rlimit structure:

typedef int rlim_t;

struct rlimit {

rlim_t rlim_cur; /* Soft limit */ /
rlim_t rlim_max; /* Hard limit (ceiling for rlim_cur) */
};

The soft limit is the value that the kernel enforces for the corresponding resource. The hard limit acts as a ceiling for the soft limit: an
unprivileged process may set only its soft limit to a value in the range from 0 up to the hard limit, and (irreversibly) lower its hard limit. A
privileged process (under Linux: one with the CAP_SYS_RESOURCE capability) may make arbitrary changes to either limit value.

The value RLIM_INFINITY denotes no limit on a resource (both in the structure returned by getrlimit() and in the structure passed to setrlimit()).

转载于:https://www.cnblogs.com/spxcds/p/5346322.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值