Week 4 - May 27-31
Lecture Recording
here.
Introduction
This week we will look at advanced file I/O with dup() and dup2(). We will also look at some more debugging methods.
Videos
Quiz
Lecture Material
Labs
- Lab 3 - I/O control.
- Lab 4 - I/O Control - The Screen
Assignment(s)
None.
Sample Code
Advanced File I/O
- Advanced file i/o features are covered, such as dup(), dup2(), fcntl(), more flags, reading and writing with an offset, reading and writing an entire structure,...
- The source code for a file which redirects the standard error channel via dup() and dup2() can be found in fileDup.cpp
- Files that exercise the various file flags via fcntl() and demonstrates race conditions can be seen in the files race1.cpp, race2.cpp and fcntl.cpp. A script to run all three appears in the file runRace.sh. Here is the Makefile for all three.
- To see how to set and unset flags with C/C++, see
SetUnsetBit.docx
- A file that exercises reading and writing with an offset via pread() and pwrite() can be found in offset.cpp along with its Makefile.
- An example of reading and writing an entire structure to a file via readv() and writev() can be found in the file car.cpp along with its Makefile.
Socket Files and IOCTL's
- Sockets are introduced. For a good tutorial, see Sockets Tutorial. Here the client server model is introduced.
- Source code for the client, server and for the makefile can be found at client1.cpp, server.cpp, and Makefile.
- We continue our study of IOCTL's through a look at the netdevice low-level access to Linux network devices.
A description of how to get the MAC address of an ethernet interface can be found at
MicroHOWTO.
- We look at an application etherCtrl2.cpp which retrieves the Hardware address, the Maximum Transfer Unit (MTU) and the Interface flags of a network interface. This will assist with Lab3 where you have to also get the IP address, netmask, and broadcast address of a network interface.
Real-time Debugging with cerr
- For real time operations, it is not possible to halt your program and look at the values of your variables. Rather, you print debug data to a file while
your program is running, and you watch that file. One method of monitoring that debug file is with the system command tail - f. Therefore,
you redirect all data to cerr to a debug file (ie ErrorLog.txt) and you watch that file in a separate window.
Be sure to enable a debug flag in your Makefile (ie DEBUG) and use it in your code. Your code can
therefore run in debug mode (DEBUG is enabled in the Makefile) or normal mode (DEBUG is disabled in the Makefile).
Source code that was used to demonstrate real-time debugging with cerr can be found at
Makefile,
Math.cpp,
Math.h,
Conversions.cpp,
Conversions.h,
General.cpp,
General.h,
Geometry.cpp,
Geometry.h.
In this source code the function CelsiusToFahrenheit was made to crash when the flag CRASH was enabled in the Makefile.
The real time debug mode was set when the flag DEBUG was enabled in the Makefile. Debug exists in Math.cpp and Conversions.cpp only.