《Linux程式設計 第四版》之第四章的練習題

來源:互聯網
上載者:User

標籤:linux   centos   虛擬機器   

1、P128

一個擷取日期 時間 格式化擷取時間 日期 的程式。

#include<stdio.h>#include<time.h>int main(int argc,char** argv){struct tm* time1,*time_trans;    //時間資料結構time_t alt;char c_time[128];char* result;char* result1="";time(&alt);time1=localtime(&alt);   //返回本地時間存到資料結構中printf("the date is %d.%d.%d \n",time1->tm_year,time1->tm_mon,time1->tm_mday);   printf("the date is %s \n",ctime(&alt));    //ctime將資料結構中轉換成字串返回strftime(c_time,128,"%Y %B %d  %p %I:%M:%S",time1);    //格式化將時間存到c_time字串printf("%s\n", c_time);strcpy(c_time,"12 Mar 2008,12:13:14");printf("Now trans the time %s \n", c_time);result=strptime(c_time,"%d %b %Y,%R:%S",time_trans);printf("%s \n", result);//printf("%d", strcmp(result1,"ye"));if(strcmp(result,result1)==0){printf("%d %d %d\n", time_trans->tm_hour,time_trans->tm_min,time_trans->tm_sec);}return 0;}

2、P130、132

一個建立臨時檔案、擷取使用者資訊、輸出Log的程式。

#include<stdio.h>#include<pwd.h>#include<sys/types.h>#include<syslog.h>int main(int argc,char** argv){uid_t id;struct passwd *pw;FILE* tempFile;char* fileName;id=getuid();     //擷取使用者的uidpw=getpwuid(id); //擷取使用者uid的pw檔案資訊printf("UID PASSWORD ENTRY:\n name=%s,uid=%d,home=%s,shell=%s\n",pw->pw_name,pw->pw_uid, pw->pw_dir,pw->pw_shell);pw=getpwnam("root"); //擷取使用者root的pw檔案資訊printf("ROOT PASSWORD ENTRY:\n name=%s,uid=%d,home=%s,shell=%s\n",pw->pw_name,pw->pw_uid, pw->pw_dir, pw->pw_shell);while(pw=getpwent()) //依次擷取所有使用者的pw檔案資訊{//printf("UID PASSWORD ENTRY:\n name=%s,uid=%d,home=%s,shell=%s\n",pw->pw_name,pw->pw_uid, pw->pw_dir, pw->pw_shell);}fileName=tmpnam((void*)0);         //返回一個唯一的檔案名稱tempFile=fopen(fileName,"r");      if(!tempFile){syslog(LOG_ERR|LOG_USER,"oops-%m\n");}tempFile=tmpfile();            //返回一個有唯一的檔案名稱的檔案file*if(!tempFile){syslog(LOG_ERR|LOG_USER,"%m\n");}return 0;}


3、P141

一個資源限制 CPU消耗的程式。

#include<stdio.h>#include<sys/time.h>#include<sys/types.h>#include<sys/resource.h>#include<math.h>void fun(){char* tmpStr="what's wrong \n";FILE* f;//f=fopen("haha","w+");f=tmpfile();int i=0;if (!f){printf("??\n");}for(;i<1000000;i++){fwrite(tmpStr, sizeof(tmpStr),1,f);if(ferror(f)){fprintf(stderr,"WRONG");return ;}}i=0;for (; i < 10000000; ++i){int k;k=(1.1+i*i);}//f.close();}int main(int argc,char** argv){struct rusage r_usage;struct rlimit r_limit;int pro;fun();getrusage(RUSAGE_SELF,&r_usage);   //獲得程式啟動並執行資訊 存到結構體rusage中printf("CPU usage: User=%d.%06d,system==%d.%06d\n",r_usage.ru_utime.tv_sec, r_usage.ru_utime.tv_usec,r_usage.ru_stime.tv_sec,r_usage.ru_stime.tv_sec);pro=getpriority(PRIO_PROCESS,getpid());  //獲得程式的優先順序printf("Current Priority:%d\n",pro);r_limit.rlim_cur=2048;r_limit.rlim_max=4096;setrlimit(RLIMIT_FSIZE,&r_limit);    //設定程式的一些資源限制fun();return 0;}



《Linux程式設計 第四版》之第四章的練習題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.