1. 程式人生 > >《Linux程式設計》第四章 (臨時檔案、使用者資訊、主機資訊、日誌、資源和限制)

《Linux程式設計》第四章 (臨時檔案、使用者資訊、主機資訊、日誌、資源和限制)

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 }
    
    實驗結果如下:
  • [email protected]:~$ ./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 }