Lecture 6 Lecture files in /home/hwang/cs375/lecture06 on csserver. Questions?

advertisement

Lecture 6

Lecture files in /home/hwang/cs375/lecture06 on csserver.

cp -r /home/hwang/cs375/lecture06 .

scp -r user@csserver.evansville.edu:/home/hwang/cs375/lecture06 .

Questions?

Tuesday, September 15 CS 375 UNIX System Programming - Lecture 6 1

Outline

● getstat exercises

Time structures

Directories

The /proc filesystem

Error handling

Handling program arguments

Formatted I/O

Tuesday, September 15 CS 375 UNIX System Programming - Lecture 6 2

getstat Exercises

Modify getstat.cpp

to display all the information shown by the stat command.

$  stat /etc/passwd

  File: `/etc/passwd'

  Size: 32889   Blocks: 72  IO Block: 4096 regular file

Device: 801h/2049d Inode: 1712752 Links: 1

Access: (0644/­rw­r­­r­­)  Uid: (0/root)   Gid: (0/root)

Access: 2010­09­08 08:17:16.000000000 ­0500

Modify: 2010­08­27 08:16:42.000000000 ­0500

Change: 2010­08­27 08:16:42.000000000 ­0500

 Birth: ­

Tuesday, September 15 CS 375 UNIX System Programming - Lecture 6 3

Processing Times

Times in the stat struct are of type time_t, which is the elapsed time in seconds since the Epoch began (January 1, 1970, 00:00 UTC).

Information about (some) time routines can be found on the ctime man page. In particular, there are routines for converting between time_t values and a tm struct of "broken down time" values. E.g., struct tm *bt = localtime(sb­>atime);

Tuesday, September 15 CS 375 UNIX System Programming - Lecture 6 4

Directory Scanning

Use opendir to open a directory. Calls to readdir will return successive directory entries. closedir closes the directory.

  DIR *dp;

  if ((dp = opendir(".")) == NULL)

     error_exit();

  struct dirent *entry;

  while((entry = readdir(dp)) != NULL) {

     cout << entry­>d_name << endl;

  }

  if(closedir(dp) == ­1)

     error_exit();

Tuesday, September 15 CS 375 UNIX System Programming - Lecture 6 5

Directory Scanning

The telldir and seekdir routines allow you to reset a directory scan to a prior position.

See the program on pages 124-125 of BLP for an example of using the directory routines to display a directory tree.

Note: Use chdir() and getcwd() to change and get current working directories.

Refer to dirscan.cpp

for another example.

Tuesday, September 15 CS 375 UNIX System Programming - Lecture 6 6

The /proc Filesystem

Linux uses several pseudo filesystems. These systems do not use up any space on the disk.

They typically provide a means for the kernel to present information to applications or users.

We will discuss briefly only the /proc filesystem which provides an interface to kernel data structures. See man proc for more information.

Tuesday, September 15 CS 375 UNIX System Programming - Lecture 6 7

The /proc Filesystem

There is a /proc/[number] directory for each running process where number is the process

ID. There are cmdline , cwd , exe , environ , etc. entries.

/proc/cpuinfo provides information about the

CPU and architecture.

/proc/cmdline contains the arguments that are passed to the kernel at boot time.

Tuesday, September 15 CS 375 UNIX System Programming - Lecture 6 8

The /proc Filesystem

/proc/net contains several entries that provide information about various network layers.

/proc/filesystems provides a list of all filesystems compiled into the kernel.

/proc/ide and /proc/scsi provide information about corresponding devices.

Tuesday, September 15 CS 375 UNIX System Programming - Lecture 6 9

The /proc Filesystem

/proc/sys contains several entries corresponding to various kernel variables.

These can not only be read, but also set to tune the kernel.

/proc/sys/fs/file-max defines the system wide limit on the number of open files for all processes. This can be changed easily (by root):

$ echo 100000 > /proc/sys/fs/file­max

Tuesday, September 15 CS 375 UNIX System Programming - Lecture 6 10

Error Handling

Most system routines return -1 on error. The global errno variable (defined in errno.h) also is set when an error occurs and indicates the type of error.

CHECK ALL ROUTINES FOR ERRORS!!!!

The perror routine (defined in stdio.h) decodes errno and displays a meaningful message on stderr.

The strerror routine (defined in string.h) returns the perror message as a char string. It can be used to display custom error messages.

Tuesday, September 15 CS 375 UNIX System Programming - Lecture 6 11

Error Handling

int main(int argc, char *argv[])

{

   if(argc != 2) {

      cerr << "usage: showerrs filename" 

           <<  endl; 

      exit(1); 

   }

   int fd;

   if((fd = open(argv[1], O_RDWR)) == ­1)

      { perror("showerrs"); exit(2); }

   if(close(fd) == ­1)

      { perror("showerrs"); exit(3); }

   return 0;

}

Tuesday, September 15 CS 375 UNIX System Programming - Lecture 6 12

Error Handling

Here is example output from running the program on the previous slide:

$  ./showerrs /etc/passwd showerrs: Permission denied

$  ./showerrs /etc showerrs: Is a directory

$  ./showerrs xxxxxxxx showerrs: No such file or directory

Tuesday, September 15 CS 375 UNIX System Programming - Lecture 6 13

The getopt( ) Routine

The getopt( ) routine parses command line arguments. Successive calls to getopt( ) return option characters and set global variables.

It can detect single character options of the form "-abc" or "-a -b -c". It can parse lines with options that require arguments "-fdata.txt" or

"-f data.txt".

Note: There is a getopt program ( man 1 getopt ) in addition to the getopt( ) routine ( man

3 getopt ). The former can be used by shell scripts.

Tuesday, September 15 CS 375 UNIX System Programming - Lecture 6 14

The getopt( ) Routine

● getopt( ) permutes argv[ ] so that all nonoptions are at the end.

The getopt prototype is:

  #include <unistd.h>

  int getopt(int argc, char *const argv[], 

             const char *optstring);

See getopt.cpp

for an example.

Tuesday, September 15 CS 375 UNIX System Programming - Lecture 6 15

Formatted I/O

BLP discusses the C stdio routines: fopen , fclose , fread , and fwrite . In addition, fscanf and fprintf provide formatted output. These routines are part of ANSI standard C.

The equivalent in C++ is the iostream types: e.g., fstream , ifstream , ofstream , along with operators << and >>, and manipulators for formatting.

Tuesday, September 15 CS 375 UNIX System Programming - Lecture 6 16

Formatted I/O

Note that the read and write routines read and write binary data ( writenum1.cpp

). They DO

NOT do format conversions (like cin / cout or scanf / printf ).

If you need formatted output, using a C++ stringstream or C sprintf to format binary data is recommend (see writenum2.cpp

for a stringstream example).

Tuesday, September 15 CS 375 UNIX System Programming - Lecture 6 17

Download