Project 2: Getting Information from System Functions
Due Thursday, April 17, by 9:00 AM
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. If we had access to MLH 310, 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.
Here's what my solution prints out, when I run it on nimrod:
- 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.
- 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).
seth@nimrod:~/cs253 $ ./a.out
Size: 938 bytes
End of file contains: noatime 0 2
/dev/sr0 /media/cdrom0 udf,iso9660 user,noauto 0 0
This host is named nimrod
Total RAM: 135094128640
We are currently in the directory /home/seth/cs253
Here is a random number: 863535237