Project 1: Getting Information from System Functions
Due Monday, October 28
There are quite a few system functions available, but they follow some common conventions, and so after learning to use a few of them, the others will (hopefully!) make sense too. For this project, make a C or C++ program that will call a number of system functions, and print out various information from them. For all the functions below, take a look at the associated manual page, and it should help. For some of them, you might need to specify a manual section. All of these are in either section 2 or 3.
Note that for some of these information-gathering functions, there is a matching "set" function. It would be fun to include use of those in this assignment, but I imagine this may be done on isoptera where we can't have everybody changing the hostname, so this project will only use "get" type functions. You can use the set version in MLH 310 by just using sudo to run your program. Just sudo the binary (./a.out or whatnot), not running gcc.
- Use fstat to determine how big the file /etc/fstab is. /etc/fstab contains information about filesystems that can be mounted automatically on the computer, and you can use it to automatically mount a hard drive. When calling fstat, you'll have to provide the address of a structure which will contain the results. st_size is the total size, in the structure. You don't have to use malloc to make the structure, since the structure will be just fine on the stack, but you can if you like.
- Use open, lseek, read, and close to print out the last 100 bytes of /etc/fstab.
- Print out the hostname of the system. Use gethostname for this. Note that you could also set the hostname, if you had admin access.
- Use sysinfo to figure out how many bytes of RAM the system has. Like fstat, you'll have to give it the address of a structure. Convert it into a more reasonable unit like gigabytes (I don't know of a system function for that).
- Print out the current directory, with getcwd.
- Print out a random number using the function random (manual section 3), but before you do, seed the random number generator using the amount of free RAM on the system (you should still have this number from calling sysinfo earlier). This isn't a truly random seed, but it does tend to vary, so you'll get different numbers some of the time if you run your program repeatedly (especially on a busy machine like isoptera).
Here's what my solution prints out, when I run it on nimrod:
seth@nimrod:~/cs253 $ ./a.out
Size: 939 bytes
End of file contains: /dev/sda2 during installation
/dev/sr0 /media/cdrom0 udf,iso9660 user,noauto 0 0
This host is named nimrod
Total RAM: 125 gigabytes
We are currently in the directory /home/seth/cs253
Here is a random number: 2143318621
Here's what it prints when running on nikabrik (Note: It requires -lsysinfo to compile on FreeBSD):
seth@nikabrik:~ $ ./a.out
Size: 79 bytes
End of file contains: # Device Mountpoint FStype Options Dump Pass#
/dev/ada0p2 none swap sw 0 0
This host is named nikabrik
Total RAM: 62 gigabytes
We are currently in the directory /storage/home/seth
Here is a random number: 1408992891