Table of Contents
This page contains miscellaneous small bits that haven't yet been further written into separate articles. Perhaps one day…
Daemons
A full install of Amix UNIX includes (and starts) some system daemons that are unlikely to be of much interest to the user in the modern day. Below are brief descriptions of many of these daemons, how to disable them if they are started at boot, and how to start them if they are not. There are a few other daemons (flopd, scrmenu, etc) I'm still looking into.
lp
Printing services. Unless you want to experiment with this, disable it. The only printers likely to work with AMIX at this point are pure postscript printers.
Stop: kill `ps -e | grep lp | cut -f 4 -d ' '`
Disable: mv /etc/rc2.d/S80lp /etc/rc2.d/s80lp
news
Maybe you use Usenet. I bet you don't want to run an NNTP server though.
Stop: /etc/init.d/news stop
Disable: mv /etc/rc2.d/S91news /etc/rc2.d/s91news
smtpd
Mail services. If you intend to use this, go ahead and leave it running. I've never seen the system send mail for anything it runs on its own, so it should be fine to disable.
Stop: kill `ps -e | grep smtpd | cut -f 4 -d ' '`
Disable: mv /etc/rc2.d/S88smtpd /etc/rc2.d/s88smtpd
rpc
Various old rpc services that you probably shouldn't use anyway. Some people who have used more modern UNIX might think you need these for NFS to work. Nope. You can turn this off.
Stop: /etc/init.d/rpc stop
Disable: mv /etc/rc2.d/S75rpc /etc/rc2.d/s75rpc
in.named
DNS nameserver. If you are using DNS and not just hosts, your system will be much happier if this is running. For details on how to configure in.named, see details on this page. It is not enabled by default.
Start: /usr/sbin/in.named
Enable: Create an init script in.named, start it at 70. Enable this at boot with the following: ln /etc/init.d/in.named /etc/rc2.d/S70in.named Now you can start/stop in.named with /etc/init.d/in.named start / stop
Vulnerabilities
Believe it or not, Amiga Unix is not obscure enough to not be mentioned by major security firms. There is a vulnerability in the included “Finger” command:
Amiga Unix allows access to files using finger CVE-1999-1218 Details: amiga-finger (522) reported Feb 1, 1993. The finger command in Amiga allows non-privileged users to access files that they would not normally be able to access. Consequences: Gain Access Remedy: As root, modify the permission of the existing /usr/bin/finger to prevent misuse. Issue the following command: # /bin/chmod 0755 /usr/bin/finger — OR — As root, install the pubsrc package from the distribution tape. In the /usr/src/pub/cmd/finger/src/finger.c file, add the line "setuid(getuid());" (without the quotes) immediately before the line reading "display_finger(finger_list);" Optionally, save a copy of the existing /usr/bin/finger and modify its permission to prevent misuse: # /bin/mv /usr/bin/finger /usr/bin/finger.orig # /bin/chmod 0755 /usr/bin/finger.orig In the /usr/src/pub/cmd/finger directory, issue the commands: # cd /usr/src/pub/cmd/finger # make install
Source: https://exchange.xforce.ibmcloud.com/vulnerabilities/522
Performance
Overall, performance is…really poor. The NFS tests took a really long time. I won't have a good idea just how bad until I get the other tests completed, but I recall various quips and quotes from Usenet and reviews indicating disk performance being slow. There are patches to improve performance, but I think these are for 2.01/2.03. If it's possible to apply them to 2.1c I'll provide results for that as well.
I/O on Amix is relatively slow and uses relatively high amounts of CPU power to do the work. NFS is much slower. An advantage of NFS, though, is you can use RAID-backed storage at the other end. There is no such option for disk-based storage on AMIX.
Results for NFS were much tighter across 4 runs than the disk data. The first disk test for IBlk was a bit of an aberration, using far less CPU and having less speed. Probably another process was kicked off by the system during this test and multitasking caused Bonnie to get less CPU time.
Tips & tricks
This will be a mess at first, it's a scribble pad for me at the moment as I work.
bash 3.0 on AMIX Compiles correctly after I fixed one source code file. AMIX uses deprecated trap designation in /etc/profile, so for CTRL-C to work you need to add a section like this to your /etc/profile:
if [ "$BASH" ] then trap - 2 3 fi
- Don't use tar. Use gtar. tar segfaults until you patch to level 2a (2.1c).
- When configuring software for compilation you will have better luck specifying host/build/target as m68k-cbm-sysv4 than letting configure guess on its own (typically m68k-unknown-sysv4)
- Edit /etc/inet/rc.inet to add default router, not /etc/defaultrouter (ignored by system)
- Export CC=/usr/local/bin/gcc as some applications keep trying to use gcc-1.4.2 in /usr/public/bin during compilation
- Mounting NFS shares from servers without NFS shares will panic the system. Oops! Don't Do That ™. Fixed in 2a patchlevel.
- /usr/include/sys/socket.h, and types.h are too buggy to be used in many situations. Replace with files from 2a patchlevel.
- Use sysadm to enable XDM, old method from 2.01/2.03 doesn't work anymore.
- Patches break w and netstat (at least) without kernel upgrade.
- Adding swap: dd if=/dev/zero of=/tmp/swap bs=1024k count=50 && swap -a /tmp/swap 0 102400
- Compilation will fail on large binaries even with 100MB or more of swap. An error like this will result: ld: cc1: libelf error: Memory error: output file space elf_update:. This is due to artificial restrictions imposed by ulimit. Set everything possible to “unlimited”, ie: ulimit -H unlimited, etc. Certain of these users cannot set, if you get this error you must become root and unset all limits. Type ulimit -? to see what you can fiddle with.
- collect2.c in gcc-2.95.3 has a bug for AMIX, comment out the SYS_SIGLIST defs. Hope this is right, guessing here. (it compiled after) Damn, compiling gcc-2.95.3 is proving to be really educational in a “FOR CRYING OUT LOUD JUST COMPILE” kind of way. This link will be helpful diagnosing late stage1 compile problems: http://sources.redhat.com/ml/crossgcc/1997/msg00326.html … appears 2.7.2.3 is buggy, wonderful.
- It may be helpful to symlink /usr/X to /usr/X11 since /usr/X11 appears often on newer systems.
mkfs
Going to need this info to write this page later…
# mkfs -F ufs /dev/dsk/c6d0s6 1841152 Mkfs: make ufs file system? (DEL if wrong) write error: D wtfs: No such device or address # mkfs Usage: mkfs [-F FSType] [-V] [-m] [current_options] [-o specific_options] special [operands] # mkfs -F ufs /dev/dsk/c6d0s6 1840000 Mkfs: make ufs file system? (DEL if wrong) Warning: 158 sector(s) in last cylinder unallocated /dev/dsk/c6d0s6: 1840000 sectors in 11359 cylinders of 9 tracks, 18 sectors 942.1Mb in 710 cyl groups (16 c/g, 1.33Mb/g, 512 i/g) super-block backups (for fsck -b#) at: 32, 2656, 5280, 7904, 10528, 13152, 15776, 18400, 21024, 23648, ^C# 1756600^C # mkfs -F ufs /dev/dsk/c6d0s6 1756600 Mkfs: make ufs file system? (DEL if wrong) Warning: 128 sector(s) in last cylinder unallocated /dev/dsk/c6d0s6: 1756600 sectors in 10844 cylinders of 9 tracks, 18 sectors 899.4Mb in 678 cyl groups (16 c/g, 1.33Mb/g, 512 i/g) super-block backups (for fsck -b#) at: 32, 2656, 5280, 7904, 10528, 13152, 15776, 18400, 21024, 23648, 26272, 28896, 31520, 34144, 36768, 39392, 41504, 44128, 46752, 49376, 52000, 54624, 57248, 59872, 62496, 65120, 67744, 70368, 72992, 75616, 78240, 80864, 82976, 85600, 88224, 90848, 93472, 96096, 98720, 101344, 103968, 106592, 109216, 111840, 114464, 117088, 119712, 122336, 124448, 127072, 129696, 132320, 134944, 137568, 140192, 142816, 145440, 148064, 150688, 153312, 155936, 158560, 161184, 163808, 165920, 168544, 171168, 173792, 176416, 179040, 181664, 184288, 186912, 189536, 192160, 194784, 197408, 200032, 202656, 205280, 207392, 210016, 212640, 215264, 217888, 220512, 223136, 225760, 228384, 231008, ^C# mkfs -F ufs /dev/dsk/c6d0s6 1755000 Mkfs: make ufs file system? (DEL if wrong) Warning: inode blocks/cyl group (22) >= data blocks (13) in last cylinder group. This implies 216 sector(s) cannot be allocated. /dev/dsk/c6d0s6: 1754784 sectors in 10832 cylinders of 9 tracks, 18 sectors 898.4Mb in 677 cyl groups (16 c/g, 1.33Mb/g, 512 i/g) super-block backups (for fsck -b#) at: 32, 2656, 5280, 7904, 10528, 13152, 15776, 18400, 21024, 23648, 26272, 28896, 31520, 34144, 36768, 39392, 41504, 44128, 46752, 49376, 52000, 54624, 57248, 59872, 62496, 65120, 67744, 70368, 72992, 75616, 78240, 80864, 82976, 85600, 88224, 90848, 93472, 96096, 98720, 101344, 103968, 106592, 109216, 111840, 114464, 117088, 119712, 122336, 124448, 127072, 129696, 132320, 134944, 137568, 140192, 142816, 145440, 148064, 150688, 153312, 155936, 158560, 161184, 163808, 165920, 168544, 171168, 173792, 176416, 179040, 181664, 184288, 186912, 189536, 192160, 194784, 197408, 200032, 202656, 205280, 207392, 210016, 212640, 215264, 217888, 220512, 223136, 225760, 228384, 231008, 233632, 236256, 238880, 241504, 244128, 246752, 248864, 251488, 254112, 256736, 259360, 261984, 264608, 267232, 269856, 272480, 275104, 277728, 280352, 282976, 285600, 288224, 290336, 292960, 295584, 298208, 300832, 303456, 306080, 308704, 311328, 313952, 316576, 319200, 321824, 324448, 327072, 329696, 331808, 334432, 337056, 339680, 342304, 344928, 347552, 350176, 352800, 355424, 358048, 360672, 363296, 365920, 368544, 371168, 373280, 375904, 378528, 381152, 383776, 386400, 389024, 391648, 394272, 396896, 399520, 402144, 404768, 407392, 410016, 412640, 414752, 417376, 420000, 422624, 425248, 427872, 430496, 433120, 435744, 438368, 440992, 443616, 446240, 448864, 451488, 454112, 456224, 458848, 461472, 464096, 466720, 469344, 471968, 474592, 477216, 479840, 482464, 485088, 487712, 490336, 492960, 495584, 497696, 500320, 502944, 505568, 508192, 510816, 513440, 516064, 518688, 521312, 523936, 526560, 529184, 531808, 534432, 537056, 539168, 541792, 544416, 547040, 549664, 552288, 554912, 557536, 560160, 562784, 565408, 568032, 570656, 573280, 575904, 578528, 580640, 583264, 585888, 588512, 591136, 593760, 596384, 599008, 601632, 604256, 606880, 609504, 612128, 614752, 617376, 620000, 622112, 624736, 627360, 629984, 632608, 635232, 637856, 640480, 643104, 645728, 648352, 650976, 653600, 656224, 658848, 661472, 663584, 666208, 668832, 671456, 674080, 676704, 679328, 681952, 684576, 687200, 689824, 692448, 695072, 697696, 700320, 702944, 705056, 707680, 710304, 712928, 715552, 718176, 720800, 723424, 726048, 728672, 731296, 733920, 736544, 739168, 741792, 744416, 746528, 749152, 751776, 754400, 757024, 759648, 762272, 764896, 767520, 770144, 772768, 775392, 778016, 780640, 783264, 785888, 788000, 790624, 793248, 795872, 798496, 801120, 803744, 806368, 808992, 811616, 814240, 816864, 819488, 822112, 824736, 827360, 829472, 832096, 834720, 837344, 839968, 842592, 845216, 847840, 850464, 853088, 855712, 858336, 860960, 863584, 866208, 868832, 870944, 873568, 876192, 878816, 881440, 884064, 886688, 889312, 891936, 894560, 897184, 899808, 902432, 905056, 907680, 910304, 912416, 915040, 917664, 920288, 922912, 925536, 928160, 930784, 933408, 936032, 938656, 941280, 943904, 946528, 949152, 951776, 953888, 956512, 959136, 961760, 964384, 967008, 969632, 972256, 974880, 977504, 980128, 982752, 985376, 988000, 990624, 993248, 995360, 997984, 1000608, 1003232, 1005856, 1008480, 1011104, 1013728, 1016352, 1018976, 1021600, 1024224, 1026848, 1029472, 1032096, 1034720, 1036832, 1039456, 1042080, 1044704, 1047328, 1049952, 1052576, 1055200, 1057824, 1060448, 1063072, 1065696, 1068320, 1070944, 1073568, 1076192, 1078304, 1080928, 1083552, 1086176, 1088800, 1091424, 1094048, 1096672, 1099296, 1101920, 1104544, 1107168, 1109792, 1112416, 1115040, 1117664, 1119776, 1122400, 1125024, 1127648, 1130272, 1132896, 1135520, 1138144, 1140768, 1143392, 1146016, 1148640, 1151264, 1153888, 1156512, 1159136, 1161248, 1163872, 1166496, 1169120, 1171744, 1174368, 1176992, 1179616, 1182240, 1184864, 1187488, 1190112, 1192736, 1195360, 1197984, 1200608, 1202720, 1205344, 1207968, 1210592, 1213216, 1215840, 1218464, 1221088, 1223712, 1226336, 1228960, 1231584, 1234208, 1236832, 1239456, 1242080, 1244192, 1246816, 1249440, 1252064, 1254688, 1257312, 1259936, 1262560, 1265184, 1267808, 1270432, 1273056, 1275680, 1278304, 1280928, 1283552, 1285664, 1288288, 1290912, 1293536, 1296160, 1298784, 1301408, 1304032, 1306656, 1309280, 1311904, 1314528, 1317152, 1319776, 1322400, 1325024, 1327136, 1329760, 1332384, 1335008, 1337632, 1340256, 1342880, 1345504, 1348128, 1350752, 1353376, 1356000, 1358624, 1361248, 1363872, 1366496, 1368608, 1371232, 1373856, 1376480, 1379104, 1381728, 1384352, 1386976, 1389600, 1392224, 1394848, 1397472, 1400096, 1402720, 1405344, 1407968, 1410080, 1412704, 1415328, 1417952, 1420576, 1423200, 1425824, 1428448, 1431072, 1433696, 1436320, 1438944, 1441568, 1444192, 1446816, 1449440, 1451552, 1454176, 1456800, 1459424, 1462048, 1464672, 1467296, 1469920, 1472544, 1475168, 1477792, 1480416, 1483040, 1485664, 1488288, 1490912, 1493024, 1495648, 1498272, 1500896, 1503520, 1506144, 1508768, 1511392, 1514016, 1516640, 1519264, 1521888, 1524512, 1527136, 1529760, 1532384, 1534496, 1537120, 1539744, 1542368, 1544992, 1547616, 1550240, 1552864, 1555488, 1558112, 1560736, 1563360, 1565984, 1568608, 1571232, 1573856, 1575968, 1578592, 1581216, 1583840, 1586464, 1589088, 1591712, 1594336, 1596960, 1599584, 1602208, 1604832, 1607456, 1610080, 1612704, 1615328, 1617440, 1620064, 1622688, 1625312, 1627936, 1630560, 1633184, 1635808, 1638432, 1641056, 1643680, 1646304, 1648928, 1651552, 1654176, 1656800, 1658912, 1661536, 1664160, 1666784, 1669408, 1672032, 1674656, 1677280, 1679904, 1682528, 1685152, 1687776, 1690400, 1693024, 1695648, 1698272, 1700384, 1703008, 1705632, 1708256, 1710880, 1713504, 1716128, 1718752, 1721376, 1724000, 1726624, 1729248, 1731872, 1734496, 1737120, 1739744, 1741856, 1744480, 1747104, 1749728, 1752352, # # mkfs -F ufs /dev/dsk/c6d0s6 1755000 18 9 8192 1024 16 0 Mkfs: make ufs file system? (DEL if wrong) Warning: inode blocks/cyl group (22) >= data blocks (13) in last cylinder group. This implies 216 sector(s) cannot be allocated. /dev/dsk/c6d0s6: 1754784 sectors in 10832 cylinders of 9 tracks, 18 sectors 898.4Mb in 677 cyl groups (16 c/g, 1.33Mb/g, 512 i/g) super-block backups (for fsck -b#) at: 32, 2656, 5280, 7904, 10528, 13152, 15776, 18400, 21024, 23648, 26272, 28896, 31520, 34144, 36768, 39392, 41504, 44128, 46752, 49376, 52000, 54624, 57248, 59872, 62496, 65120, 67744, 70368, 72992, 75616, 78240, 80864, 82976, 85600, 88224, 90848, 93472, 96096, 98720, 101344, 103968, 106592, 109216, 111840, 114464, 117088, 119712, 122336, 124448, 127072, 129696, 132320, 134944, 137568, 140192, 142816, 145440, 148064, 150688, 153312, 155936, 158560, 161184, 163808, 165920, 168544, 171168, 173792, 176416, 179040, 181664, 184288, 186912, 189536, 192160, 194784, 197408, 200032, 202656, 205280, 207392, 210016, 212640, 215264, 217888, 220512, 223136, 225760, 228384, 231008, 233632, 236256, 238880, 241504, 244128, 246752, 248864, 251488, 254112, 256736, 259360, 261984, 264608, 267232, 269856, 272480, 275104, 277728, 280352, 282976, 285600, 288224, 290336, 292960, 295584, 298208, 300832, 303456, 306080, 308704, 311328, 313952, 316576, 319200, 321824, 324448, 327072, 329696, 331808, 334432, 337056, 339680, 342304, 344928, 347552, 350176, 352800, 355424, 358048, 360672, 363296, 365920, 368544, 371168, 373280, 375904, 378528, 381152, 383776, 386400, 389024, 391648, 394272, 396896, 399520, 402144, 404768, 407392, 410016, 412640, 414752, 417376, 420000, 422624, 425248, 427872, 430496, 433120, 435744, 438368, 440992, 443616, 446240, 448864, 451488, 454112, 456224, 458848, 461472, 464096, 466720, 469344, 471968, 474592, 477216, 479840, 482464, 485088, 487712, 490336, 492960, 495584, 497696, 500320, 502944, 505568, 508192, 510816, 513440, 516064, 518688, 521312, 523936, 526560, 529184, 531808, 534432, 537056, 539168, 541792, 544416, 547040, 549664, 552288, 554912, 557536, 560160, 562784, 565408, 568032, 570656, 573280, 575904, 578528, 580640, 583264, 585888, 588512, 591136, 593760, 596384, 599008, 601632, 604256, 606880, 609504, 612128, 614752, 617376, 620000, 622112, 624736, 627360, 629984, 632608, 635232, 637856, 640480, 643104, 645728, 648352, 650976, 653600, 656224, 658848, 661472, 663584, 666208, 668832, 671456, 674080, 676704, 679328, 681952, 684576, 687200, 689824, 692448, 695072, 697696, 700320, 702944, 705056, 707680, 710304, 712928, 715552, 718176, 720800, 723424, 726048, 728672, 731296, 733920, 736544, 739168, 741792, 744416, 746528, 749152, 751776, 754400, 757024, 759648, 762272, 764896, 767520, 770144, 772768, 775392, 778016, 780640, 783264, 785888, 788000, 790624, 793248, 795872, 798496, 801120, 803744, 806368, 808992, 811616, 814240, 816864, 819488, 822112, 824736, 827360, 829472, 832096, 834720, 837344, 839968, 842592, 845216, 847840, 850464, 853088, 855712, 858336, 860960, 863584, 866208, 868832, 870944, 873568, 876192, 878816, 881440, 884064, 886688, 889312, 891936, 894560, 897184, 899808, 902432, 905056, 907680, 910304, 912416, 915040, 917664, 920288, 922912, 925536, 928160, 930784, 933408, 936032, 938656, 941280, 943904, 946528, 949152, 951776, 953888, 956512, 959136, 961760, 964384, 967008, 969632, 972256, 974880, 977504, 980128, 982752, 985376, 988000, 990624, 993248, 995360, 997984, 1000608, 1003232, 1005856, 1008480, 1011104, 1013728, 1016352, 1018976, 1021600, 1024224, 1026848, 1029472, 1032096, 1034720, 1036832, 1039456, 1042080, 1044704, 1047328, 1049952, 1052576, 1055200, 1057824, 1060448, 1063072, 1065696, 1068320, 1070944, 1073568, 1076192, 1078304, 1080928, 1083552, 1086176, 1088800, 1091424, 1094048, 1096672, 1099296, 1101920, 1104544, 1107168, 1109792, 1112416, 1115040, 1117664, 1119776, 1122400, 1125024, 1127648, 1130272, 1132896, 1135520, 1138144, 1140768, 1143392, 1146016, 1148640, 1151264, 1153888, 1156512, 1159136, 1161248, 1163872, 1166496, 1169120, 1171744, 1174368, 1176992, 1179616, 1182240, 1184864, 1187488, 1190112, 1192736, 1195360, 1197984, 1200608, 1202720, 1205344, 1207968, 1210592, 1213216, 1215840, 1218464, 1221088, 1223712, 1226336, 1228960, 1231584, 1234208, 1236832, 1239456, 1242080, 1244192, 1246816, 1249440, 1252064, 1254688, 1257312, 1259936, 1262560, 1265184, 1267808, 1270432, 1273056, 1275680, 1278304, 1280928, 1283552, 1285664, 1288288, 1290912, 1293536, 1296160, 1298784, 1301408, 1304032, 1306656, 1309280, 1311904, 1314528, 1317152, 1319776, 1322400, 1325024, 1327136, 1329760, 1332384, 1335008, 1337632, 1340256, 1342880, 1345504, 1348128, 1350752, 1353376, 1356000, 1358624, 1361248, 1363872, 1366496, 1368608, 1371232, 1373856, 1376480, 1379104, 1381728, 1384352, 1386976, 1389600, 1392224, 1394848, 1397472, 1400096, 1402720, 1405344, 1407968, 1410080, 1412704, 1415328, 1417952, 1420576, 1423200, 1425824, 1428448, 1431072, 1433696, 1436320, 1438944, 1441568, 1444192, 1446816, 1449440, 1451552, 1454176, 1456800, 1459424, 1462048, 1464672, 1467296, 1469920, 1472544, 1475168, 1477792, 1480416, 1483040, 1485664, 1488288, 1490912, 1493024, 1495648, 1498272, 1500896, 1503520, 1506144, 1508768, 1511392, 1514016, 1516640, 1519264, 1521888, 1524512, 1527136, 1529760, 1532384, 1534496, 1537120, 1539744, 1542368, 1544992, 1547616, 1550240, 1552864, 1555488, 1558112, 1560736, 1563360, 1565984, 1568608, 1571232, 1573856, 1575968, 1578592, 1581216, 1583840, 1586464, 1589088, 1591712, 1594336, 1596960, 1599584, 1602208, 1604832, 1607456, 1610080, 1612704, 1615328, 1617440, 1620064, 1622688, 1625312, 1627936, 1630560, 1633184, 1635808, 1638432, 1641056, 1643680, 1646304, 1648928, 1651552, 1654176, 1656800, 1658912, 1661536, 1664160, 1666784, 1669408, 1672032, 1674656, 1677280, 1679904, 1682528, 1685152, 1687776, 1690400, 1693024, 1695648, 1698272, 1700384, 1703008, 1705632, 1708256, 1710880, 1713504, 1716128, 1718752, 1721376, 1724000, 1726624, 1729248, 1731872, 1734496, 1737120, 1739744, 1741856, 1744480, 1747104, 1749728, 1752352, # fsck -F ufs /dev/dsk/c6d0s6 ** /dev/dsk/c6d0s6 ** Last Mounted on ** Phase 1 - Check Blocks and Sizes ** Phase 2 - Check Pathnames ** Phase 3 - Check Connectivity ** Phase 4 - Check Reference Counts ** Phase 5 - Check Cyl groups 2 files, 9 used, 823196 free (12 frags, 102898 blocks, 0.0% fragmentation) # mount -F ufs /dev/dsk/c6d0s6 /mnt # df -k filesystem kbytes used avail capacity mounted on /dev/dsk/c6d0s3 484729 315636 169093 65% / /proc 0 0 0 0% /proc /dev/fd 0 0 0 0% /dev/fd /dev/dsk/c6d0s6 823205 9 823196 0% /mnt #
Internal Timers in Amiga Unix
By David Miller (Commodore Applications and Technical Support, Commodore Business Machines, Inc.)
Copied for safekeeping from http://amigadev.elowar.com archives
The Sleep() function
When you want your program to pause for a number of seconds then continue, you will typically use the sleep(3) function (the notation NAME(SECTION#) refers to the manual page NAME in the SECTION# chapter of the UNIX Reference Manuals; section 1 is commands, section 2 is system calls, and section 3 is library functions). There is also a sleep(1) program which provides the same function to shell scripts. For example:
In C: main() { printf("Hello world!\n"); sleep(5); } Shell: #!/usr/bin/sh echo "Hello world!" sleep 5
The following example is an implementation of the sleep(3) function.
Example 1: SLEEP() Using ALARM() -------------------------------- 1 # include <signal.h> 2 static void trap () 3 { 4 } 5 unsigned sleep ( unsigned duration ) 6 { 7 void (*oldsig)(); 8 unsigned oldtime; 9 oldsig = signal(SIGALRM, trap); 10 oldtime = alarm(0); 11 if (oldtime && oldtime < duration) 12 alarm(oldtime); 13 else 14 alarm(duration); 15 pause(); 16 signal(SIGALRM, oldsig); 17 if (oldtime > duration) 18 alarm(oldtime - duration); 19 if (oldtime && oldtime < duration) 20 return(duration - oldtime); 21 else 22 return 0; 23 } Line Explanation ---- ----------- 1 The file signal.h defines the parameters for the signal(2) function. 2-4 trap() is the simple signal handler that just sets a flag that another piece of code will examine. 5 Define the sleep() function. 6-8 oldsig is a variable that will be used to save the previous state of the signal handler; oldtime will be used to save the state of the alarm clock. 9 Establish trap() as the current signal handler for the alarm signal. The previous handler is saved in oldsig. 10 Clear the alarm clock, saving the current state. 11-14 If the previous setting of alarm was sooner than duration, use the old value, otherwise use duration to set the alarm. 15 Pause the process until the alarm goes off. 16 Restore the old signal handler. 17-18 If the old alarm setting was later than duration, reset the alarm with the difference between duration and oldtime (the time remaining until the previous alarm). 19-22 If an existing alarm is making sleep return early, return the time remaining on the requested sleep.
To schedule its wake up time, sleep(3) uses the alarm(2) system call. The alarm(2) system call asks the OS to deliver a signal (basically a software interrupt) in some number of seconds according to the system clock. However, setting an alarm for 2 seconds does not mean that you will receive an alarm signal in exactly two seconds.
The OS processes alarm requests once every second. Each time an alarm request is processed, the number of seconds remaining for that alarm is decremented by one. When the number of seconds remaining reaches zero, the OS delivers a signal to the process. Given this, if a process places a 1 second alarm request 1 microsecond before the OS does its alarm processing, the signal will arrive in 1 microsecond, not in 1 second. An alarm set for N seconds actually means:
deliver a signal after N-1 seconds, but before N seconds.
If you'd like to see for yourself, try running this shell script:
$ for i in 1 2 3 4 5 6 7 8 9 10 > do > time sleep 2 > done
How many times did the process actually take more than 2 seconds? If you try it with both the Korn shell, ksh(1), and the System V shell, sh(1), you'll find that under ksh(1) it takes about 1.2 seconds and under sh(1) it takes about 1.8 seconds (There is a difference because sh(1) counts the time required to start the sleep process whereas ksh(1) only counts the actual running time). The sleep(1) program rarely, if ever, actually sleeps for 2 seconds.
If you are writing a daemon that checks for some event every 5 minutes, or if you want to pause the output to give the user a chance to read it, alarm's 1 second granularity is fine. But what about that daemon that needs to wake up every second? Waking up after 1 microsecond could cause the process to run almost continuously. For any sort of realtime processing, one second is a very long time. So how do you sleep for less than one second reliably?
The answer is do not use an alarm, use an interval timer.
Interval Timers
Each interval timer has a resolution of 1 tick of the system's clock, or 1 microsecond (whichever is larger). Additionally, you can configure an interval timer to automatically restart itself. The system provides each process with three independent interval timers:
ITIMER_REAL
This timer will count down in real time. That is, this timer will continue to run when your process is waiting for the OS to perform a system call, or when the OS preempts your process. When the timer expires, the OS will deliver a SIGALARM signal.
ITIMER_VIRTUAL
This timer counts down only when your process is running. If your process makes a system call, or is preempted, this timer will stop counting. The timer will resume when your process resumes execution. When this timer reaches zero, the process will receive a SIGVTALRM signal.
Possible uses for this timer include checkpointing (saving data after some period of execution) and multithreading. The virtual timer is more desireable for these applications since it counts only when the process is running; there is no reason to perform a checkpoint or switch threads if the process has been idle.
ITIMER_PROF
This timer will stop counting any time your process is preempted by the OS, but will not stop when the process is waiting for a system call to return. When it expires the OS generates a SIGPROF signal.
This timer is designed to be used for execution profiling by interpreters. By having a profiling timer send a signal every second, or fraction of a second, and examining the current position in the interpreted code, the process can determine where the most execution time is being spent.
All three timers operate on the following structure:
struct itimerval { struct timeval it_interval; struct timeval it_value; }
The timeval structure looks like this:
struct timeval { long tv_sec; long tv_usec; }
Both of these structures are defined in the <sys/time.h> include file.
Note that System V Release 4.0 does not guarantee that these are the only members of these structures, nor that they will occur in this order. You must initialize the members individually. This can be annoying and tedious, but it allows the structure to be expanded in future releases.
You set and examine timers using these two functions:
int getitimer(int which, struct itimerval *myvalue)
places the current timer setting into myvalue
int setitimer(int which, struct itimerval *myvalue, struct itimerval *myovalue);
sets the timer. It gets the new time from myvalue and places the previous setting in myovalue.
Now, let us take a look at the sleep function again. The code in example 2 creates a new version of sleep that will sleep an exact number of seconds.
Example 2: SLEEP() Using an Interval Timer ------------------------------------------ 1 # include <signal.h> 2 # include <sys/time.h> 3 static void trap () 4 { 5 } 6 unsigned mysleep ( unsigned duration ) 7 { 8 void (*oldsig)(); 9 struct itimerval oldtime; 10 struct itimerval newtime; 11 oldsig = signal(SIGALRM, trap); 12 getitimer(ITIMER_REAL, &oldtime); 13 if (oldtime.it_value.tv_sec == 0 && oldtime.it_value.tv_usec == 0 || oldtime.it_value.tv_sec >= duration) 14 { 15 newtime.it_interval.tv_sec = 0; 16 newtime.it_interval.tv_usec = 0; 17 newtime.it_value.tv_sec = duration; 18 newtime.it_value.tv_usec = 0; 19 setitimer(ITIMER_REAL, &newtime, NULL); 20 } 21 pause(); 22 signal(SIGALRM, oldsig); 23 if (oldtime.it_value.tv_sec > duration) 24 { 25 oldtime.it_value.tv_sec -= duration; 26 setitimer(ITIMER_REAL, &newtime, NULL); 27 } 28 if (oldtime && oldtime < n) 29 return(n - oldtime); 30 else 31 return 0; 32 } Line Explanation ---- ----------- 1-2 The file signal.h defines the parameters of the signal(2) function. The file <sys/time.h> defines ITIMER_REAL and the structures used by getitimer(3) and setitimer(3). 3-5 trap() is the simple signal handler that just sets a flag that another piece of code will examine. 6-7 Define the mysleep() function. 8-10 oldsig will hold the previous state of the signal handler; oldtime will hold the state of the timer; and newtime will be used to set the new timer parameters. 11 Establish trap() as the current signal handler for the alarm signal. The previous handler is saved in oldsig. 12 Fetch the current settings of the timer. 13-20 If the timer was idle (both parts of it_value are zero) or it is set to go off later than duration (it_value.tv_sec is greater than duration), set the timer to go off in duration seconds. 21 pause(2) the process until the timer expires. 22 Restore the old signal handler. 23-27 If the old timer setting was later than duration, reset the timer with the difference between duration and oldtime.it_value.tv_sec (the time remaining until expiration of the previous setting). 28-32 If an existing timer is making mysleep() return early, return the time remaining on the requested mysleep().
This example uses what is called a one-shot timer. The timer goes off once, and then stops. By supplying an interval setting, the timer becomes a clock, generating alarm signals on a regular basis.
The code fragment in Example 3 shows how to set the timer to produce a 1.5 second clock that will start ticking in 1 minute.
Example 3: Using an Interval Timer as a Clock --------------------------------------------- 1 # include <sys/time.h> 2 ... 3 { 4 struct itimerval newtime; 5 newtime.it_value.tv_sec = 60; 6 newtime.it_value.tv_usec = 0; 7 newtime.it_interval.tv_sec = 5; 8 newtime.it_interval.tv_usec = 500000; 9 setitimer(ITIMER_REAL, &newtime, 0); 10 } 11 ... Line Explanation ---- ----------- 1 The file <sys/time.h> defines ITIMER_REAL and the structures used by getitimer(3) and setitimer(3). 2 Other code. 3 Start of block. 4 Structure newtime will hold the setting for the timer. 5-6 Set it_value to expire in 1 minute. 7-8 Set it_interval to reload it_value with 5 seconds and 500,000. microseconds (one half of a second). 9 Load the new settings into the ITIMER_REAL timer. 10 End of block. 11 Other code.
When the time interval specified by it_value expires, the contents of it_interval is copied into it_value and the timer is restarted. If the interval specified by it_interval is zero, the timer stops. Timers can be stopped at anytime by calling setitimer(3) with the members of it_value set to zero.
So there you have the realtime interval timer. The other timers work exactly the same way, varying only in when the timer is running. For more information on interval timers see UNIX System V Release 4 - Programmer's Reference Manual, published by Prentice Hall. Next time, we'll explore context switching, multithreading, and light weight processes.