Read the documentation for XLock, the source of this is available from the Sunsite archives. To lock console screens manually there are also several utilities at Sunsite but to lock automatically requires more effort.
Also visit Scott Draves' page for modules and source code.
A more advanced screensaver launcher is xscreensaver by Jamie Zawinski. This is a client, server system and has several features which are not normally available to Windows® systems, such as a fade to black before launching and running different modules on different displays.
Less information is available for Macintosh but if you visit the Afterdark site then they have a SDK for Mac which will get you started. See also MAC Screensaver FAQ.
XOCHI provides a kit for producing screensavers on all MACs.