1.临时文件

  • 程序有的时候会使用一些中间文件存储某些中间计算结果,最后在将这些文件删掉。这样的文件成为临时文件。
    tmpnam函数可以生成一个唯一的临时文件名。函数原型如下:
    #include <stdio.h>
    char* tmpnam(char *s);//返回一个唯一的文件名。
    FILE* tmpfile(void);//该函数指向唯一的临时文件,以读写的方式打开。当对它的引用全部关闭的时候,该文件会被删除。
  • 改程序以UNIX有另一种生成临时文件名的方式,就是mktemp和mkstemp函数。他们可以为临时文件名制定一个模板,
    模板能够让你对文件的存放位置和名字有更多的控制。
    函数原型如下:
    #include <stdlib.h>
    char* mktemp(char *template);
    char* mkstemp(char *template);

2.用户登录

  • 用户在登录Linux系统的时候,会有一个唯一的UID。UID是用户身份信息的关键。UID有自己的类型uid_t。一般
    用户的UID值都大于100。
    #include <sys/types.h>
    #include <unistd.h>
    uid_t getuid(void);//返回程序关联的UID,通常是启动程序的用户的UID。
    char *getlogin(void);//返回与当前用户关联的登录名
  • #include <sys/types.h>
    #include <pwd.h>
    struct passwd *getpwuid(uid_t uid);//这两个函数都返回一个与某个用户对应的passwd指针。
    struct passwd *getpwnam(const char *name);
  • 1 #include <sys/types.h>
      2 #include <pwd.h>
      3 #include <stdio.h>
      4 #include <unistd.h>
      5 #include <stdlib.h>
      6
      7 int main()
      8 {
      9     uid_t uid;
     10     gid_t gid;
     11
     12     struct passwd *pw;
     13     uid=getuid();
     14     gid=getgid();
     15
     16     printf("User is %s\n",getlogin());
     17
     18     printf("User IDs:uid=%d, gid=%d\n",uid,gid);
     19
     20     pw=getpwuid(uid);
     21     printf("UID passwd entry:\n name=%s,uid=%d,gid=%d,home=%s,shell=%s\n",
     22     pw->pw_name,pw->pw_uid,pw->pw_gid,pw->pw_dir,pw->pw_shell);
     23
     24     pw=getpwnam("root");
     25
     26     printf("root passwd entry:\n");
     27     printf("name=%s,uid=%d,gid=%d,home=%s,shell=%s\n",
     28     pw->pw_name,pw->pw_uid,pw->pw_gid,pw->pw_dir,pw->pw_shell);
     29     exit(0);
     30 }
    
    实验结果如下:
  • jiang@ubuntu:~$ ./tesuid
    User is (null)
    User IDs:uid=1000, gid=1000
    UID passwd entry:
     name=jiang,uid=1000,gid=1000,home=/home/jiang,shell=/bin/bash
    root passwd entry:
    name=root,uid=0,gid=0,home=/root,shell=/bin/bash
    

3.主机信息

  • gethostname函数获取机器名
    #include <unistd.h>
    int gethostname(char* name,size_t namelen);//该函数把机器的网络名写入name字符串,该字符串至少有namelen个字符长。
    成功时gethostname返回0,否则是1。
    可以通过uname系统调用获得关于主机的更多详细信息。
    #include <sys/utsname.h>
    int uname(struct utsname *name);//获得更多的主机信息
 1 #include <sys/utsname.h>
  2 #include <unistd.h>
  3 #include <stdio.h>
  4 #include <stdlib.h>
  5 int main()
  6 {
  7     char computer[256];
  8     struct utsname uts;
  9     if(gethostname(computer,255)!=0||uname(&uts)<0){//这一步的时候已经将主机信息写入了computer和uts中
 10         fprintf(stderr,"Could not get host information\n");
 11         exit(1);
 12     }
 13     printf("Computer host name is %s\n",computer);
 14     printf("System is %s on %s hardware\n",uts.sysname,uts.machine);
 15     printf("Nodename is %s,%s\n",uts.release,uts.version);
 16     exit(0);
 17 }

4.日志

  • #include <syslog.h>
    void syslog(int priority,const char *message, arguments...);//UNIX规范通过syslog函数为所有成粗产生日志信息提供一个接口
    //该函数向系统的日志设施发送一条日志信息,每条信息都有一个priority参数,该参数是一个严重级别与一个设施值的
    //按位或。严重级别控制日志的信息处理方式,设施值记录日志的信息来源。
  • 其他的三个函数:
    #include <syslog.h>
    void closelog(void)
    void openlog(const char *ident,int logopt,int facility);
    int setlogmask(int maskpri);

5.资源和限制

头文件limits.h中定义了很多代表操作系统方面限制的显式常量。头文件sys/resource.h提供了资源操作方面的定义,其中包括
程序长度、执行优先级和文件资源等方面限制及虚拟性查询和设置的参数。
#include <sys/resouce.h>
int getpriority(int which, id_t who);
int setpriority(int which,id_t who, int priority);
int getrlimit(int resource, struct rlimit *r_limit);
int setrlimit(int resouce, const struct rlimit *r_limit);
int getrusage(int who struct rusage *r_usage);
id_t是一个整形,用于用户和组标识符。rusage结构用来确定当前程序已经耗费了多少CPU时间。
程序消耗CPU的时间分为用户时间(程序执行自身使用的时间)和系统时间(操作系统为程序执行所耗费的时间)。普通用户只能降低其程序的优先级而不能升高。
优先级的有效范围是-20~20,数值越高优先级越低。
函数举例:模拟一个典型的应用程序,设置并超越了一个资源限制:

 1 #include <sys/types.h>
  2 #include <sys/resource.h>
  3 #include <sys/time.h>
  4 #include <unistd.h>
  5 #include <stdio.h>
  6 #include <stdlib.h>
  7 #include <math.h>
  8
  9 void work()
 10 {
 11     FILE *f;
 12     int i;
 13     double x=4.5;
 14     f=tmpfile();//生成一个临时文件
 15     for(i=0;i<10000;i++){
 16         fprintf(f,"Do some output\n");//使用10000个循环向临时文件中写入一个字符串。
 17         if(ferror(f)){
 18             fprintf(stderr,"Error writing to temporary file\n");
 19             exit(1);
 20         }
 21
 22     }
 23     for(i=0;i<1000000;i++){
 24         x=log(x*x+3.21);//进行计算,产生一些cpu负载。
 25     }
 26 }
 27 int main()
 28 {
 29     struct rusage r_usage;
 30     struct rlimit r_limit;
 31     int priority;
 32     work();
 33     getrusage(RUSAGE_SELF,&r_usage);//rusage结构用来确定当前程序已经消耗了多少CPU时间
 34
 35     printf("CPU usage:User=%ld.%06ld,System=%ld.%06ld\n",
 36     r_usage.ru_utime.tv_sec,r_usage.ru_utime.tv_usec,
 37     r_usage.ru_stime.tv_sec,r_usage.ru_stime.tv_usec);
 38     priority=getpriority(PRIO_PROCESS,getpid());//获得优先级
 39     printf("Current priority=%d\n",priority);//默认优先级是0
 40     getrlimit(RLIMIT_FSIZE,&r_limit);//系统资源方面的限制可以使用getrlimit和setrlimit。
 41     printf("Current FSIZE limit:soft =%ld,hard=%ld\n",r_limit.rlim_cur,r_lim    it.rlim_max);
 42     r_limit.rlim_cur=2048;
 43     r_limit.rlim_max=4096;
 44     printf("Setting a 2k file size limit\n");
 45     setrlimit(RLIMIT_FSIZE,&r_limit);
 46     work();
 47     exit(0);
 48 }

 

10-06 19:53