"Fourth edition of Linux Programming", chapter Fourth of the exercises

Source: Internet
Author: User
Tags local time strcmp syslog

1, P128

A program that gets the date-time format to get the date of the time.

#include <stdio.h> #include <time.h>int main (int argc,char** argv) {struct tm* Time1,*time_trans;    Temporal data Structure time_t Alt;char c_time[128];char* result;char* result1= ""; Time (&alt); Time1=localtime (&alt)   ; Returns the local time stored in the data structure 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 convert the data structure into string return strftime (c_time,128, "%Y%B%d  %p%i:%m:%s", time1);    Format to save time to C_time string printf ("%s\n", C_time); strcpy (C_time, "Mar 2008,12:13:14");p rintf ("Now trans, the time%s \ n", c_ time); Result=strptime (C_time, "%d%b%y,%r:%s", Time_trans);p rintf ("%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

A program that creates temporary files, obtains user information, and outputs logs.

#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 ();     Gets the user's uidpw=getpwuid (ID); Get the PW file information for user uid 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);p W=getpwnam ("root"); Get the PW file information for user root 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 ())//Get all user's pw file information {//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);         Returns a unique filename tempfile=fopen (filename, "R");      if (!tempfile) {syslog (log_err| Log_user, "oops-%m\n");} Tempfile=tmpfile ();            Returns a file with a unique file name File*if (!tempfile) {syslog (log_err| Log_user, "%m\n");} return 0;}


3, P141

A resource that restricts the CPU consumption of the program.

#include <stdio.h> #include <sys/time.h> #include <sys/types.h> #include <sys/resource.h># Include<math.h>void () {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);   The information to get the program running is stored in the struct rusage printf ("CPU usage:user=%d.%0 6d,system==%d.%0 6d\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);p ro= GetPriority (Prio_process,getpid ());  Get the priority of the program printf ("Current priority:%d\n", pro); R_limit.rlim_cur=2048;r_limit.rlim_max=4096;setrlimit (Rlimit_ Fsize,&r_limit);    Set some resource limits for the program Fun (); return 0;}



"Fourth edition of Linux Programming", chapter Fourth of the exercises

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.