/usr/share/doc/glibc-doc/html/libc_13.html is in glibc-doc 2.15-0ubuntu10.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
<html>
<!-- This file documents the GNU C library.
This is Edition 0.13, last updated 2011-07-19,
of The GNU C Library Reference Manual, for version
2.14 (Ubuntu EGLIBC 2.15-0ubuntu10) .
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007, 2008, 2010, 2011 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Free Software Needs Free Documentation"
and "GNU Lesser General Public License", the Front-Cover texts being
"A GNU Manual", and with the Back-Cover Texts as in (a) below. A
copy of the license is included in the section entitled "GNU Free
Documentation License".
(a) The FSF's Back-Cover Text is: "You have the freedom to
copy and modify this GNU manual. Buying copies from the FSF
supports it in developing GNU and promoting software freedom."
-->
<!-- Created on April 20, 2012 by texi2html 1.82
texi2html was written by:
Lionel Cons <Lionel.Cons@cern.ch> (original author)
Karl Berry <karl@freefriends.org>
Olaf Bachmann <obachman@mathematik.uni-kl.de>
and many others.
Maintained by: Many creative people.
Send bugs and suggestions to <texi2html-bug@nongnu.org>
-->
<head>
<title>The GNU C Library: 13. Low-Level Input/Output</title>
<meta name="description" content="The GNU C Library: 13. Low-Level Input/Output">
<meta name="keywords" content="The GNU C Library: 13. Low-Level Input/Output">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="texi2html 1.82">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
pre.display {font-family: serif}
pre.format {font-family: serif}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: serif; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: serif; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.roman {font-family:serif; font-weight:normal;}
span.sansserif {font-family:sans-serif; font-weight:normal;}
ul.toc {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Low_002dLevel-I_002fO"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="libc_12.html#Example" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Opening-and-Closing-Files" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc_12.html#I_002fO-on-Streams" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Low_002dLevel-Input_002fOutput"></a>
<h1 class="chapter">13. Low-Level Input/Output</h1>
<p>This chapter describes functions for performing low-level input/output
operations on file descriptors. These functions include the primitives
for the higher-level I/O functions described in <a href="libc_12.html#I_002fO-on-Streams">Input/Output on Streams</a>, as
well as functions for performing low-level control operations for which
there are no equivalents on streams.
</p>
<p>Stream-level I/O is more flexible and usually more convenient;
therefore, programmers generally use the descriptor-level functions only
when necessary. These are some of the usual reasons:
</p>
<ul>
<li>
For reading binary files in large chunks.
</li><li>
For reading an entire file into core before parsing it.
</li><li>
To perform operations other than data transfer, which can only be done
with a descriptor. (You can use <code>fileno</code> to get the descriptor
corresponding to a stream.)
</li><li>
To pass descriptors to a child process. (The child can create its own
stream to use a descriptor that it inherits, but cannot inherit a stream
directly.)
</li></ul>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Opening-and-Closing-Files">13.1 Opening and Closing Files</a></td><td> </td><td align="left" valign="top"> How to open and close file
descriptors.
</td></tr>
<tr><td align="left" valign="top"><a href="#I_002fO-Primitives">13.2 Input and Output Primitives</a></td><td> </td><td align="left" valign="top"> Reading and writing data.
</td></tr>
<tr><td align="left" valign="top"><a href="#File-Position-Primitive">13.3 Setting the File Position of a Descriptor</a></td><td> </td><td align="left" valign="top"> Setting a descriptor’s file
position.
</td></tr>
<tr><td align="left" valign="top"><a href="#Descriptors-and-Streams">13.4 Descriptors and Streams</a></td><td> </td><td align="left" valign="top"> Converting descriptor to stream
or vice-versa.
</td></tr>
<tr><td align="left" valign="top"><a href="#Stream_002fDescriptor-Precautions">13.5 Dangers of Mixing Streams and Descriptors</a></td><td> </td><td align="left" valign="top"> Precautions needed if you use both
descriptors and streams.
</td></tr>
<tr><td align="left" valign="top"><a href="#Scatter_002dGather">13.6 Fast Scatter-Gather I/O</a></td><td> </td><td align="left" valign="top"> Fast I/O to discontinuous buffers.
</td></tr>
<tr><td align="left" valign="top"><a href="#Memory_002dmapped-I_002fO">13.7 Memory-mapped I/O</a></td><td> </td><td align="left" valign="top"> Using files like memory.
</td></tr>
<tr><td align="left" valign="top"><a href="#Waiting-for-I_002fO">13.8 Waiting for Input or Output</a></td><td> </td><td align="left" valign="top"> How to check for input or output
on multiple file descriptors.
</td></tr>
<tr><td align="left" valign="top"><a href="#Synchronizing-I_002fO">13.9 Synchronizing I/O operations</a></td><td> </td><td align="left" valign="top"> Making sure all I/O actions completed.
</td></tr>
<tr><td align="left" valign="top"><a href="#Asynchronous-I_002fO">13.10 Perform I/O Operations in Parallel</a></td><td> </td><td align="left" valign="top"> Perform I/O in parallel.
</td></tr>
<tr><td align="left" valign="top"><a href="#Control-Operations">13.11 Control Operations on Files</a></td><td> </td><td align="left" valign="top"> Various other operations on file
descriptors.
</td></tr>
<tr><td align="left" valign="top"><a href="#Duplicating-Descriptors">13.12 Duplicating Descriptors</a></td><td> </td><td align="left" valign="top"> Fcntl commands for duplicating
file descriptors.
</td></tr>
<tr><td align="left" valign="top"><a href="#Descriptor-Flags">13.13 File Descriptor Flags</a></td><td> </td><td align="left" valign="top"> Fcntl commands for manipulating
flags associated with file
descriptors.
</td></tr>
<tr><td align="left" valign="top"><a href="#File-Status-Flags">13.14 File Status Flags</a></td><td> </td><td align="left" valign="top"> Fcntl commands for manipulating
flags associated with open files.
</td></tr>
<tr><td align="left" valign="top"><a href="#File-Locks">13.15 File Locks</a></td><td> </td><td align="left" valign="top"> Fcntl commands for implementing
file locking.
</td></tr>
<tr><td align="left" valign="top"><a href="#Interrupt-Input">13.16 Interrupt-Driven Input</a></td><td> </td><td align="left" valign="top"> Getting an asynchronous signal when
input arrives.
</td></tr>
<tr><td align="left" valign="top"><a href="#IOCTLs">13.17 Generic I/O Control operations</a></td><td> </td><td align="left" valign="top"></td></tr>
</table>
<hr size="6">
<a name="Opening-and-Closing-Files"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#I_002fO-Primitives" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Opening-and-Closing-Files-1"></a>
<h2 class="section">13.1 Opening and Closing Files</h2>
<a name="index-opening-a-file-descriptor"></a>
<a name="index-closing-a-file-descriptor"></a>
<p>This section describes the primitives for opening and closing files
using file descriptors. The <code>open</code> and <code>creat</code> functions are
declared in the header file ‘<tt>fcntl.h</tt>’, while <code>close</code> is
declared in ‘<tt>unistd.h</tt>’.
<a name="index-unistd_002eh"></a>
<a name="index-fcntl_002eh-1"></a>
</p>
<dl>
<dt><a name="index-open"></a><u>Function:</u> int <b>open</b><i> (const char *<var>filename</var>, int <var>flags</var>[, mode_t <var>mode</var>])</i></dt>
<dd><p>The <code>open</code> function creates and returns a new file descriptor
for the file named by <var>filename</var>. Initially, the file position
indicator for the file is at the beginning of the file. The argument
<var>mode</var> is used only when a file is created, but it doesn’t hurt
to supply the argument in any case.
</p>
<p>The <var>flags</var> argument controls how the file is to be opened. This is
a bit mask; you create the value by the bitwise OR of the appropriate
parameters (using the ‘<samp>|</samp>’ operator in C).
See section <a href="#File-Status-Flags">File Status Flags</a>, for the parameters available.
</p>
<p>The normal return value from <code>open</code> is a non-negative integer file
descriptor. In the case of an error, a value of <em>-1</em> is returned
instead. In addition to the usual file name errors (see section <a href="libc_11.html#File-Name-Errors">File Name Errors</a>), the following <code>errno</code> error conditions are defined
for this function:
</p>
<dl compact="compact">
<dt> <code>EACCES</code></dt>
<dd><p>The file exists but is not readable/writable as requested by the <var>flags</var>
argument, the file does not exist and the directory is unwritable so
it cannot be created.
</p>
</dd>
<dt> <code>EEXIST</code></dt>
<dd><p>Both <code>O_CREAT</code> and <code>O_EXCL</code> are set, and the named file already
exists.
</p>
</dd>
<dt> <code>EINTR</code></dt>
<dd><p>The <code>open</code> operation was interrupted by a signal.
See section <a href="libc_24.html#Interrupted-Primitives">Primitives Interrupted by Signals</a>.
</p>
</dd>
<dt> <code>EISDIR</code></dt>
<dd><p>The <var>flags</var> argument specified write access, and the file is a directory.
</p>
</dd>
<dt> <code>EMFILE</code></dt>
<dd><p>The process has too many files open.
The maximum number of file descriptors is controlled by the
<code>RLIMIT_NOFILE</code> resource limit; see section <a href="libc_22.html#Limits-on-Resources">Limiting Resource Usage</a>.
</p>
</dd>
<dt> <code>ENFILE</code></dt>
<dd><p>The entire system, or perhaps the file system which contains the
directory, cannot support any additional open files at the moment.
(This problem cannot happen on the GNU system.)
</p>
</dd>
<dt> <code>ENOENT</code></dt>
<dd><p>The named file does not exist, and <code>O_CREAT</code> is not specified.
</p>
</dd>
<dt> <code>ENOSPC</code></dt>
<dd><p>The directory or file system that would contain the new file cannot be
extended, because there is no disk space left.
</p>
</dd>
<dt> <code>ENXIO</code></dt>
<dd><p><code>O_NONBLOCK</code> and <code>O_WRONLY</code> are both set in the <var>flags</var>
argument, the file named by <var>filename</var> is a FIFO (see section <a href="libc_15.html#Pipes-and-FIFOs">Pipes and FIFOs</a>), and no process has the file open for reading.
</p>
</dd>
<dt> <code>EROFS</code></dt>
<dd><p>The file resides on a read-only file system and any of <code>O_WRONLY</code>,
<code>O_RDWR</code>, and <code>O_TRUNC</code> are set in the <var>flags</var> argument,
or <code>O_CREAT</code> is set and the file does not already exist.
</p></dd>
</dl>
<p>If on a 32 bit machine the sources are translated with
<code>_FILE_OFFSET_BITS == 64</code> the function <code>open</code> returns a file
descriptor opened in the large file mode which enables the file handling
functions to use files up to <em>2^63</em> bytes in size and offset from
<em>-2^63</em> to <em>2^63</em>. This happens transparently for the user
since all of the lowlevel file handling functions are equally replaced.
</p>
<p>This function is a cancellation point in multi-threaded programs. This
is a problem if the thread allocates some resources (like memory, file
descriptors, semaphores or whatever) at the time <code>open</code> is
called. If the thread gets canceled these resources stay allocated
until the program ends. To avoid this calls to <code>open</code> should be
protected using cancellation handlers.
</p>
<p>The <code>open</code> function is the underlying primitive for the <code>fopen</code>
and <code>freopen</code> functions, that create streams.
</p></dd></dl>
<dl>
<dt><a name="index-open64"></a><u>Function:</u> int <b>open64</b><i> (const char *<var>filename</var>, int <var>flags</var>[, mode_t <var>mode</var>])</i></dt>
<dd><p>This function is similar to <code>open</code>. It returns a file descriptor
which can be used to access the file named by <var>filename</var>. The only
difference is that on 32 bit systems the file is opened in the
large file mode. I.e., file length and file offsets can exceed 31 bits.
</p>
<p>When the sources are translated with <code>_FILE_OFFSET_BITS == 64</code> this
function is actually available under the name <code>open</code>. I.e., the
new, extended API using 64 bit file sizes and offsets transparently
replaces the old API.
</p></dd></dl>
<dl>
<dt><a name="index-creat"></a><u>Obsolete function:</u> int <b>creat</b><i> (const char *<var>filename</var>, mode_t <var>mode</var>)</i></dt>
<dd><p>This function is obsolete. The call:
</p>
<table><tr><td> </td><td><pre class="smallexample">creat (<var>filename</var>, <var>mode</var>)
</pre></td></tr></table>
<p>is equivalent to:
</p>
<table><tr><td> </td><td><pre class="smallexample">open (<var>filename</var>, O_WRONLY | O_CREAT | O_TRUNC, <var>mode</var>)
</pre></td></tr></table>
<p>If on a 32 bit machine the sources are translated with
<code>_FILE_OFFSET_BITS == 64</code> the function <code>creat</code> returns a file
descriptor opened in the large file mode which enables the file handling
functions to use files up to <em>2^63</em> in size and offset from
<em>-2^63</em> to <em>2^63</em>. This happens transparently for the user
since all of the lowlevel file handling functions are equally replaced.
</p></dd></dl>
<dl>
<dt><a name="index-creat64"></a><u>Obsolete function:</u> int <b>creat64</b><i> (const char *<var>filename</var>, mode_t <var>mode</var>)</i></dt>
<dd><p>This function is similar to <code>creat</code>. It returns a file descriptor
which can be used to access the file named by <var>filename</var>. The only
the difference is that on 32 bit systems the file is opened in the
large file mode. I.e., file length and file offsets can exceed 31 bits.
</p>
<p>To use this file descriptor one must not use the normal operations but
instead the counterparts named <code>*64</code>, e.g., <code>read64</code>.
</p>
<p>When the sources are translated with <code>_FILE_OFFSET_BITS == 64</code> this
function is actually available under the name <code>open</code>. I.e., the
new, extended API using 64 bit file sizes and offsets transparently
replaces the old API.
</p></dd></dl>
<dl>
<dt><a name="index-close"></a><u>Function:</u> int <b>close</b><i> (int <var>filedes</var>)</i></dt>
<dd><p>The function <code>close</code> closes the file descriptor <var>filedes</var>.
Closing a file has the following consequences:
</p>
<ul>
<li>
The file descriptor is deallocated.
</li><li>
Any record locks owned by the process on the file are unlocked.
</li><li>
When all file descriptors associated with a pipe or FIFO have been closed,
any unread data is discarded.
</li></ul>
<p>This function is a cancellation point in multi-threaded programs. This
is a problem if the thread allocates some resources (like memory, file
descriptors, semaphores or whatever) at the time <code>close</code> is
called. If the thread gets canceled these resources stay allocated
until the program ends. To avoid this, calls to <code>close</code> should be
protected using cancellation handlers.
</p>
<p>The normal return value from <code>close</code> is <em>0</em>; a value of <em>-1</em>
is returned in case of failure. The following <code>errno</code> error
conditions are defined for this function:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The <var>filedes</var> argument is not a valid file descriptor.
</p>
</dd>
<dt> <code>EINTR</code></dt>
<dd><p>The <code>close</code> call was interrupted by a signal.
See section <a href="libc_24.html#Interrupted-Primitives">Primitives Interrupted by Signals</a>.
Here is an example of how to handle <code>EINTR</code> properly:
</p>
<table><tr><td> </td><td><pre class="smallexample">TEMP_FAILURE_RETRY (close (desc));
</pre></td></tr></table>
</dd>
<dt> <code>ENOSPC</code></dt>
<dt> <code>EIO</code></dt>
<dt> <code>EDQUOT</code></dt>
<dd><p>When the file is accessed by NFS, these errors from <code>write</code> can sometimes
not be detected until <code>close</code>. See section <a href="#I_002fO-Primitives">Input and Output Primitives</a>, for details
on their meaning.
</p></dd>
</dl>
<p>Please note that there is <em>no</em> separate <code>close64</code> function.
This is not necessary since this function does not determine nor depend
on the mode of the file. The kernel which performs the <code>close</code>
operation knows which mode the descriptor is used for and can handle
this situation.
</p></dd></dl>
<p>To close a stream, call <code>fclose</code> (see section <a href="libc_12.html#Closing-Streams">Closing Streams</a>) instead
of trying to close its underlying file descriptor with <code>close</code>.
This flushes any buffered output and updates the stream object to
indicate that it is closed.
</p>
<hr size="6">
<a name="I_002fO-Primitives"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Opening-and-Closing-Files" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#File-Position-Primitive" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Input-and-Output-Primitives"></a>
<h2 class="section">13.2 Input and Output Primitives</h2>
<p>This section describes the functions for performing primitive input and
output operations on file descriptors: <code>read</code>, <code>write</code>, and
<code>lseek</code>. These functions are declared in the header file
‘<tt>unistd.h</tt>’.
<a name="index-unistd_002eh-1"></a>
</p>
<dl>
<dt><a name="index-ssize_005ft"></a><u>Data Type:</u> <b>ssize_t</b></dt>
<dd><p>This data type is used to represent the sizes of blocks that can be
read or written in a single operation. It is similar to <code>size_t</code>,
but must be a signed type.
</p></dd></dl>
<a name="index-reading-from-a-file-descriptor"></a>
<dl>
<dt><a name="index-read"></a><u>Function:</u> ssize_t <b>read</b><i> (int <var>filedes</var>, void *<var>buffer</var>, size_t <var>size</var>)</i></dt>
<dd><p>The <code>read</code> function reads up to <var>size</var> bytes from the file
with descriptor <var>filedes</var>, storing the results in the <var>buffer</var>.
(This is not necessarily a character string, and no terminating null
character is added.)
</p>
<a name="index-end_002dof_002dfile_002c-on-a-file-descriptor"></a>
<p>The return value is the number of bytes actually read. This might be
less than <var>size</var>; for example, if there aren’t that many bytes left
in the file or if there aren’t that many bytes immediately available.
The exact behavior depends on what kind of file it is. Note that
reading less than <var>size</var> bytes is not an error.
</p>
<p>A value of zero indicates end-of-file (except if the value of the
<var>size</var> argument is also zero). This is not considered an error.
If you keep calling <code>read</code> while at end-of-file, it will keep
returning zero and doing nothing else.
</p>
<p>If <code>read</code> returns at least one character, there is no way you can
tell whether end-of-file was reached. But if you did reach the end, the
next read will return zero.
</p>
<p>In case of an error, <code>read</code> returns <em>-1</em>. The following
<code>errno</code> error conditions are defined for this function:
</p>
<dl compact="compact">
<dt> <code>EAGAIN</code></dt>
<dd><p>Normally, when no input is immediately available, <code>read</code> waits for
some input. But if the <code>O_NONBLOCK</code> flag is set for the file
(see section <a href="#File-Status-Flags">File Status Flags</a>), <code>read</code> returns immediately without
reading any data, and reports this error.
</p>
<p><strong>Compatibility Note:</strong> Most versions of BSD Unix use a different
error code for this: <code>EWOULDBLOCK</code>. In the GNU library,
<code>EWOULDBLOCK</code> is an alias for <code>EAGAIN</code>, so it doesn’t matter
which name you use.
</p>
<p>On some systems, reading a large amount of data from a character special
file can also fail with <code>EAGAIN</code> if the kernel cannot find enough
physical memory to lock down the user’s pages. This is limited to
devices that transfer with direct memory access into the user’s memory,
which means it does not include terminals, since they always use
separate buffers inside the kernel. This problem never happens in the
GNU system.
</p>
<p>Any condition that could result in <code>EAGAIN</code> can instead result in a
successful <code>read</code> which returns fewer bytes than requested.
Calling <code>read</code> again immediately would result in <code>EAGAIN</code>.
</p>
</dd>
<dt> <code>EBADF</code></dt>
<dd><p>The <var>filedes</var> argument is not a valid file descriptor,
or is not open for reading.
</p>
</dd>
<dt> <code>EINTR</code></dt>
<dd><p><code>read</code> was interrupted by a signal while it was waiting for input.
See section <a href="libc_24.html#Interrupted-Primitives">Primitives Interrupted by Signals</a>. A signal will not necessary cause
<code>read</code> to return <code>EINTR</code>; it may instead result in a
successful <code>read</code> which returns fewer bytes than requested.
</p>
</dd>
<dt> <code>EIO</code></dt>
<dd><p>For many devices, and for disk files, this error code indicates
a hardware error.
</p>
<p><code>EIO</code> also occurs when a background process tries to read from the
controlling terminal, and the normal action of stopping the process by
sending it a <code>SIGTTIN</code> signal isn’t working. This might happen if
the signal is being blocked or ignored, or because the process group is
orphaned. See section <a href="libc_27.html#Job-Control">Job Control</a>, for more information about job control,
and <a href="libc_24.html#Signal-Handling">Signal Handling</a>, for information about signals.
</p>
</dd>
<dt> <code>EINVAL</code></dt>
<dd><p>In some systems, when reading from a character or block device, position
and size offsets must be aligned to a particular block size. This error
indicates that the offsets were not properly aligned.
</p></dd>
</dl>
<p>Please note that there is no function named <code>read64</code>. This is not
necessary since this function does not directly modify or handle the
possibly wide file offset. Since the kernel handles this state
internally, the <code>read</code> function can be used for all cases.
</p>
<p>This function is a cancellation point in multi-threaded programs. This
is a problem if the thread allocates some resources (like memory, file
descriptors, semaphores or whatever) at the time <code>read</code> is
called. If the thread gets canceled these resources stay allocated
until the program ends. To avoid this, calls to <code>read</code> should be
protected using cancellation handlers.
</p>
<p>The <code>read</code> function is the underlying primitive for all of the
functions that read from streams, such as <code>fgetc</code>.
</p></dd></dl>
<dl>
<dt><a name="index-pread"></a><u>Function:</u> ssize_t <b>pread</b><i> (int <var>filedes</var>, void *<var>buffer</var>, size_t <var>size</var>, off_t <var>offset</var>)</i></dt>
<dd><p>The <code>pread</code> function is similar to the <code>read</code> function. The
first three arguments are identical, and the return values and error
codes also correspond.
</p>
<p>The difference is the fourth argument and its handling. The data block
is not read from the current position of the file descriptor
<code>filedes</code>. Instead the data is read from the file starting at
position <var>offset</var>. The position of the file descriptor itself is
not affected by the operation. The value is the same as before the call.
</p>
<p>When the source file is compiled with <code>_FILE_OFFSET_BITS == 64</code> the
<code>pread</code> function is in fact <code>pread64</code> and the type
<code>off_t</code> has 64 bits, which makes it possible to handle files up to
<em>2^63</em> bytes in length.
</p>
<p>The return value of <code>pread</code> describes the number of bytes read.
In the error case it returns <em>-1</em> like <code>read</code> does and the
error codes are also the same, with these additions:
</p>
<dl compact="compact">
<dt> <code>EINVAL</code></dt>
<dd><p>The value given for <var>offset</var> is negative and therefore illegal.
</p>
</dd>
<dt> <code>ESPIPE</code></dt>
<dd><p>The file descriptor <var>filedes</var> is associate with a pipe or a FIFO and
this device does not allow positioning of the file pointer.
</p></dd>
</dl>
<p>The function is an extension defined in the Unix Single Specification
version 2.
</p></dd></dl>
<dl>
<dt><a name="index-pread64"></a><u>Function:</u> ssize_t <b>pread64</b><i> (int <var>filedes</var>, void *<var>buffer</var>, size_t <var>size</var>, off64_t <var>offset</var>)</i></dt>
<dd><p>This function is similar to the <code>pread</code> function. The difference
is that the <var>offset</var> parameter is of type <code>off64_t</code> instead of
<code>off_t</code> which makes it possible on 32 bit machines to address
files larger than <em>2^31</em> bytes and up to <em>2^63</em> bytes. The
file descriptor <code>filedes</code> must be opened using <code>open64</code> since
otherwise the large offsets possible with <code>off64_t</code> will lead to
errors with a descriptor in small file mode.
</p>
<p>When the source file is compiled with <code>_FILE_OFFSET_BITS == 64</code> on a
32 bit machine this function is actually available under the name
<code>pread</code> and so transparently replaces the 32 bit interface.
</p></dd></dl>
<a name="index-writing-to-a-file-descriptor"></a>
<dl>
<dt><a name="index-write"></a><u>Function:</u> ssize_t <b>write</b><i> (int <var>filedes</var>, const void *<var>buffer</var>, size_t <var>size</var>)</i></dt>
<dd><p>The <code>write</code> function writes up to <var>size</var> bytes from
<var>buffer</var> to the file with descriptor <var>filedes</var>. The data in
<var>buffer</var> is not necessarily a character string and a null character is
output like any other character.
</p>
<p>The return value is the number of bytes actually written. This may be
<var>size</var>, but can always be smaller. Your program should always call
<code>write</code> in a loop, iterating until all the data is written.
</p>
<p>Once <code>write</code> returns, the data is enqueued to be written and can be
read back right away, but it is not necessarily written out to permanent
storage immediately. You can use <code>fsync</code> when you need to be sure
your data has been permanently stored before continuing. (It is more
efficient for the system to batch up consecutive writes and do them all
at once when convenient. Normally they will always be written to disk
within a minute or less.) Modern systems provide another function
<code>fdatasync</code> which guarantees integrity only for the file data and
is therefore faster.
You can use the <code>O_FSYNC</code> open mode to make <code>write</code> always
store the data to disk before returning; see section <a href="#Operating-Modes">I/O Operating Modes</a>.
</p>
<p>In the case of an error, <code>write</code> returns <em>-1</em>. The following
<code>errno</code> error conditions are defined for this function:
</p>
<dl compact="compact">
<dt> <code>EAGAIN</code></dt>
<dd><p>Normally, <code>write</code> blocks until the write operation is complete.
But if the <code>O_NONBLOCK</code> flag is set for the file (see section <a href="#Control-Operations">Control Operations on Files</a>), it returns immediately without writing any data and
reports this error. An example of a situation that might cause the
process to block on output is writing to a terminal device that supports
flow control, where output has been suspended by receipt of a STOP
character.
</p>
<p><strong>Compatibility Note:</strong> Most versions of BSD Unix use a different
error code for this: <code>EWOULDBLOCK</code>. In the GNU library,
<code>EWOULDBLOCK</code> is an alias for <code>EAGAIN</code>, so it doesn’t matter
which name you use.
</p>
<p>On some systems, writing a large amount of data from a character special
file can also fail with <code>EAGAIN</code> if the kernel cannot find enough
physical memory to lock down the user’s pages. This is limited to
devices that transfer with direct memory access into the user’s memory,
which means it does not include terminals, since they always use
separate buffers inside the kernel. This problem does not arise in the
GNU system.
</p>
</dd>
<dt> <code>EBADF</code></dt>
<dd><p>The <var>filedes</var> argument is not a valid file descriptor,
or is not open for writing.
</p>
</dd>
<dt> <code>EFBIG</code></dt>
<dd><p>The size of the file would become larger than the implementation can support.
</p>
</dd>
<dt> <code>EINTR</code></dt>
<dd><p>The <code>write</code> operation was interrupted by a signal while it was
blocked waiting for completion. A signal will not necessarily cause
<code>write</code> to return <code>EINTR</code>; it may instead result in a
successful <code>write</code> which writes fewer bytes than requested.
See section <a href="libc_24.html#Interrupted-Primitives">Primitives Interrupted by Signals</a>.
</p>
</dd>
<dt> <code>EIO</code></dt>
<dd><p>For many devices, and for disk files, this error code indicates
a hardware error.
</p>
</dd>
<dt> <code>ENOSPC</code></dt>
<dd><p>The device containing the file is full.
</p>
</dd>
<dt> <code>EPIPE</code></dt>
<dd><p>This error is returned when you try to write to a pipe or FIFO that
isn’t open for reading by any process. When this happens, a <code>SIGPIPE</code>
signal is also sent to the process; see <a href="libc_24.html#Signal-Handling">Signal Handling</a>.
</p>
</dd>
<dt> <code>EINVAL</code></dt>
<dd><p>In some systems, when writing to a character or block device, position
and size offsets must be aligned to a particular block size. This error
indicates that the offsets were not properly aligned.
</p></dd>
</dl>
<p>Unless you have arranged to prevent <code>EINTR</code> failures, you should
check <code>errno</code> after each failing call to <code>write</code>, and if the
error was <code>EINTR</code>, you should simply repeat the call.
See section <a href="libc_24.html#Interrupted-Primitives">Primitives Interrupted by Signals</a>. The easy way to do this is with the
macro <code>TEMP_FAILURE_RETRY</code>, as follows:
</p>
<table><tr><td> </td><td><pre class="smallexample">nbytes = TEMP_FAILURE_RETRY (write (desc, buffer, count));
</pre></td></tr></table>
<p>Please note that there is no function named <code>write64</code>. This is not
necessary since this function does not directly modify or handle the
possibly wide file offset. Since the kernel handles this state
internally the <code>write</code> function can be used for all cases.
</p>
<p>This function is a cancellation point in multi-threaded programs. This
is a problem if the thread allocates some resources (like memory, file
descriptors, semaphores or whatever) at the time <code>write</code> is
called. If the thread gets canceled these resources stay allocated
until the program ends. To avoid this, calls to <code>write</code> should be
protected using cancellation handlers.
</p>
<p>The <code>write</code> function is the underlying primitive for all of the
functions that write to streams, such as <code>fputc</code>.
</p></dd></dl>
<dl>
<dt><a name="index-pwrite"></a><u>Function:</u> ssize_t <b>pwrite</b><i> (int <var>filedes</var>, const void *<var>buffer</var>, size_t <var>size</var>, off_t <var>offset</var>)</i></dt>
<dd><p>The <code>pwrite</code> function is similar to the <code>write</code> function. The
first three arguments are identical, and the return values and error codes
also correspond.
</p>
<p>The difference is the fourth argument and its handling. The data block
is not written to the current position of the file descriptor
<code>filedes</code>. Instead the data is written to the file starting at
position <var>offset</var>. The position of the file descriptor itself is
not affected by the operation. The value is the same as before the call.
</p>
<p>When the source file is compiled with <code>_FILE_OFFSET_BITS == 64</code> the
<code>pwrite</code> function is in fact <code>pwrite64</code> and the type
<code>off_t</code> has 64 bits, which makes it possible to handle files up to
<em>2^63</em> bytes in length.
</p>
<p>The return value of <code>pwrite</code> describes the number of written bytes.
In the error case it returns <em>-1</em> like <code>write</code> does and the
error codes are also the same, with these additions:
</p>
<dl compact="compact">
<dt> <code>EINVAL</code></dt>
<dd><p>The value given for <var>offset</var> is negative and therefore illegal.
</p>
</dd>
<dt> <code>ESPIPE</code></dt>
<dd><p>The file descriptor <var>filedes</var> is associated with a pipe or a FIFO and
this device does not allow positioning of the file pointer.
</p></dd>
</dl>
<p>The function is an extension defined in the Unix Single Specification
version 2.
</p></dd></dl>
<dl>
<dt><a name="index-pwrite64"></a><u>Function:</u> ssize_t <b>pwrite64</b><i> (int <var>filedes</var>, const void *<var>buffer</var>, size_t <var>size</var>, off64_t <var>offset</var>)</i></dt>
<dd><p>This function is similar to the <code>pwrite</code> function. The difference
is that the <var>offset</var> parameter is of type <code>off64_t</code> instead of
<code>off_t</code> which makes it possible on 32 bit machines to address
files larger than <em>2^31</em> bytes and up to <em>2^63</em> bytes. The
file descriptor <code>filedes</code> must be opened using <code>open64</code> since
otherwise the large offsets possible with <code>off64_t</code> will lead to
errors with a descriptor in small file mode.
</p>
<p>When the source file is compiled using <code>_FILE_OFFSET_BITS == 64</code> on a
32 bit machine this function is actually available under the name
<code>pwrite</code> and so transparently replaces the 32 bit interface.
</p></dd></dl>
<hr size="6">
<a name="File-Position-Primitive"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#I_002fO-Primitives" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Descriptors-and-Streams" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Setting-the-File-Position-of-a-Descriptor"></a>
<h2 class="section">13.3 Setting the File Position of a Descriptor</h2>
<p>Just as you can set the file position of a stream with <code>fseek</code>, you
can set the file position of a descriptor with <code>lseek</code>. This
specifies the position in the file for the next <code>read</code> or
<code>write</code> operation. See section <a href="libc_12.html#File-Positioning">File Positioning</a>, for more information
on the file position and what it means.
</p>
<p>To read the current file position value from a descriptor, use
<code>lseek (<var>desc</var>, 0, SEEK_CUR)</code>.
</p>
<a name="index-file-positioning-on-a-file-descriptor"></a>
<a name="index-positioning-a-file-descriptor"></a>
<a name="index-seeking-on-a-file-descriptor"></a>
<dl>
<dt><a name="index-lseek"></a><u>Function:</u> off_t <b>lseek</b><i> (int <var>filedes</var>, off_t <var>offset</var>, int <var>whence</var>)</i></dt>
<dd><p>The <code>lseek</code> function is used to change the file position of the
file with descriptor <var>filedes</var>.
</p>
<p>The <var>whence</var> argument specifies how the <var>offset</var> should be
interpreted, in the same way as for the <code>fseek</code> function, and it must
be one of the symbolic constants <code>SEEK_SET</code>, <code>SEEK_CUR</code>, or
<code>SEEK_END</code>.
</p>
<dl compact="compact">
<dt> <code>SEEK_SET</code></dt>
<dd><p>Specifies that <var>whence</var> is a count of characters from the beginning
of the file.
</p>
</dd>
<dt> <code>SEEK_CUR</code></dt>
<dd><p>Specifies that <var>whence</var> is a count of characters from the current
file position. This count may be positive or negative.
</p>
</dd>
<dt> <code>SEEK_END</code></dt>
<dd><p>Specifies that <var>whence</var> is a count of characters from the end of
the file. A negative count specifies a position within the current
extent of the file; a positive count specifies a position past the
current end. If you set the position past the current end, and
actually write data, you will extend the file with zeros up to that
position.
</p></dd>
</dl>
<p>The return value from <code>lseek</code> is normally the resulting file
position, measured in bytes from the beginning of the file.
You can use this feature together with <code>SEEK_CUR</code> to read the
current file position.
</p>
<p>If you want to append to the file, setting the file position to the
current end of file with <code>SEEK_END</code> is not sufficient. Another
process may write more data after you seek but before you write,
extending the file so the position you write onto clobbers their data.
Instead, use the <code>O_APPEND</code> operating mode; see section <a href="#Operating-Modes">I/O Operating Modes</a>.
</p>
<p>You can set the file position past the current end of the file. This
does not by itself make the file longer; <code>lseek</code> never changes the
file. But subsequent output at that position will extend the file.
Characters between the previous end of file and the new position are
filled with zeros. Extending the file in this way can create a
“hole”: the blocks of zeros are not actually allocated on disk, so the
file takes up less space than it appears to; it is then called a
“sparse file”.
<a name="index-sparse-files"></a>
<a name="index-holes-in-files"></a>
</p>
<p>If the file position cannot be changed, or the operation is in some way
invalid, <code>lseek</code> returns a value of <em>-1</em>. The following
<code>errno</code> error conditions are defined for this function:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The <var>filedes</var> is not a valid file descriptor.
</p>
</dd>
<dt> <code>EINVAL</code></dt>
<dd><p>The <var>whence</var> argument value is not valid, or the resulting
file offset is not valid. A file offset is invalid.
</p>
</dd>
<dt> <code>ESPIPE</code></dt>
<dd><p>The <var>filedes</var> corresponds to an object that cannot be positioned,
such as a pipe, FIFO or terminal device. (POSIX.1 specifies this error
only for pipes and FIFOs, but in the GNU system, you always get
<code>ESPIPE</code> if the object is not seekable.)
</p></dd>
</dl>
<p>When the source file is compiled with <code>_FILE_OFFSET_BITS == 64</code> the
<code>lseek</code> function is in fact <code>lseek64</code> and the type
<code>off_t</code> has 64 bits which makes it possible to handle files up to
<em>2^63</em> bytes in length.
</p>
<p>This function is a cancellation point in multi-threaded programs. This
is a problem if the thread allocates some resources (like memory, file
descriptors, semaphores or whatever) at the time <code>lseek</code> is
called. If the thread gets canceled these resources stay allocated
until the program ends. To avoid this calls to <code>lseek</code> should be
protected using cancellation handlers.
</p>
<p>The <code>lseek</code> function is the underlying primitive for the
<code>fseek</code>, <code>fseeko</code>, <code>ftell</code>, <code>ftello</code> and
<code>rewind</code> functions, which operate on streams instead of file
descriptors.
</p></dd></dl>
<dl>
<dt><a name="index-lseek64"></a><u>Function:</u> off64_t <b>lseek64</b><i> (int <var>filedes</var>, off64_t <var>offset</var>, int <var>whence</var>)</i></dt>
<dd><p>This function is similar to the <code>lseek</code> function. The difference
is that the <var>offset</var> parameter is of type <code>off64_t</code> instead of
<code>off_t</code> which makes it possible on 32 bit machines to address
files larger than <em>2^31</em> bytes and up to <em>2^63</em> bytes. The
file descriptor <code>filedes</code> must be opened using <code>open64</code> since
otherwise the large offsets possible with <code>off64_t</code> will lead to
errors with a descriptor in small file mode.
</p>
<p>When the source file is compiled with <code>_FILE_OFFSET_BITS == 64</code> on a
32 bits machine this function is actually available under the name
<code>lseek</code> and so transparently replaces the 32 bit interface.
</p></dd></dl>
<p>You can have multiple descriptors for the same file if you open the file
more than once, or if you duplicate a descriptor with <code>dup</code>.
Descriptors that come from separate calls to <code>open</code> have independent
file positions; using <code>lseek</code> on one descriptor has no effect on the
other. For example,
</p>
<table><tr><td> </td><td><pre class="smallexample">{
int d1, d2;
char buf[4];
d1 = open ("foo", O_RDONLY);
d2 = open ("foo", O_RDONLY);
lseek (d1, 1024, SEEK_SET);
read (d2, buf, 4);
}
</pre></td></tr></table>
<p>will read the first four characters of the file ‘<tt>foo</tt>’. (The
error-checking code necessary for a real program has been omitted here
for brevity.)
</p>
<p>By contrast, descriptors made by duplication share a common file
position with the original descriptor that was duplicated. Anything
which alters the file position of one of the duplicates, including
reading or writing data, affects all of them alike. Thus, for example,
</p>
<table><tr><td> </td><td><pre class="smallexample">{
int d1, d2, d3;
char buf1[4], buf2[4];
d1 = open ("foo", O_RDONLY);
d2 = dup (d1);
d3 = dup (d2);
lseek (d3, 1024, SEEK_SET);
read (d1, buf1, 4);
read (d2, buf2, 4);
}
</pre></td></tr></table>
<p>will read four characters starting with the 1024’th character of
‘<tt>foo</tt>’, and then four more characters starting with the 1028’th
character.
</p>
<dl>
<dt><a name="index-off_005ft"></a><u>Data Type:</u> <b>off_t</b></dt>
<dd><p>This is an arithmetic data type used to represent file sizes.
In the GNU system, this is equivalent to <code>fpos_t</code> or <code>long int</code>.
</p>
<p>If the source is compiled with <code>_FILE_OFFSET_BITS == 64</code> this type
is transparently replaced by <code>off64_t</code>.
</p></dd></dl>
<dl>
<dt><a name="index-off64_005ft"></a><u>Data Type:</u> <b>off64_t</b></dt>
<dd><p>This type is used similar to <code>off_t</code>. The difference is that even
on 32 bit machines, where the <code>off_t</code> type would have 32 bits,
<code>off64_t</code> has 64 bits and so is able to address files up to
<em>2^63</em> bytes in length.
</p>
<p>When compiling with <code>_FILE_OFFSET_BITS == 64</code> this type is
available under the name <code>off_t</code>.
</p></dd></dl>
<p>These aliases for the ‘<samp>SEEK_…</samp>’ constants exist for the sake
of compatibility with older BSD systems. They are defined in two
different header files: ‘<tt>fcntl.h</tt>’ and ‘<tt>sys/file.h</tt>’.
</p>
<dl compact="compact">
<dt> <code>L_SET</code></dt>
<dd><p>An alias for <code>SEEK_SET</code>.
</p>
</dd>
<dt> <code>L_INCR</code></dt>
<dd><p>An alias for <code>SEEK_CUR</code>.
</p>
</dd>
<dt> <code>L_XTND</code></dt>
<dd><p>An alias for <code>SEEK_END</code>.
</p></dd>
</dl>
<hr size="6">
<a name="Descriptors-and-Streams"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#File-Position-Primitive" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Stream_002fDescriptor-Precautions" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Descriptors-and-Streams-1"></a>
<h2 class="section">13.4 Descriptors and Streams</h2>
<a name="index-streams_002c-and-file-descriptors"></a>
<a name="index-converting-file-descriptor-to-stream"></a>
<a name="index-extracting-file-descriptor-from-stream"></a>
<p>Given an open file descriptor, you can create a stream for it with the
<code>fdopen</code> function. You can get the underlying file descriptor for
an existing stream with the <code>fileno</code> function. These functions are
declared in the header file ‘<tt>stdio.h</tt>’.
<a name="index-stdio_002eh-15"></a>
</p>
<dl>
<dt><a name="index-fdopen"></a><u>Function:</u> FILE * <b>fdopen</b><i> (int <var>filedes</var>, const char *<var>opentype</var>)</i></dt>
<dd><p>The <code>fdopen</code> function returns a new stream for the file descriptor
<var>filedes</var>.
</p>
<p>The <var>opentype</var> argument is interpreted in the same way as for the
<code>fopen</code> function (see section <a href="libc_12.html#Opening-Streams">Opening Streams</a>), except that
the ‘<samp>b</samp>’ option is not permitted; this is because GNU makes no
distinction between text and binary files. Also, <code>"w"</code> and
<code>"w+"</code> do not cause truncation of the file; these have an effect only
when opening a file, and in this case the file has already been opened.
You must make sure that the <var>opentype</var> argument matches the actual
mode of the open file descriptor.
</p>
<p>The return value is the new stream. If the stream cannot be created
(for example, if the modes for the file indicated by the file descriptor
do not permit the access specified by the <var>opentype</var> argument), a
null pointer is returned instead.
</p>
<p>In some other systems, <code>fdopen</code> may fail to detect that the modes
for file descriptor do not permit the access specified by
<code>opentype</code>. The GNU C library always checks for this.
</p></dd></dl>
<p>For an example showing the use of the <code>fdopen</code> function,
see <a href="libc_15.html#Creating-a-Pipe">Creating a Pipe</a>.
</p>
<dl>
<dt><a name="index-fileno"></a><u>Function:</u> int <b>fileno</b><i> (FILE *<var>stream</var>)</i></dt>
<dd><p>This function returns the file descriptor associated with the stream
<var>stream</var>. If an error is detected (for example, if the <var>stream</var>
is not valid) or if <var>stream</var> does not do I/O to a file,
<code>fileno</code> returns <em>-1</em>.
</p></dd></dl>
<dl>
<dt><a name="index-fileno_005funlocked"></a><u>Function:</u> int <b>fileno_unlocked</b><i> (FILE *<var>stream</var>)</i></dt>
<dd><p>The <code>fileno_unlocked</code> function is equivalent to the <code>fileno</code>
function except that it does not implicitly lock the stream if the state
is <code>FSETLOCKING_INTERNAL</code>.
</p>
<p>This function is a GNU extension.
</p></dd></dl>
<a name="index-standard-file-descriptors"></a>
<a name="index-file-descriptors_002c-standard"></a>
<p>There are also symbolic constants defined in ‘<tt>unistd.h</tt>’ for the
file descriptors belonging to the standard streams <code>stdin</code>,
<code>stdout</code>, and <code>stderr</code>; see <a href="libc_12.html#Standard-Streams">Standard Streams</a>.
<a name="index-unistd_002eh-2"></a>
</p>
<dl compact="compact">
<dt> <code>STDIN_FILENO</code></dt>
<dd><a name="index-STDIN_005fFILENO"></a>
<p>This macro has value <code>0</code>, which is the file descriptor for
standard input.
<a name="index-standard-input-file-descriptor"></a>
</p>
</dd>
<dt> <code>STDOUT_FILENO</code></dt>
<dd><a name="index-STDOUT_005fFILENO"></a>
<p>This macro has value <code>1</code>, which is the file descriptor for
standard output.
<a name="index-standard-output-file-descriptor"></a>
</p>
</dd>
<dt> <code>STDERR_FILENO</code></dt>
<dd><a name="index-STDERR_005fFILENO"></a>
<p>This macro has value <code>2</code>, which is the file descriptor for
standard error output.
</p></dd>
</dl>
<a name="index-standard-error-file-descriptor"></a>
<hr size="6">
<a name="Stream_002fDescriptor-Precautions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Descriptors-and-Streams" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Linked-Channels" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Dangers-of-Mixing-Streams-and-Descriptors"></a>
<h2 class="section">13.5 Dangers of Mixing Streams and Descriptors</h2>
<a name="index-channels"></a>
<a name="index-streams-and-descriptors"></a>
<a name="index-descriptors-and-streams"></a>
<a name="index-mixing-descriptors-and-streams"></a>
<p>You can have multiple file descriptors and streams (let’s call both
streams and descriptors “channels” for short) connected to the same
file, but you must take care to avoid confusion between channels. There
are two cases to consider: <em>linked</em> channels that share a single
file position value, and <em>independent</em> channels that have their own
file positions.
</p>
<p>It’s best to use just one channel in your program for actual data
transfer to any given file, except when all the access is for input.
For example, if you open a pipe (something you can only do at the file
descriptor level), either do all I/O with the descriptor, or construct a
stream from the descriptor with <code>fdopen</code> and then do all I/O with
the stream.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Linked-Channels">13.5.1 Linked Channels</a></td><td> </td><td align="left" valign="top"> Dealing with channels sharing a file position.
</td></tr>
<tr><td align="left" valign="top"><a href="#Independent-Channels">13.5.2 Independent Channels</a></td><td> </td><td align="left" valign="top"> Dealing with separately opened, unlinked channels.
</td></tr>
<tr><td align="left" valign="top"><a href="#Cleaning-Streams">13.5.3 Cleaning Streams</a></td><td> </td><td align="left" valign="top"> Cleaning a stream makes it safe to use
another channel.
</td></tr>
</table>
<hr size="6">
<a name="Linked-Channels"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Stream_002fDescriptor-Precautions" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Independent-Channels" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Stream_002fDescriptor-Precautions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Linked-Channels-1"></a>
<h3 class="subsection">13.5.1 Linked Channels</h3>
<a name="index-linked-channels"></a>
<p>Channels that come from a single opening share the same file position;
we call them <em>linked</em> channels. Linked channels result when you
make a stream from a descriptor using <code>fdopen</code>, when you get a
descriptor from a stream with <code>fileno</code>, when you copy a descriptor
with <code>dup</code> or <code>dup2</code>, and when descriptors are inherited
during <code>fork</code>. For files that don’t support random access, such as
terminals and pipes, <em>all</em> channels are effectively linked. On
random-access files, all append-type output streams are effectively
linked to each other.
</p>
<a name="index-cleaning-up-a-stream"></a>
<p>If you have been using a stream for I/O (or have just opened the stream),
and you want to do I/O using
another channel (either a stream or a descriptor) that is linked to it,
you must first <em>clean up</em> the stream that you have been using.
See section <a href="#Cleaning-Streams">Cleaning Streams</a>.
</p>
<p>Terminating a process, or executing a new program in the process,
destroys all the streams in the process. If descriptors linked to these
streams persist in other processes, their file positions become
undefined as a result. To prevent this, you must clean up the streams
before destroying them.
</p>
<hr size="6">
<a name="Independent-Channels"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Linked-Channels" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Cleaning-Streams" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Stream_002fDescriptor-Precautions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Independent-Channels-1"></a>
<h3 class="subsection">13.5.2 Independent Channels</h3>
<a name="index-independent-channels"></a>
<p>When you open channels (streams or descriptors) separately on a seekable
file, each channel has its own file position. These are called
<em>independent channels</em>.
</p>
<p>The system handles each channel independently. Most of the time, this
is quite predictable and natural (especially for input): each channel
can read or write sequentially at its own place in the file. However,
if some of the channels are streams, you must take these precautions:
</p>
<ul>
<li>
You should clean an output stream after use, before doing anything else
that might read or write from the same part of the file.
</li><li>
You should clean an input stream before reading data that may have been
modified using an independent channel. Otherwise, you might read
obsolete data that had been in the stream’s buffer.
</li></ul>
<p>If you do output to one channel at the end of the file, this will
certainly leave the other independent channels positioned somewhere
before the new end. You cannot reliably set their file positions to the
new end of file before writing, because the file can always be extended
by another process between when you set the file position and when you
write the data. Instead, use an append-type descriptor or stream; they
always output at the current end of the file. In order to make the
end-of-file position accurate, you must clean the output channel you
were using, if it is a stream.
</p>
<p>It’s impossible for two channels to have separate file pointers for a
file that doesn’t support random access. Thus, channels for reading or
writing such files are always linked, never independent. Append-type
channels are also always linked. For these channels, follow the rules
for linked channels; see <a href="#Linked-Channels">Linked Channels</a>.
</p>
<hr size="6">
<a name="Cleaning-Streams"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Independent-Channels" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Scatter_002dGather" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Stream_002fDescriptor-Precautions" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Cleaning-Streams-1"></a>
<h3 class="subsection">13.5.3 Cleaning Streams</h3>
<p>On the GNU system, you can clean up any stream with <code>fclean</code>:
</p>
<dl>
<dt><a name="index-fclean"></a><u>Function:</u> int <b>fclean</b><i> (FILE *<var>stream</var>)</i></dt>
<dd><p>Clean up the stream <var>stream</var> so that its buffer is empty. If
<var>stream</var> is doing output, force it out. If <var>stream</var> is doing
input, give the data in the buffer back to the system, arranging to
reread it.
</p></dd></dl>
<p>On other systems, you can use <code>fflush</code> to clean a stream in most
cases.
</p>
<p>You can skip the <code>fclean</code> or <code>fflush</code> if you know the stream
is already clean. A stream is clean whenever its buffer is empty. For
example, an unbuffered stream is always clean. An input stream that is
at end-of-file is clean. A line-buffered stream is clean when the last
character output was a newline. However, a just-opened input stream
might not be clean, as its input buffer might not be empty.
</p>
<p>There is one case in which cleaning a stream is impossible on most
systems. This is when the stream is doing input from a file that is not
random-access. Such streams typically read ahead, and when the file is
not random access, there is no way to give back the excess data already
read. When an input stream reads from a random-access file,
<code>fflush</code> does clean the stream, but leaves the file pointer at an
unpredictable place; you must set the file pointer before doing any
further I/O. On the GNU system, using <code>fclean</code> avoids both of
these problems.
</p>
<p>Closing an output-only stream also does <code>fflush</code>, so this is a
valid way of cleaning an output stream. On the GNU system, closing an
input stream does <code>fclean</code>.
</p>
<p>You need not clean a stream before using its descriptor for control
operations such as setting terminal modes; these operations don’t affect
the file position and are not affected by it. You can use any
descriptor for these operations, and all channels are affected
simultaneously. However, text already “output” to a stream but still
buffered by the stream will be subject to the new terminal modes when
subsequently flushed. To make sure “past” output is covered by the
terminal settings that were in effect at the time, flush the output
streams for that terminal before setting the modes. See section <a href="libc_17.html#Terminal-Modes">Terminal Modes</a>.
</p>
<hr size="6">
<a name="Scatter_002dGather"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Cleaning-Streams" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Memory_002dmapped-I_002fO" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Fast-Scatter_002dGather-I_002fO"></a>
<h2 class="section">13.6 Fast Scatter-Gather I/O</h2>
<a name="index-scatter_002dgather"></a>
<p>Some applications may need to read or write data to multiple buffers,
which are separated in memory. Although this can be done easily enough
with multiple calls to <code>read</code> and <code>write</code>, it is inefficient
because there is overhead associated with each kernel call.
</p>
<p>Instead, many platforms provide special high-speed primitives to perform
these <em>scatter-gather</em> operations in a single kernel call. The GNU C
library will provide an emulation on any system that lacks these
primitives, so they are not a portability threat. They are defined in
<code>sys/uio.h</code>.
</p>
<p>These functions are controlled with arrays of <code>iovec</code> structures,
which describe the location and size of each buffer.
</p>
<dl>
<dt><a name="index-struct-iovec"></a><u>Data Type:</u> <b>struct iovec</b></dt>
<dd>
<p>The <code>iovec</code> structure describes a buffer. It contains two fields:
</p>
<dl compact="compact">
<dt> <code>void *iov_base</code></dt>
<dd><p>Contains the address of a buffer.
</p>
</dd>
<dt> <code>size_t iov_len</code></dt>
<dd><p>Contains the length of the buffer.
</p>
</dd>
</dl>
</dd></dl>
<dl>
<dt><a name="index-readv"></a><u>Function:</u> ssize_t <b>readv</b><i> (int <var>filedes</var>, const struct iovec *<var>vector</var>, int <var>count</var>)</i></dt>
<dd>
<p>The <code>readv</code> function reads data from <var>filedes</var> and scatters it
into the buffers described in <var>vector</var>, which is taken to be
<var>count</var> structures long. As each buffer is filled, data is sent to the
next.
</p>
<p>Note that <code>readv</code> is not guaranteed to fill all the buffers.
It may stop at any point, for the same reasons <code>read</code> would.
</p>
<p>The return value is a count of bytes (<em>not</em> buffers) read, <em>0</em>
indicating end-of-file, or <em>-1</em> indicating an error. The possible
errors are the same as in <code>read</code>.
</p>
</dd></dl>
<dl>
<dt><a name="index-writev"></a><u>Function:</u> ssize_t <b>writev</b><i> (int <var>filedes</var>, const struct iovec *<var>vector</var>, int <var>count</var>)</i></dt>
<dd>
<p>The <code>writev</code> function gathers data from the buffers described in
<var>vector</var>, which is taken to be <var>count</var> structures long, and writes
them to <code>filedes</code>. As each buffer is written, it moves on to the
next.
</p>
<p>Like <code>readv</code>, <code>writev</code> may stop midstream under the same
conditions <code>write</code> would.
</p>
<p>The return value is a count of bytes written, or <em>-1</em> indicating an
error. The possible errors are the same as in <code>write</code>.
</p>
</dd></dl>
<p>Note that if the buffers are small (under about 1kB), high-level streams
may be easier to use than these functions. However, <code>readv</code> and
<code>writev</code> are more efficient when the individual buffers themselves
(as opposed to the total output), are large. In that case, a high-level
stream would not be able to cache the data effectively.
</p>
<hr size="6">
<a name="Memory_002dmapped-I_002fO"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Scatter_002dGather" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Waiting-for-I_002fO" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Memory_002dmapped-I_002fO-1"></a>
<h2 class="section">13.7 Memory-mapped I/O</h2>
<p>On modern operating systems, it is possible to <em>mmap</em> (pronounced
“em-map”) a file to a region of memory. When this is done, the file can
be accessed just like an array in the program.
</p>
<p>This is more efficient than <code>read</code> or <code>write</code>, as only the regions
of the file that a program actually accesses are loaded. Accesses to
not-yet-loaded parts of the mmapped region are handled in the same way as
swapped out pages.
</p>
<p>Since mmapped pages can be stored back to their file when physical
memory is low, it is possible to mmap files orders of magnitude larger
than both the physical memory <em>and</em> swap space. The only limit is
address space. The theoretical limit is 4GB on a 32-bit machine -
however, the actual limit will be smaller since some areas will be
reserved for other purposes. If the LFS interface is used the file size
on 32-bit systems is not limited to 2GB (offsets are signed which
reduces the addressable area of 4GB by half); the full 64-bit are
available.
</p>
<p>Memory mapping only works on entire pages of memory. Thus, addresses
for mapping must be page-aligned, and length values will be rounded up.
To determine the size of a page the machine uses one should use
</p>
<a name="index-_005fSC_005fPAGESIZE"></a>
<table><tr><td> </td><td><pre class="smallexample">size_t page_size = (size_t) sysconf (_SC_PAGESIZE);
</pre></td></tr></table>
<p>These functions are declared in ‘<tt>sys/mman.h</tt>’.
</p>
<dl>
<dt><a name="index-mmap"></a><u>Function:</u> void * <b>mmap</b><i> (void *<var>address</var>, size_t <var>length</var>,int <var>protect</var>, int <var>flags</var>, int <var>filedes</var>, off_t <var>offset</var>)</i></dt>
<dd>
<p>The <code>mmap</code> function creates a new mapping, connected to bytes
(<var>offset</var>) to (<var>offset</var> + <var>length</var> - 1) in the file open on
<var>filedes</var>. A new reference for the file specified by <var>filedes</var>
is created, which is not removed by closing the file.
</p>
<p><var>address</var> gives a preferred starting address for the mapping.
<code>NULL</code> expresses no preference. Any previous mapping at that
address is automatically removed. The address you give may still be
changed, unless you use the <code>MAP_FIXED</code> flag.
</p>
<a name="index-PROT_005fREAD"></a>
<a name="index-PROT_005fWRITE"></a>
<a name="index-PROT_005fEXEC"></a>
<p><var>protect</var> contains flags that control what kind of access is
permitted. They include <code>PROT_READ</code>, <code>PROT_WRITE</code>, and
<code>PROT_EXEC</code>, which permit reading, writing, and execution,
respectively. Inappropriate access will cause a segfault (see section <a href="libc_24.html#Program-Error-Signals">Program Error Signals</a>).
</p>
<p>Note that most hardware designs cannot support write permission without
read permission, and many do not distinguish read and execute permission.
Thus, you may receive wider permissions than you ask for, and mappings of
write-only files may be denied even if you do not use <code>PROT_READ</code>.
</p>
<p><var>flags</var> contains flags that control the nature of the map.
One of <code>MAP_SHARED</code> or <code>MAP_PRIVATE</code> must be specified.
</p>
<p>They include:
</p>
<dl compact="compact">
<dt> <code>MAP_PRIVATE</code>
<a name="index-MAP_005fPRIVATE"></a>
</dt>
<dd><p>This specifies that writes to the region should never be written back
to the attached file. Instead, a copy is made for the process, and the
region will be swapped normally if memory runs low. No other process will
see the changes.
</p>
<p>Since private mappings effectively revert to ordinary memory
when written to, you must have enough virtual memory for a copy of
the entire mmapped region if you use this mode with <code>PROT_WRITE</code>.
</p>
</dd>
<dt> <code>MAP_SHARED</code>
<a name="index-MAP_005fSHARED"></a>
</dt>
<dd><p>This specifies that writes to the region will be written back to the
file. Changes made will be shared immediately with other processes
mmaping the same file.
</p>
<p>Note that actual writing may take place at any time. You need to use
<code>msync</code>, described below, if it is important that other processes
using conventional I/O get a consistent view of the file.
</p>
</dd>
<dt> <code>MAP_FIXED</code>
<a name="index-MAP_005fFIXED"></a>
</dt>
<dd><p>This forces the system to use the exact mapping address specified in
<var>address</var> and fail if it can’t.
</p>
</dd>
<dt> <code>MAP_ANONYMOUS</code>
<a name="index-MAP_005fANONYMOUS"></a>
</dt>
<dt> <code>MAP_ANON</code>
<a name="index-MAP_005fANON"></a>
</dt>
<dd><p>This flag tells the system to create an anonymous mapping, not connected
to a file. <var>filedes</var> and <var>off</var> are ignored, and the region is
initialized with zeros.
</p>
<p>Anonymous maps are used as the basic primitive to extend the heap on some
systems. They are also useful to share data between multiple tasks
without creating a file.
</p>
<p>On some systems using private anonymous mmaps is more efficient than using
<code>malloc</code> for large blocks. This is not an issue with the GNU C library,
as the included <code>malloc</code> automatically uses <code>mmap</code> where appropriate.
</p>
</dd>
</dl>
<p><code>mmap</code> returns the address of the new mapping, or <em>-1</em> for an
error.
</p>
<p>Possible errors include:
</p>
<dl compact="compact">
<dt> <code>EINVAL</code></dt>
<dd>
<p>Either <var>address</var> was unusable, or inconsistent <var>flags</var> were
given.
</p>
</dd>
<dt> <code>EACCES</code></dt>
<dd>
<p><var>filedes</var> was not open for the type of access specified in <var>protect</var>.
</p>
</dd>
<dt> <code>ENOMEM</code></dt>
<dd>
<p>Either there is not enough memory for the operation, or the process is
out of address space.
</p>
</dd>
<dt> <code>ENODEV</code></dt>
<dd>
<p>This file is of a type that doesn’t support mapping.
</p>
</dd>
<dt> <code>ENOEXEC</code></dt>
<dd>
<p>The file is on a filesystem that doesn’t support mapping.
</p>
</dd>
</dl>
</dd></dl>
<dl>
<dt><a name="index-mmap64"></a><u>Function:</u> void * <b>mmap64</b><i> (void *<var>address</var>, size_t <var>length</var>,int <var>protect</var>, int <var>flags</var>, int <var>filedes</var>, off64_t <var>offset</var>)</i></dt>
<dd><p>The <code>mmap64</code> function is equivalent to the <code>mmap</code> function but
the <var>offset</var> parameter is of type <code>off64_t</code>. On 32-bit systems
this allows the file associated with the <var>filedes</var> descriptor to be
larger than 2GB. <var>filedes</var> must be a descriptor returned from a
call to <code>open64</code> or <code>fopen64</code> and <code>freopen64</code> where the
descriptor is retrieved with <code>fileno</code>.
</p>
<p>When the sources are translated with <code>_FILE_OFFSET_BITS == 64</code> this
function is actually available under the name <code>mmap</code>. I.e., the
new, extended API using 64 bit file sizes and offsets transparently
replaces the old API.
</p></dd></dl>
<dl>
<dt><a name="index-munmap"></a><u>Function:</u> int <b>munmap</b><i> (void *<var>addr</var>, size_t <var>length</var>)</i></dt>
<dd>
<p><code>munmap</code> removes any memory maps from (<var>addr</var>) to (<var>addr</var> +
<var>length</var>). <var>length</var> should be the length of the mapping.
</p>
<p>It is safe to unmap multiple mappings in one command, or include unmapped
space in the range. It is also possible to unmap only part of an existing
mapping. However, only entire pages can be removed. If <var>length</var> is not
an even number of pages, it will be rounded up.
</p>
<p>It returns <em>0</em> for success and <em>-1</em> for an error.
</p>
<p>One error is possible:
</p>
<dl compact="compact">
<dt> <code>EINVAL</code></dt>
<dd><p>The memory range given was outside the user mmap range or wasn’t page
aligned.
</p>
</dd>
</dl>
</dd></dl>
<dl>
<dt><a name="index-msync"></a><u>Function:</u> int <b>msync</b><i> (void *<var>address</var>, size_t <var>length</var>, int <var>flags</var>)</i></dt>
<dd>
<p>When using shared mappings, the kernel can write the file at any time
before the mapping is removed. To be certain data has actually been
written to the file and will be accessible to non-memory-mapped I/O, it
is necessary to use this function.
</p>
<p>It operates on the region <var>address</var> to (<var>address</var> + <var>length</var>).
It may be used on part of a mapping or multiple mappings, however the
region given should not contain any unmapped space.
</p>
<p><var>flags</var> can contain some options:
</p>
<dl compact="compact">
<dt> <code>MS_SYNC</code>
<a name="index-MS_005fSYNC"></a>
</dt>
<dd>
<p>This flag makes sure the data is actually written <em>to disk</em>.
Normally <code>msync</code> only makes sure that accesses to a file with
conventional I/O reflect the recent changes.
</p>
</dd>
<dt> <code>MS_ASYNC</code>
<a name="index-MS_005fASYNC"></a>
</dt>
<dd>
<p>This tells <code>msync</code> to begin the synchronization, but not to wait for
it to complete.
</p>
</dd>
</dl>
<p><code>msync</code> returns <em>0</em> for success and <em>-1</em> for
error. Errors include:
</p>
<dl compact="compact">
<dt> <code>EINVAL</code></dt>
<dd><p>An invalid region was given, or the <var>flags</var> were invalid.
</p>
</dd>
<dt> <code>EFAULT</code></dt>
<dd><p>There is no existing mapping in at least part of the given region.
</p>
</dd>
</dl>
</dd></dl>
<dl>
<dt><a name="index-mremap"></a><u>Function:</u> void * <b>mremap</b><i> (void *<var>address</var>, size_t <var>length</var>, size_t <var>new_length</var>, int <var>flag</var>)</i></dt>
<dd>
<p>This function can be used to change the size of an existing memory
area. <var>address</var> and <var>length</var> must cover a region entirely mapped
in the same <code>mmap</code> statement. A new mapping with the same
characteristics will be returned with the length <var>new_length</var>.
</p>
<p>One option is possible, <code>MREMAP_MAYMOVE</code>. If it is given in
<var>flags</var>, the system may remove the existing mapping and create a new
one of the desired length in another location.
</p>
<p>The address of the resulting mapping is returned, or <em>-1</em>. Possible
error codes include:
</p>
<dl compact="compact">
<dt> <code>EFAULT</code></dt>
<dd><p>There is no existing mapping in at least part of the original region, or
the region covers two or more distinct mappings.
</p>
</dd>
<dt> <code>EINVAL</code></dt>
<dd><p>The address given is misaligned or inappropriate.
</p>
</dd>
<dt> <code>EAGAIN</code></dt>
<dd><p>The region has pages locked, and if extended it would exceed the
process’s resource limit for locked pages. See section <a href="libc_22.html#Limits-on-Resources">Limiting Resource Usage</a>.
</p>
</dd>
<dt> <code>ENOMEM</code></dt>
<dd><p>The region is private writable, and insufficient virtual memory is
available to extend it. Also, this error will occur if
<code>MREMAP_MAYMOVE</code> is not given and the extension would collide with
another mapped region.
</p>
</dd>
</dl>
</dd></dl>
<p>This function is only available on a few systems. Except for performing
optional optimizations one should not rely on this function.
</p>
<p>Not all file descriptors may be mapped. Sockets, pipes, and most devices
only allow sequential access and do not fit into the mapping abstraction.
In addition, some regular files may not be mmapable, and older kernels may
not support mapping at all. Thus, programs using <code>mmap</code> should
have a fallback method to use should it fail. See <a href="../standards/Mmap.html#Mmap">(standards)Mmap</a> section ‘Mmap’ in <cite>GNU Coding Standards</cite>.
</p>
<dl>
<dt><a name="index-madvise"></a><u>Function:</u> int <b>madvise</b><i> (void *<var>addr</var>, size_t <var>length</var>, int <var>advice</var>)</i></dt>
<dd>
<p>This function can be used to provide the system with <var>advice</var> about
the intended usage patterns of the memory region starting at <var>addr</var>
and extending <var>length</var> bytes.
</p>
<p>The valid BSD values for <var>advice</var> are:
</p>
<dl compact="compact">
<dt> <code>MADV_NORMAL</code></dt>
<dd><p>The region should receive no further special treatment.
</p>
</dd>
<dt> <code>MADV_RANDOM</code></dt>
<dd><p>The region will be accessed via random page references. The kernel
should page-in the minimal number of pages for each page fault.
</p>
</dd>
<dt> <code>MADV_SEQUENTIAL</code></dt>
<dd><p>The region will be accessed via sequential page references. This
may cause the kernel to aggressively read-ahead, expecting further
sequential references after any page fault within this region.
</p>
</dd>
<dt> <code>MADV_WILLNEED</code></dt>
<dd><p>The region will be needed. The pages within this region may
be pre-faulted in by the kernel.
</p>
</dd>
<dt> <code>MADV_DONTNEED</code></dt>
<dd><p>The region is no longer needed. The kernel may free these pages,
causing any changes to the pages to be lost, as well as swapped
out pages to be discarded.
</p>
</dd>
</dl>
<p>The POSIX names are slightly different, but with the same meanings:
</p>
<dl compact="compact">
<dt> <code>POSIX_MADV_NORMAL</code></dt>
<dd><p>This corresponds with BSD’s <code>MADV_NORMAL</code>.
</p>
</dd>
<dt> <code>POSIX_MADV_RANDOM</code></dt>
<dd><p>This corresponds with BSD’s <code>MADV_RANDOM</code>.
</p>
</dd>
<dt> <code>POSIX_MADV_SEQUENTIAL</code></dt>
<dd><p>This corresponds with BSD’s <code>MADV_SEQUENTIAL</code>.
</p>
</dd>
<dt> <code>POSIX_MADV_WILLNEED</code></dt>
<dd><p>This corresponds with BSD’s <code>MADV_WILLNEED</code>.
</p>
</dd>
<dt> <code>POSIX_MADV_DONTNEED</code></dt>
<dd><p>This corresponds with BSD’s <code>MADV_DONTNEED</code>.
</p>
</dd>
</dl>
<p><code>msync</code> returns <em>0</em> for success and <em>-1</em> for
error. Errors include:
</p><dl compact="compact">
<dt> <code>EINVAL</code></dt>
<dd><p>An invalid region was given, or the <var>advice</var> was invalid.
</p>
</dd>
<dt> <code>EFAULT</code></dt>
<dd><p>There is no existing mapping in at least part of the given region.
</p>
</dd>
</dl>
</dd></dl>
<hr size="6">
<a name="Waiting-for-I_002fO"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Memory_002dmapped-I_002fO" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Synchronizing-I_002fO" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Waiting-for-Input-or-Output"></a>
<h2 class="section">13.8 Waiting for Input or Output</h2>
<a name="index-waiting-for-input-or-output"></a>
<a name="index-multiplexing-input"></a>
<a name="index-input-from-multiple-files"></a>
<p>Sometimes a program needs to accept input on multiple input channels
whenever input arrives. For example, some workstations may have devices
such as a digitizing tablet, function button box, or dial box that are
connected via normal asynchronous serial interfaces; good user interface
style requires responding immediately to input on any device. Another
example is a program that acts as a server to several other processes
via pipes or sockets.
</p>
<p>You cannot normally use <code>read</code> for this purpose, because this
blocks the program until input is available on one particular file
descriptor; input on other channels won’t wake it up. You could set
nonblocking mode and poll each file descriptor in turn, but this is very
inefficient.
</p>
<p>A better solution is to use the <code>select</code> function. This blocks the
program until input or output is ready on a specified set of file
descriptors, or until a timer expires, whichever comes first. This
facility is declared in the header file ‘<tt>sys/types.h</tt>’.
<a name="index-sys_002ftypes_002eh"></a>
</p>
<p>In the case of a server socket (see section <a href="libc_16.html#Listening">Listening for Connections</a>), we say that
“input” is available when there are pending connections that could be
accepted (see section <a href="libc_16.html#Accepting-Connections">Accepting Connections</a>). <code>accept</code> for server
sockets blocks and interacts with <code>select</code> just as <code>read</code> does
for normal input.
</p>
<a name="index-file-descriptor-sets_002c-for-select"></a>
<p>The file descriptor sets for the <code>select</code> function are specified
as <code>fd_set</code> objects. Here is the description of the data type
and some macros for manipulating these objects.
</p>
<dl>
<dt><a name="index-fd_005fset"></a><u>Data Type:</u> <b>fd_set</b></dt>
<dd><p>The <code>fd_set</code> data type represents file descriptor sets for the
<code>select</code> function. It is actually a bit array.
</p></dd></dl>
<dl>
<dt><a name="index-FD_005fSETSIZE"></a><u>Macro:</u> int <b>FD_SETSIZE</b></dt>
<dd><p>The value of this macro is the maximum number of file descriptors that a
<code>fd_set</code> object can hold information about. On systems with a
fixed maximum number, <code>FD_SETSIZE</code> is at least that number. On
some systems, including GNU, there is no absolute limit on the number of
descriptors open, but this macro still has a constant value which
controls the number of bits in an <code>fd_set</code>; if you get a file
descriptor with a value as high as <code>FD_SETSIZE</code>, you cannot put
that descriptor into an <code>fd_set</code>.
</p></dd></dl>
<dl>
<dt><a name="index-FD_005fZERO"></a><u>Macro:</u> void <b>FD_ZERO</b><i> (fd_set *<var>set</var>)</i></dt>
<dd><p>This macro initializes the file descriptor set <var>set</var> to be the
empty set.
</p></dd></dl>
<dl>
<dt><a name="index-FD_005fSET"></a><u>Macro:</u> void <b>FD_SET</b><i> (int <var>filedes</var>, fd_set *<var>set</var>)</i></dt>
<dd><p>This macro adds <var>filedes</var> to the file descriptor set <var>set</var>.
</p>
<p>The <var>filedes</var> parameter must not have side effects since it is
evaluated more than once.
</p></dd></dl>
<dl>
<dt><a name="index-FD_005fCLR"></a><u>Macro:</u> void <b>FD_CLR</b><i> (int <var>filedes</var>, fd_set *<var>set</var>)</i></dt>
<dd><p>This macro removes <var>filedes</var> from the file descriptor set <var>set</var>.
</p>
<p>The <var>filedes</var> parameter must not have side effects since it is
evaluated more than once.
</p></dd></dl>
<dl>
<dt><a name="index-FD_005fISSET"></a><u>Macro:</u> int <b>FD_ISSET</b><i> (int <var>filedes</var>, const fd_set *<var>set</var>)</i></dt>
<dd><p>This macro returns a nonzero value (true) if <var>filedes</var> is a member
of the file descriptor set <var>set</var>, and zero (false) otherwise.
</p>
<p>The <var>filedes</var> parameter must not have side effects since it is
evaluated more than once.
</p></dd></dl>
<p>Next, here is the description of the <code>select</code> function itself.
</p>
<dl>
<dt><a name="index-select"></a><u>Function:</u> int <b>select</b><i> (int <var>nfds</var>, fd_set *<var>read-fds</var>, fd_set *<var>write-fds</var>, fd_set *<var>except-fds</var>, struct timeval *<var>timeout</var>)</i></dt>
<dd><p>The <code>select</code> function blocks the calling process until there is
activity on any of the specified sets of file descriptors, or until the
timeout period has expired.
</p>
<p>The file descriptors specified by the <var>read-fds</var> argument are
checked to see if they are ready for reading; the <var>write-fds</var> file
descriptors are checked to see if they are ready for writing; and the
<var>except-fds</var> file descriptors are checked for exceptional
conditions. You can pass a null pointer for any of these arguments if
you are not interested in checking for that kind of condition.
</p>
<p>A file descriptor is considered ready for reading if a <code>read</code>
call will not block. This usually includes the read offset being at
the end of the file or there is an error to report. A server socket
is considered ready for reading if there is a pending connection which
can be accepted with <code>accept</code>; see section <a href="libc_16.html#Accepting-Connections">Accepting Connections</a>. A
client socket is ready for writing when its connection is fully
established; see section <a href="libc_16.html#Connecting">Making a Connection</a>.
</p>
<p>“Exceptional conditions” does not mean errors—errors are reported
immediately when an erroneous system call is executed, and do not
constitute a state of the descriptor. Rather, they include conditions
such as the presence of an urgent message on a socket. (See section <a href="libc_16.html#Sockets">Sockets</a>,
for information on urgent messages.)
</p>
<p>The <code>select</code> function checks only the first <var>nfds</var> file
descriptors. The usual thing is to pass <code>FD_SETSIZE</code> as the value
of this argument.
</p>
<p>The <var>timeout</var> specifies the maximum time to wait. If you pass a
null pointer for this argument, it means to block indefinitely until one
of the file descriptors is ready. Otherwise, you should provide the
time in <code>struct timeval</code> format; see <a href="libc_21.html#High_002dResolution-Calendar">High-Resolution Calendar</a>. Specify zero as the time (a <code>struct timeval</code> containing
all zeros) if you want to find out which descriptors are ready without
waiting if none are ready.
</p>
<p>The normal return value from <code>select</code> is the total number of ready file
descriptors in all of the sets. Each of the argument sets is overwritten
with information about the descriptors that are ready for the corresponding
operation. Thus, to see if a particular descriptor <var>desc</var> has input,
use <code>FD_ISSET (<var>desc</var>, <var>read-fds</var>)</code> after <code>select</code> returns.
</p>
<p>If <code>select</code> returns because the timeout period expires, it returns
a value of zero.
</p>
<p>Any signal will cause <code>select</code> to return immediately. So if your
program uses signals, you can’t rely on <code>select</code> to keep waiting
for the full time specified. If you want to be sure of waiting for a
particular amount of time, you must check for <code>EINTR</code> and repeat
the <code>select</code> with a newly calculated timeout based on the current
time. See the example below. See also <a href="libc_24.html#Interrupted-Primitives">Primitives Interrupted by Signals</a>.
</p>
<p>If an error occurs, <code>select</code> returns <code>-1</code> and does not modify
the argument file descriptor sets. The following <code>errno</code> error
conditions are defined for this function:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>One of the file descriptor sets specified an invalid file descriptor.
</p>
</dd>
<dt> <code>EINTR</code></dt>
<dd><p>The operation was interrupted by a signal. See section <a href="libc_24.html#Interrupted-Primitives">Primitives Interrupted by Signals</a>.
</p>
</dd>
<dt> <code>EINVAL</code></dt>
<dd><p>The <var>timeout</var> argument is invalid; one of the components is negative
or too large.
</p></dd>
</dl>
</dd></dl>
<p><strong>Portability Note:</strong> The <code>select</code> function is a BSD Unix
feature.
</p>
<p>Here is an example showing how you can use <code>select</code> to establish a
timeout period for reading from a file descriptor. The <code>input_timeout</code>
function blocks the calling process until input is available on the
file descriptor, or until the timeout period expires.
</p>
<table><tr><td> </td><td><pre class="smallexample">#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
</pre><pre class="smallexample">
</pre><pre class="smallexample">int
input_timeout (int filedes, unsigned int seconds)
{
fd_set set;
struct timeval timeout;
</pre><pre class="smallexample">
/* <span class="roman">Initialize the file descriptor set.</span> */
FD_ZERO (&set);
FD_SET (filedes, &set);
/* <span class="roman">Initialize the timeout data structure.</span> */
timeout.tv_sec = seconds;
timeout.tv_usec = 0;
</pre><pre class="smallexample"> /* <span class="roman"><code>select</code> returns 0 if timeout, 1 if input available, -1 if error.</span> */
return TEMP_FAILURE_RETRY (select (FD_SETSIZE,
&set, NULL, NULL,
&timeout));
}
</pre><pre class="smallexample">
</pre><pre class="smallexample">int
main (void)
{
fprintf (stderr, "select returned %d.\n",
input_timeout (STDIN_FILENO, 5));
return 0;
}
</pre></td></tr></table>
<p>There is another example showing the use of <code>select</code> to multiplex
input from multiple sockets in <a href="libc_16.html#Server-Example">Byte Stream Connection Server Example</a>.
</p>
<hr size="6">
<a name="Synchronizing-I_002fO"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Waiting-for-I_002fO" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Asynchronous-I_002fO" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Synchronizing-I_002fO-operations"></a>
<h2 class="section">13.9 Synchronizing I/O operations</h2>
<a name="index-synchronizing"></a>
<p>In most modern operating systems, the normal I/O operations are not
executed synchronously. I.e., even if a <code>write</code> system call
returns, this does not mean the data is actually written to the media,
e.g., the disk.
</p>
<p>In situations where synchronization points are necessary, you can use
special functions which ensure that all operations finish before
they return.
</p>
<dl>
<dt><a name="index-sync"></a><u>Function:</u> int <b>sync</b><i> (void)</i></dt>
<dd><p>A call to this function will not return as long as there is data which
has not been written to the device. All dirty buffers in the kernel will
be written and so an overall consistent system can be achieved (if no
other process in parallel writes data).
</p>
<p>A prototype for <code>sync</code> can be found in ‘<tt>unistd.h</tt>’.
</p>
<p>The return value is zero to indicate no error.
</p></dd></dl>
<p>Programs more often want to ensure that data written to a given file is
committed, rather than all data in the system. For this, <code>sync</code> is overkill.
</p>
<dl>
<dt><a name="index-fsync"></a><u>Function:</u> int <b>fsync</b><i> (int <var>fildes</var>)</i></dt>
<dd><p>The <code>fsync</code> function can be used to make sure all data associated with
the open file <var>fildes</var> is written to the device associated with the
descriptor. The function call does not return unless all actions have
finished.
</p>
<p>A prototype for <code>fsync</code> can be found in ‘<tt>unistd.h</tt>’.
</p>
<p>This function is a cancellation point in multi-threaded programs. This
is a problem if the thread allocates some resources (like memory, file
descriptors, semaphores or whatever) at the time <code>fsync</code> is
called. If the thread gets canceled these resources stay allocated
until the program ends. To avoid this, calls to <code>fsync</code> should be
protected using cancellation handlers.
</p>
<p>The return value of the function is zero if no error occurred. Otherwise
it is <em>-1</em> and the global variable <var>errno</var> is set to the
following values:
</p><dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The descriptor <var>fildes</var> is not valid.
</p>
</dd>
<dt> <code>EINVAL</code></dt>
<dd><p>No synchronization is possible since the system does not implement this.
</p></dd>
</dl>
</dd></dl>
<p>Sometimes it is not even necessary to write all data associated with a
file descriptor. E.g., in database files which do not change in size it
is enough to write all the file content data to the device.
Meta-information, like the modification time etc., are not that important
and leaving such information uncommitted does not prevent a successful
recovering of the file in case of a problem.
</p>
<dl>
<dt><a name="index-fdatasync"></a><u>Function:</u> int <b>fdatasync</b><i> (int <var>fildes</var>)</i></dt>
<dd><p>When a call to the <code>fdatasync</code> function returns, it is ensured
that all of the file data is written to the device. For all pending I/O
operations, the parts guaranteeing data integrity finished.
</p>
<p>Not all systems implement the <code>fdatasync</code> operation. On systems
missing this functionality <code>fdatasync</code> is emulated by a call to
<code>fsync</code> since the performed actions are a superset of those
required by <code>fdatasync</code>.
</p>
<p>The prototype for <code>fdatasync</code> is in ‘<tt>unistd.h</tt>’.
</p>
<p>The return value of the function is zero if no error occurred. Otherwise
it is <em>-1</em> and the global variable <var>errno</var> is set to the
following values:
</p><dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The descriptor <var>fildes</var> is not valid.
</p>
</dd>
<dt> <code>EINVAL</code></dt>
<dd><p>No synchronization is possible since the system does not implement this.
</p></dd>
</dl>
</dd></dl>
<hr size="6">
<a name="Asynchronous-I_002fO"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Synchronizing-I_002fO" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Asynchronous-Reads_002fWrites" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Perform-I_002fO-Operations-in-Parallel"></a>
<h2 class="section">13.10 Perform I/O Operations in Parallel</h2>
<p>The POSIX.1b standard defines a new set of I/O operations which can
significantly reduce the time an application spends waiting at I/O. The
new functions allow a program to initiate one or more I/O operations and
then immediately resume normal work while the I/O operations are
executed in parallel. This functionality is available if the
‘<tt>unistd.h</tt>’ file defines the symbol <code>_POSIX_ASYNCHRONOUS_IO</code>.
</p>
<p>These functions are part of the library with realtime functions named
‘<tt>librt</tt>’. They are not actually part of the ‘<tt>libc</tt>’ binary.
The implementation of these functions can be done using support in the
kernel (if available) or using an implementation based on threads at
userlevel. In the latter case it might be necessary to link applications
with the thread library ‘<tt>libpthread</tt>’ in addition to ‘<tt>librt</tt>’.
</p>
<p>All AIO operations operate on files which were opened previously. There
might be arbitrarily many operations running for one file. The
asynchronous I/O operations are controlled using a data structure named
<code>struct aiocb</code> (<em>AIO control block</em>). It is defined in
‘<tt>aio.h</tt>’ as follows.
</p>
<dl>
<dt><a name="index-struct-aiocb"></a><u>Data Type:</u> <b>struct aiocb</b></dt>
<dd><p>The POSIX.1b standard mandates that the <code>struct aiocb</code> structure
contains at least the members described in the following table. There
might be more elements which are used by the implementation, but
depending upon these elements is not portable and is highly deprecated.
</p>
<dl compact="compact">
<dt> <code>int aio_fildes</code></dt>
<dd><p>This element specifies the file descriptor to be used for the
operation. It must be a legal descriptor, otherwise the operation will
fail.
</p>
<p>The device on which the file is opened must allow the seek operation.
I.e., it is not possible to use any of the AIO operations on devices
like terminals where an <code>lseek</code> call would lead to an error.
</p>
</dd>
<dt> <code>off_t aio_offset</code></dt>
<dd><p>This element specifies the offset in the file at which the operation (input
or output) is performed. Since the operations are carried out in arbitrary
order and more than one operation for one file descriptor can be
started, one cannot expect a current read/write position of the file
descriptor.
</p>
</dd>
<dt> <code>volatile void *aio_buf</code></dt>
<dd><p>This is a pointer to the buffer with the data to be written or the place
where the read data is stored.
</p>
</dd>
<dt> <code>size_t aio_nbytes</code></dt>
<dd><p>This element specifies the length of the buffer pointed to by <code>aio_buf</code>.
</p>
</dd>
<dt> <code>int aio_reqprio</code></dt>
<dd><p>If the platform has defined <code>_POSIX_PRIORITIZED_IO</code> and
<code>_POSIX_PRIORITY_SCHEDULING</code>, the AIO requests are
processed based on the current scheduling priority. The
<code>aio_reqprio</code> element can then be used to lower the priority of the
AIO operation.
</p>
</dd>
<dt> <code>struct sigevent aio_sigevent</code></dt>
<dd><p>This element specifies how the calling process is notified once the
operation terminates. If the <code>sigev_notify</code> element is
<code>SIGEV_NONE</code>, no notification is sent. If it is <code>SIGEV_SIGNAL</code>,
the signal determined by <code>sigev_signo</code> is sent. Otherwise,
<code>sigev_notify</code> must be <code>SIGEV_THREAD</code>. In this case, a thread
is created which starts executing the function pointed to by
<code>sigev_notify_function</code>.
</p>
</dd>
<dt> <code>int aio_lio_opcode</code></dt>
<dd><p>This element is only used by the <code>lio_listio</code> and
<code>lio_listio64</code> functions. Since these functions allow an
arbitrary number of operations to start at once, and each operation can be
input or output (or nothing), the information must be stored in the
control block. The possible values are:
</p>
<dl compact="compact">
<dt> <code>LIO_READ</code>
<a name="index-LIO_005fREAD"></a>
</dt>
<dd><p>Start a read operation. Read from the file at position
<code>aio_offset</code> and store the next <code>aio_nbytes</code> bytes in the
buffer pointed to by <code>aio_buf</code>.
</p>
</dd>
<dt> <code>LIO_WRITE</code>
<a name="index-LIO_005fWRITE"></a>
</dt>
<dd><p>Start a write operation. Write <code>aio_nbytes</code> bytes starting at
<code>aio_buf</code> into the file starting at position <code>aio_offset</code>.
</p>
</dd>
<dt> <code>LIO_NOP</code>
<a name="index-LIO_005fNOP"></a>
</dt>
<dd><p>Do nothing for this control block. This value is useful sometimes when
an array of <code>struct aiocb</code> values contains holes, i.e., some of the
values must not be handled although the whole array is presented to the
<code>lio_listio</code> function.
</p></dd>
</dl>
</dd>
</dl>
<p>When the sources are compiled using <code>_FILE_OFFSET_BITS == 64</code> on a
32 bit machine, this type is in fact <code>struct aiocb64</code>, since the LFS
interface transparently replaces the <code>struct aiocb</code> definition.
</p></dd></dl>
<p>For use with the AIO functions defined in the LFS, there is a similar type
defined which replaces the types of the appropriate members with larger
types but otherwise is equivalent to <code>struct aiocb</code>. Particularly,
all member names are the same.
</p>
<dl>
<dt><a name="index-struct-aiocb64"></a><u>Data Type:</u> <b>struct aiocb64</b></dt>
<dd><dl compact="compact">
<dt> <code>int aio_fildes</code></dt>
<dd><p>This element specifies the file descriptor which is used for the
operation. It must be a legal descriptor since otherwise the operation
fails for obvious reasons.
</p>
<p>The device on which the file is opened must allow the seek operation.
I.e., it is not possible to use any of the AIO operations on devices
like terminals where an <code>lseek</code> call would lead to an error.
</p>
</dd>
<dt> <code>off64_t aio_offset</code></dt>
<dd><p>This element specifies at which offset in the file the operation (input
or output) is performed. Since the operation are carried in arbitrary
order and more than one operation for one file descriptor can be
started, one cannot expect a current read/write position of the file
descriptor.
</p>
</dd>
<dt> <code>volatile void *aio_buf</code></dt>
<dd><p>This is a pointer to the buffer with the data to be written or the place
where the read data is stored.
</p>
</dd>
<dt> <code>size_t aio_nbytes</code></dt>
<dd><p>This element specifies the length of the buffer pointed to by <code>aio_buf</code>.
</p>
</dd>
<dt> <code>int aio_reqprio</code></dt>
<dd><p>If for the platform <code>_POSIX_PRIORITIZED_IO</code> and
<code>_POSIX_PRIORITY_SCHEDULING</code> are defined the AIO requests are
processed based on the current scheduling priority. The
<code>aio_reqprio</code> element can then be used to lower the priority of the
AIO operation.
</p>
</dd>
<dt> <code>struct sigevent aio_sigevent</code></dt>
<dd><p>This element specifies how the calling process is notified once the
operation terminates. If the <code>sigev_notify</code>, element is
<code>SIGEV_NONE</code> no notification is sent. If it is <code>SIGEV_SIGNAL</code>,
the signal determined by <code>sigev_signo</code> is sent. Otherwise,
<code>sigev_notify</code> must be <code>SIGEV_THREAD</code> in which case a thread
which starts executing the function pointed to by
<code>sigev_notify_function</code>.
</p>
</dd>
<dt> <code>int aio_lio_opcode</code></dt>
<dd><p>This element is only used by the <code>lio_listio</code> and
<code>[lio_listio64</code> functions. Since these functions allow an
arbitrary number of operations to start at once, and since each operation can be
input or output (or nothing), the information must be stored in the
control block. See the description of <code>struct aiocb</code> for a description
of the possible values.
</p></dd>
</dl>
<p>When the sources are compiled using <code>_FILE_OFFSET_BITS == 64</code> on a
32 bit machine, this type is available under the name <code>struct
aiocb64</code>, since the LFS transparently replaces the old interface.
</p></dd></dl>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Asynchronous-Reads_002fWrites">13.10.1 Asynchronous Read and Write Operations</a></td><td> </td><td align="left" valign="top"></td></tr>
<tr><td align="left" valign="top"><a href="#Status-of-AIO-Operations">13.10.2 Getting the Status of AIO Operations</a></td><td> </td><td align="left" valign="top"></td></tr>
<tr><td align="left" valign="top"><a href="#Synchronizing-AIO-Operations">13.10.3 Getting into a Consistent State</a></td><td> </td><td align="left" valign="top"> Getting into a consistent state.
</td></tr>
<tr><td align="left" valign="top"><a href="#Cancel-AIO-Operations">13.10.4 Cancellation of AIO Operations</a></td><td> </td><td align="left" valign="top"></td></tr>
<tr><td align="left" valign="top"><a href="#Configuration-of-AIO">13.10.5 How to optimize the AIO implementation</a></td><td> </td><td align="left" valign="top"></td></tr>
</table>
<hr size="6">
<a name="Asynchronous-Reads_002fWrites"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Asynchronous-I_002fO" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Status-of-AIO-Operations" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Asynchronous-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Asynchronous-Read-and-Write-Operations"></a>
<h3 class="subsection">13.10.1 Asynchronous Read and Write Operations</h3>
<dl>
<dt><a name="index-aio_005fread"></a><u>Function:</u> int <b>aio_read</b><i> (struct aiocb *<var>aiocbp</var>)</i></dt>
<dd><p>This function initiates an asynchronous read operation. It
immediately returns after the operation was enqueued or when an
error was encountered.
</p>
<p>The first <code>aiocbp->aio_nbytes</code> bytes of the file for which
<code>aiocbp->aio_fildes</code> is a descriptor are written to the buffer
starting at <code>aiocbp->aio_buf</code>. Reading starts at the absolute
position <code>aiocbp->aio_offset</code> in the file.
</p>
<p>If prioritized I/O is supported by the platform the
<code>aiocbp->aio_reqprio</code> value is used to adjust the priority before
the request is actually enqueued.
</p>
<p>The calling process is notified about the termination of the read
request according to the <code>aiocbp->aio_sigevent</code> value.
</p>
<p>When <code>aio_read</code> returns, the return value is zero if no error
occurred that can be found before the process is enqueued. If such an
early error is found, the function returns <em>-1</em> and sets
<code>errno</code> to one of the following values:
</p>
<dl compact="compact">
<dt> <code>EAGAIN</code></dt>
<dd><p>The request was not enqueued due to (temporarily) exceeded resource
limitations.
</p></dd>
<dt> <code>ENOSYS</code></dt>
<dd><p>The <code>aio_read</code> function is not implemented.
</p></dd>
<dt> <code>EBADF</code></dt>
<dd><p>The <code>aiocbp->aio_fildes</code> descriptor is not valid. This condition
need not be recognized before enqueueing the request and so this error
might also be signaled asynchronously.
</p></dd>
<dt> <code>EINVAL</code></dt>
<dd><p>The <code>aiocbp->aio_offset</code> or <code>aiocbp->aio_reqpiro</code> value is
invalid. This condition need not be recognized before enqueueing the
request and so this error might also be signaled asynchronously.
</p></dd>
</dl>
<p>If <code>aio_read</code> returns zero, the current status of the request
can be queried using <code>aio_error</code> and <code>aio_return</code> functions.
As long as the value returned by <code>aio_error</code> is <code>EINPROGRESS</code>
the operation has not yet completed. If <code>aio_error</code> returns zero,
the operation successfully terminated, otherwise the value is to be
interpreted as an error code. If the function terminated, the result of
the operation can be obtained using a call to <code>aio_return</code>. The
returned value is the same as an equivalent call to <code>read</code> would
have returned. Possible error codes returned by <code>aio_error</code> are:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The <code>aiocbp->aio_fildes</code> descriptor is not valid.
</p></dd>
<dt> <code>ECANCELED</code></dt>
<dd><p>The operation was canceled before the operation was finished
(see section <a href="#Cancel-AIO-Operations">Cancellation of AIO Operations</a>)
</p></dd>
<dt> <code>EINVAL</code></dt>
<dd><p>The <code>aiocbp->aio_offset</code> value is invalid.
</p></dd>
</dl>
<p>When the sources are compiled with <code>_FILE_OFFSET_BITS == 64</code> this
function is in fact <code>aio_read64</code> since the LFS interface transparently
replaces the normal implementation.
</p></dd></dl>
<dl>
<dt><a name="index-aio_005fread64"></a><u>Function:</u> int <b>aio_read64</b><i> (struct aiocb *<var>aiocbp</var>)</i></dt>
<dd><p>This function is similar to the <code>aio_read</code> function. The only
difference is that on 32 bit machines, the file descriptor should
be opened in the large file mode. Internally, <code>aio_read64</code> uses
functionality equivalent to <code>lseek64</code> (see section <a href="#File-Position-Primitive">Setting the File Position of a Descriptor</a>) to position the file descriptor correctly for the reading,
as opposed to <code>lseek</code> functionality used in <code>aio_read</code>.
</p>
<p>When the sources are compiled with <code>_FILE_OFFSET_BITS == 64</code>, this
function is available under the name <code>aio_read</code> and so transparently
replaces the interface for small files on 32 bit machines.
</p></dd></dl>
<p>To write data asynchronously to a file, there exists an equivalent pair
of functions with a very similar interface.
</p>
<dl>
<dt><a name="index-aio_005fwrite"></a><u>Function:</u> int <b>aio_write</b><i> (struct aiocb *<var>aiocbp</var>)</i></dt>
<dd><p>This function initiates an asynchronous write operation. The function
call immediately returns after the operation was enqueued or if before
this happens an error was encountered.
</p>
<p>The first <code>aiocbp->aio_nbytes</code> bytes from the buffer starting at
<code>aiocbp->aio_buf</code> are written to the file for which
<code>aiocbp->aio_fildes</code> is an descriptor, starting at the absolute
position <code>aiocbp->aio_offset</code> in the file.
</p>
<p>If prioritized I/O is supported by the platform, the
<code>aiocbp->aio_reqprio</code> value is used to adjust the priority before
the request is actually enqueued.
</p>
<p>The calling process is notified about the termination of the read
request according to the <code>aiocbp->aio_sigevent</code> value.
</p>
<p>When <code>aio_write</code> returns, the return value is zero if no error
occurred that can be found before the process is enqueued. If such an
early error is found the function returns <em>-1</em> and sets
<code>errno</code> to one of the following values.
</p>
<dl compact="compact">
<dt> <code>EAGAIN</code></dt>
<dd><p>The request was not enqueued due to (temporarily) exceeded resource
limitations.
</p></dd>
<dt> <code>ENOSYS</code></dt>
<dd><p>The <code>aio_write</code> function is not implemented.
</p></dd>
<dt> <code>EBADF</code></dt>
<dd><p>The <code>aiocbp->aio_fildes</code> descriptor is not valid. This condition
may not be recognized before enqueueing the request, and so this error
might also be signaled asynchronously.
</p></dd>
<dt> <code>EINVAL</code></dt>
<dd><p>The <code>aiocbp->aio_offset</code> or <code>aiocbp->aio_reqprio</code> value is
invalid. This condition may not be recognized before enqueueing the
request and so this error might also be signaled asynchronously.
</p></dd>
</dl>
<p>In the case <code>aio_write</code> returns zero, the current status of the
request can be queried using <code>aio_error</code> and <code>aio_return</code>
functions. As long as the value returned by <code>aio_error</code> is
<code>EINPROGRESS</code> the operation has not yet completed. If
<code>aio_error</code> returns zero, the operation successfully terminated,
otherwise the value is to be interpreted as an error code. If the
function terminated, the result of the operation can be get using a call
to <code>aio_return</code>. The returned value is the same as an equivalent
call to <code>read</code> would have returned. Possible error codes returned
by <code>aio_error</code> are:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The <code>aiocbp->aio_fildes</code> descriptor is not valid.
</p></dd>
<dt> <code>ECANCELED</code></dt>
<dd><p>The operation was canceled before the operation was finished.
(see section <a href="#Cancel-AIO-Operations">Cancellation of AIO Operations</a>)
</p></dd>
<dt> <code>EINVAL</code></dt>
<dd><p>The <code>aiocbp->aio_offset</code> value is invalid.
</p></dd>
</dl>
<p>When the sources are compiled with <code>_FILE_OFFSET_BITS == 64</code>, this
function is in fact <code>aio_write64</code> since the LFS interface transparently
replaces the normal implementation.
</p></dd></dl>
<dl>
<dt><a name="index-aio_005fwrite64"></a><u>Function:</u> int <b>aio_write64</b><i> (struct aiocb *<var>aiocbp</var>)</i></dt>
<dd><p>This function is similar to the <code>aio_write</code> function. The only
difference is that on 32 bit machines the file descriptor should
be opened in the large file mode. Internally <code>aio_write64</code> uses
functionality equivalent to <code>lseek64</code> (see section <a href="#File-Position-Primitive">Setting the File Position of a Descriptor</a>) to position the file descriptor correctly for the writing,
as opposed to <code>lseek</code> functionality used in <code>aio_write</code>.
</p>
<p>When the sources are compiled with <code>_FILE_OFFSET_BITS == 64</code>, this
function is available under the name <code>aio_write</code> and so transparently
replaces the interface for small files on 32 bit machines.
</p></dd></dl>
<p>Besides these functions with the more or less traditional interface,
POSIX.1b also defines a function which can initiate more than one
operation at a time, and which can handle freely mixed read and write
operations. It is therefore similar to a combination of <code>readv</code> and
<code>writev</code>.
</p>
<dl>
<dt><a name="index-lio_005flistio"></a><u>Function:</u> int <b>lio_listio</b><i> (int <var>mode</var>, struct aiocb *const <var>list</var>[], int <var>nent</var>, struct sigevent *<var>sig</var>)</i></dt>
<dd><p>The <code>lio_listio</code> function can be used to enqueue an arbitrary
number of read and write requests at one time. The requests can all be
meant for the same file, all for different files or every solution in
between.
</p>
<p><code>lio_listio</code> gets the <var>nent</var> requests from the array pointed to
by <var>list</var>. The operation to be performed is determined by the
<code>aio_lio_opcode</code> member in each element of <var>list</var>. If this
field is <code>LIO_READ</code> a read operation is enqueued, similar to a call
of <code>aio_read</code> for this element of the array (except that the way
the termination is signalled is different, as we will see below). If
the <code>aio_lio_opcode</code> member is <code>LIO_WRITE</code> a write operation
is enqueued. Otherwise the <code>aio_lio_opcode</code> must be <code>LIO_NOP</code>
in which case this element of <var>list</var> is simply ignored. This
“operation” is useful in situations where one has a fixed array of
<code>struct aiocb</code> elements from which only a few need to be handled at
a time. Another situation is where the <code>lio_listio</code> call was
canceled before all requests are processed (see section <a href="#Cancel-AIO-Operations">Cancellation of AIO Operations</a>) and the remaining requests have to be reissued.
</p>
<p>The other members of each element of the array pointed to by
<code>list</code> must have values suitable for the operation as described in
the documentation for <code>aio_read</code> and <code>aio_write</code> above.
</p>
<p>The <var>mode</var> argument determines how <code>lio_listio</code> behaves after
having enqueued all the requests. If <var>mode</var> is <code>LIO_WAIT</code> it
waits until all requests terminated. Otherwise <var>mode</var> must be
<code>LIO_NOWAIT</code> and in this case the function returns immediately after
having enqueued all the requests. In this case the caller gets a
notification of the termination of all requests according to the
<var>sig</var> parameter. If <var>sig</var> is <code>NULL</code> no notification is
send. Otherwise a signal is sent or a thread is started, just as
described in the description for <code>aio_read</code> or <code>aio_write</code>.
</p>
<p>If <var>mode</var> is <code>LIO_WAIT</code>, the return value of <code>lio_listio</code>
is <em>0</em> when all requests completed successfully. Otherwise the
function return <em>-1</em> and <code>errno</code> is set accordingly. To find
out which request or requests failed one has to use the <code>aio_error</code>
function on all the elements of the array <var>list</var>.
</p>
<p>In case <var>mode</var> is <code>LIO_NOWAIT</code>, the function returns <em>0</em> if
all requests were enqueued correctly. The current state of the requests
can be found using <code>aio_error</code> and <code>aio_return</code> as described
above. If <code>lio_listio</code> returns <em>-1</em> in this mode, the
global variable <code>errno</code> is set accordingly. If a request did not
yet terminate, a call to <code>aio_error</code> returns <code>EINPROGRESS</code>. If
the value is different, the request is finished and the error value (or
<em>0</em>) is returned and the result of the operation can be retrieved
using <code>aio_return</code>.
</p>
<p>Possible values for <code>errno</code> are:
</p>
<dl compact="compact">
<dt> <code>EAGAIN</code></dt>
<dd><p>The resources necessary to queue all the requests are not available at
the moment. The error status for each element of <var>list</var> must be
checked to determine which request failed.
</p>
<p>Another reason could be that the system wide limit of AIO requests is
exceeded. This cannot be the case for the implementation on GNU systems
since no arbitrary limits exist.
</p></dd>
<dt> <code>EINVAL</code></dt>
<dd><p>The <var>mode</var> parameter is invalid or <var>nent</var> is larger than
<code>AIO_LISTIO_MAX</code>.
</p></dd>
<dt> <code>EIO</code></dt>
<dd><p>One or more of the request’s I/O operations failed. The error status of
each request should be checked to determine which one failed.
</p></dd>
<dt> <code>ENOSYS</code></dt>
<dd><p>The <code>lio_listio</code> function is not supported.
</p></dd>
</dl>
<p>If the <var>mode</var> parameter is <code>LIO_NOWAIT</code> and the caller cancels
a request, the error status for this request returned by
<code>aio_error</code> is <code>ECANCELED</code>.
</p>
<p>When the sources are compiled with <code>_FILE_OFFSET_BITS == 64</code>, this
function is in fact <code>lio_listio64</code> since the LFS interface
transparently replaces the normal implementation.
</p></dd></dl>
<dl>
<dt><a name="index-lio_005flistio64"></a><u>Function:</u> int <b>lio_listio64</b><i> (int <var>mode</var>, struct aiocb *const <var>list</var>, int <var>nent</var>, struct sigevent *<var>sig</var>)</i></dt>
<dd><p>This function is similar to the <code>lio_listio</code> function. The only
difference is that on 32 bit machines, the file descriptor should
be opened in the large file mode. Internally, <code>lio_listio64</code> uses
functionality equivalent to <code>lseek64</code> (see section <a href="#File-Position-Primitive">Setting the File Position of a Descriptor</a>) to position the file descriptor correctly for the reading or
writing, as opposed to <code>lseek</code> functionality used in
<code>lio_listio</code>.
</p>
<p>When the sources are compiled with <code>_FILE_OFFSET_BITS == 64</code>, this
function is available under the name <code>lio_listio</code> and so
transparently replaces the interface for small files on 32 bit
machines.
</p></dd></dl>
<hr size="6">
<a name="Status-of-AIO-Operations"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Asynchronous-Reads_002fWrites" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Synchronizing-AIO-Operations" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Asynchronous-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Getting-the-Status-of-AIO-Operations"></a>
<h3 class="subsection">13.10.2 Getting the Status of AIO Operations</h3>
<p>As already described in the documentation of the functions in the last
section, it must be possible to get information about the status of an I/O
request. When the operation is performed truly asynchronously (as with
<code>aio_read</code> and <code>aio_write</code> and with <code>lio_listio</code> when the
mode is <code>LIO_NOWAIT</code>), one sometimes needs to know whether a
specific request already terminated and if so, what the result was.
The following two functions allow you to get this kind of information.
</p>
<dl>
<dt><a name="index-aio_005ferror"></a><u>Function:</u> int <b>aio_error</b><i> (const struct aiocb *<var>aiocbp</var>)</i></dt>
<dd><p>This function determines the error state of the request described by the
<code>struct aiocb</code> variable pointed to by <var>aiocbp</var>. If the
request has not yet terminated the value returned is always
<code>EINPROGRESS</code>. Once the request has terminated the value
<code>aio_error</code> returns is either <em>0</em> if the request completed
successfully or it returns the value which would be stored in the
<code>errno</code> variable if the request would have been done using
<code>read</code>, <code>write</code>, or <code>fsync</code>.
</p>
<p>The function can return <code>ENOSYS</code> if it is not implemented. It
could also return <code>EINVAL</code> if the <var>aiocbp</var> parameter does not
refer to an asynchronous operation whose return status is not yet known.
</p>
<p>When the sources are compiled with <code>_FILE_OFFSET_BITS == 64</code> this
function is in fact <code>aio_error64</code> since the LFS interface
transparently replaces the normal implementation.
</p></dd></dl>
<dl>
<dt><a name="index-aio_005ferror64"></a><u>Function:</u> int <b>aio_error64</b><i> (const struct aiocb64 *<var>aiocbp</var>)</i></dt>
<dd><p>This function is similar to <code>aio_error</code> with the only difference
that the argument is a reference to a variable of type <code>struct
aiocb64</code>.
</p>
<p>When the sources are compiled with <code>_FILE_OFFSET_BITS == 64</code> this
function is available under the name <code>aio_error</code> and so
transparently replaces the interface for small files on 32 bit
machines.
</p></dd></dl>
<dl>
<dt><a name="index-aio_005freturn"></a><u>Function:</u> ssize_t <b>aio_return</b><i> (const struct aiocb *<var>aiocbp</var>)</i></dt>
<dd><p>This function can be used to retrieve the return status of the operation
carried out by the request described in the variable pointed to by
<var>aiocbp</var>. As long as the error status of this request as returned
by <code>aio_error</code> is <code>EINPROGRESS</code> the return of this function is
undefined.
</p>
<p>Once the request is finished this function can be used exactly once to
retrieve the return value. Following calls might lead to undefined
behavior. The return value itself is the value which would have been
returned by the <code>read</code>, <code>write</code>, or <code>fsync</code> call.
</p>
<p>The function can return <code>ENOSYS</code> if it is not implemented. It
could also return <code>EINVAL</code> if the <var>aiocbp</var> parameter does not
refer to an asynchronous operation whose return status is not yet known.
</p>
<p>When the sources are compiled with <code>_FILE_OFFSET_BITS == 64</code> this
function is in fact <code>aio_return64</code> since the LFS interface
transparently replaces the normal implementation.
</p></dd></dl>
<dl>
<dt><a name="index-aio_005freturn64"></a><u>Function:</u> int <b>aio_return64</b><i> (const struct aiocb64 *<var>aiocbp</var>)</i></dt>
<dd><p>This function is similar to <code>aio_return</code> with the only difference
that the argument is a reference to a variable of type <code>struct
aiocb64</code>.
</p>
<p>When the sources are compiled with <code>_FILE_OFFSET_BITS == 64</code> this
function is available under the name <code>aio_return</code> and so
transparently replaces the interface for small files on 32 bit
machines.
</p></dd></dl>
<hr size="6">
<a name="Synchronizing-AIO-Operations"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Status-of-AIO-Operations" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Cancel-AIO-Operations" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Asynchronous-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Getting-into-a-Consistent-State"></a>
<h3 class="subsection">13.10.3 Getting into a Consistent State</h3>
<p>When dealing with asynchronous operations it is sometimes necessary to
get into a consistent state. This would mean for AIO that one wants to
know whether a certain request or a group of request were processed.
This could be done by waiting for the notification sent by the system
after the operation terminated, but this sometimes would mean wasting
resources (mainly computation time). Instead POSIX.1b defines two
functions which will help with most kinds of consistency.
</p>
<p>The <code>aio_fsync</code> and <code>aio_fsync64</code> functions are only available
if the symbol <code>_POSIX_SYNCHRONIZED_IO</code> is defined in ‘<tt>unistd.h</tt>’.
</p>
<a name="index-synchronizing-1"></a>
<dl>
<dt><a name="index-aio_005ffsync"></a><u>Function:</u> int <b>aio_fsync</b><i> (int <var>op</var>, struct aiocb *<var>aiocbp</var>)</i></dt>
<dd><p>Calling this function forces all I/O operations operating queued at the
time of the function call operating on the file descriptor
<code>aiocbp->aio_fildes</code> into the synchronized I/O completion state
(see section <a href="#Synchronizing-I_002fO">Synchronizing I/O operations</a>). The <code>aio_fsync</code> function returns
immediately but the notification through the method described in
<code>aiocbp->aio_sigevent</code> will happen only after all requests for this
file descriptor have terminated and the file is synchronized. This also
means that requests for this very same file descriptor which are queued
after the synchronization request are not affected.
</p>
<p>If <var>op</var> is <code>O_DSYNC</code> the synchronization happens as with a call
to <code>fdatasync</code>. Otherwise <var>op</var> should be <code>O_SYNC</code> and
the synchronization happens as with <code>fsync</code>.
</p>
<p>As long as the synchronization has not happened, a call to
<code>aio_error</code> with the reference to the object pointed to by
<var>aiocbp</var> returns <code>EINPROGRESS</code>. Once the synchronization is
done <code>aio_error</code> return <em>0</em> if the synchronization was not
successful. Otherwise the value returned is the value to which the
<code>fsync</code> or <code>fdatasync</code> function would have set the
<code>errno</code> variable. In this case nothing can be assumed about the
consistency for the data written to this file descriptor.
</p>
<p>The return value of this function is <em>0</em> if the request was
successfully enqueued. Otherwise the return value is <em>-1</em> and
<code>errno</code> is set to one of the following values:
</p>
<dl compact="compact">
<dt> <code>EAGAIN</code></dt>
<dd><p>The request could not be enqueued due to temporary lack of resources.
</p></dd>
<dt> <code>EBADF</code></dt>
<dd><p>The file descriptor <code>aiocbp->aio_fildes</code> is not valid or not open
for writing.
</p></dd>
<dt> <code>EINVAL</code></dt>
<dd><p>The implementation does not support I/O synchronization or the <var>op</var>
parameter is other than <code>O_DSYNC</code> and <code>O_SYNC</code>.
</p></dd>
<dt> <code>ENOSYS</code></dt>
<dd><p>This function is not implemented.
</p></dd>
</dl>
<p>When the sources are compiled with <code>_FILE_OFFSET_BITS == 64</code> this
function is in fact <code>aio_fsync64</code> since the LFS interface
transparently replaces the normal implementation.
</p></dd></dl>
<dl>
<dt><a name="index-aio_005ffsync64"></a><u>Function:</u> int <b>aio_fsync64</b><i> (int <var>op</var>, struct aiocb64 *<var>aiocbp</var>)</i></dt>
<dd><p>This function is similar to <code>aio_fsync</code> with the only difference
that the argument is a reference to a variable of type <code>struct
aiocb64</code>.
</p>
<p>When the sources are compiled with <code>_FILE_OFFSET_BITS == 64</code> this
function is available under the name <code>aio_fsync</code> and so
transparently replaces the interface for small files on 32 bit
machines.
</p></dd></dl>
<p>Another method of synchronization is to wait until one or more requests of a
specific set terminated. This could be achieved by the <code>aio_*</code>
functions to notify the initiating process about the termination but in
some situations this is not the ideal solution. In a program which
constantly updates clients somehow connected to the server it is not
always the best solution to go round robin since some connections might
be slow. On the other hand letting the <code>aio_*</code> function notify the
caller might also be not the best solution since whenever the process
works on preparing data for on client it makes no sense to be
interrupted by a notification since the new client will not be handled
before the current client is served. For situations like this
<code>aio_suspend</code> should be used.
</p>
<dl>
<dt><a name="index-aio_005fsuspend"></a><u>Function:</u> int <b>aio_suspend</b><i> (const struct aiocb *const <var>list</var>[], int <var>nent</var>, const struct timespec *<var>timeout</var>)</i></dt>
<dd><p>When calling this function, the calling thread is suspended until at
least one of the requests pointed to by the <var>nent</var> elements of the
array <var>list</var> has completed. If any of the requests has already
completed at the time <code>aio_suspend</code> is called, the function returns
immediately. Whether a request has terminated or not is determined by
comparing the error status of the request with <code>EINPROGRESS</code>. If
an element of <var>list</var> is <code>NULL</code>, the entry is simply ignored.
</p>
<p>If no request has finished, the calling process is suspended. If
<var>timeout</var> is <code>NULL</code>, the process is not woken until a request
has finished. If <var>timeout</var> is not <code>NULL</code>, the process remains
suspended at least as long as specified in <var>timeout</var>. In this case,
<code>aio_suspend</code> returns with an error.
</p>
<p>The return value of the function is <em>0</em> if one or more requests
from the <var>list</var> have terminated. Otherwise the function returns
<em>-1</em> and <code>errno</code> is set to one of the following values:
</p>
<dl compact="compact">
<dt> <code>EAGAIN</code></dt>
<dd><p>None of the requests from the <var>list</var> completed in the time specified
by <var>timeout</var>.
</p></dd>
<dt> <code>EINTR</code></dt>
<dd><p>A signal interrupted the <code>aio_suspend</code> function. This signal might
also be sent by the AIO implementation while signalling the termination
of one of the requests.
</p></dd>
<dt> <code>ENOSYS</code></dt>
<dd><p>The <code>aio_suspend</code> function is not implemented.
</p></dd>
</dl>
<p>When the sources are compiled with <code>_FILE_OFFSET_BITS == 64</code> this
function is in fact <code>aio_suspend64</code> since the LFS interface
transparently replaces the normal implementation.
</p></dd></dl>
<dl>
<dt><a name="index-aio_005fsuspend64"></a><u>Function:</u> int <b>aio_suspend64</b><i> (const struct aiocb64 *const <var>list</var>[], int <var>nent</var>, const struct timespec *<var>timeout</var>)</i></dt>
<dd><p>This function is similar to <code>aio_suspend</code> with the only difference
that the argument is a reference to a variable of type <code>struct
aiocb64</code>.
</p>
<p>When the sources are compiled with <code>_FILE_OFFSET_BITS == 64</code> this
function is available under the name <code>aio_suspend</code> and so
transparently replaces the interface for small files on 32 bit
machines.
</p></dd></dl>
<hr size="6">
<a name="Cancel-AIO-Operations"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Synchronizing-AIO-Operations" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Configuration-of-AIO" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Asynchronous-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Cancellation-of-AIO-Operations"></a>
<h3 class="subsection">13.10.4 Cancellation of AIO Operations</h3>
<p>When one or more requests are asynchronously processed, it might be
useful in some situations to cancel a selected operation, e.g., if it
becomes obvious that the written data is no longer accurate and would
have to be overwritten soon. As an example, assume an application, which
writes data in files in a situation where new incoming data would have
to be written in a file which will be updated by an enqueued request.
The POSIX AIO implementation provides such a function, but this function
is not capable of forcing the cancellation of the request. It is up to the
implementation to decide whether it is possible to cancel the operation
or not. Therefore using this function is merely a hint.
</p>
<dl>
<dt><a name="index-aio_005fcancel"></a><u>Function:</u> int <b>aio_cancel</b><i> (int <var>fildes</var>, struct aiocb *<var>aiocbp</var>)</i></dt>
<dd><p>The <code>aio_cancel</code> function can be used to cancel one or more
outstanding requests. If the <var>aiocbp</var> parameter is <code>NULL</code>, the
function tries to cancel all of the outstanding requests which would process
the file descriptor <var>fildes</var> (i.e., whose <code>aio_fildes</code> member
is <var>fildes</var>). If <var>aiocbp</var> is not <code>NULL</code>, <code>aio_cancel</code>
attempts to cancel the specific request pointed to by <var>aiocbp</var>.
</p>
<p>For requests which were successfully canceled, the normal notification
about the termination of the request should take place. I.e., depending
on the <code>struct sigevent</code> object which controls this, nothing
happens, a signal is sent or a thread is started. If the request cannot
be canceled, it terminates the usual way after performing the operation.
</p>
<p>After a request is successfully canceled, a call to <code>aio_error</code> with
a reference to this request as the parameter will return
<code>ECANCELED</code> and a call to <code>aio_return</code> will return <em>-1</em>.
If the request wasn’t canceled and is still running the error status is
still <code>EINPROGRESS</code>.
</p>
<p>The return value of the function is <code>AIO_CANCELED</code> if there were
requests which haven’t terminated and which were successfully canceled.
If there is one or more requests left which couldn’t be canceled, the
return value is <code>AIO_NOTCANCELED</code>. In this case <code>aio_error</code>
must be used to find out which of the, perhaps multiple, requests (in
<var>aiocbp</var> is <code>NULL</code>) weren’t successfully canceled. If all
requests already terminated at the time <code>aio_cancel</code> is called the
return value is <code>AIO_ALLDONE</code>.
</p>
<p>If an error occurred during the execution of <code>aio_cancel</code> the
function returns <em>-1</em> and sets <code>errno</code> to one of the following
values.
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The file descriptor <var>fildes</var> is not valid.
</p></dd>
<dt> <code>ENOSYS</code></dt>
<dd><p><code>aio_cancel</code> is not implemented.
</p></dd>
</dl>
<p>When the sources are compiled with <code>_FILE_OFFSET_BITS == 64</code>, this
function is in fact <code>aio_cancel64</code> since the LFS interface
transparently replaces the normal implementation.
</p></dd></dl>
<dl>
<dt><a name="index-aio_005fcancel64"></a><u>Function:</u> int <b>aio_cancel64</b><i> (int <var>fildes</var>, struct aiocb64 *<var>aiocbp</var>)</i></dt>
<dd><p>This function is similar to <code>aio_cancel</code> with the only difference
that the argument is a reference to a variable of type <code>struct
aiocb64</code>.
</p>
<p>When the sources are compiled with <code>_FILE_OFFSET_BITS == 64</code>, this
function is available under the name <code>aio_cancel</code> and so
transparently replaces the interface for small files on 32 bit
machines.
</p></dd></dl>
<hr size="6">
<a name="Configuration-of-AIO"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Cancel-AIO-Operations" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Control-Operations" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Asynchronous-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="How-to-optimize-the-AIO-implementation"></a>
<h3 class="subsection">13.10.5 How to optimize the AIO implementation</h3>
<p>The POSIX standard does not specify how the AIO functions are
implemented. They could be system calls, but it is also possible to
emulate them at userlevel.
</p>
<p>At the point of this writing, the available implementation is a userlevel
implementation which uses threads for handling the enqueued requests.
While this implementation requires making some decisions about
limitations, hard limitations are something which is best avoided
in the GNU C library. Therefore, the GNU C library provides a means
for tuning the AIO implementation according to the individual use.
</p>
<dl>
<dt><a name="index-struct-aioinit"></a><u>Data Type:</u> <b>struct aioinit</b></dt>
<dd><p>This data type is used to pass the configuration or tunable parameters
to the implementation. The program has to initialize the members of
this struct and pass it to the implementation using the <code>aio_init</code>
function.
</p>
<dl compact="compact">
<dt> <code>int aio_threads</code></dt>
<dd><p>This member specifies the maximal number of threads which may be used
at any one time.
</p></dd>
<dt> <code>int aio_num</code></dt>
<dd><p>This number provides an estimate on the maximal number of simultaneously
enqueued requests.
</p></dd>
<dt> <code>int aio_locks</code></dt>
<dd><p>Unused.
</p></dd>
<dt> <code>int aio_usedba</code></dt>
<dd><p>Unused.
</p></dd>
<dt> <code>int aio_debug</code></dt>
<dd><p>Unused.
</p></dd>
<dt> <code>int aio_numusers</code></dt>
<dd><p>Unused.
</p></dd>
<dt> <code>int aio_reserved[2]</code></dt>
<dd><p>Unused.
</p></dd>
</dl>
</dd></dl>
<dl>
<dt><a name="index-aio_005finit"></a><u>Function:</u> void <b>aio_init</b><i> (const struct aioinit *<var>init</var>)</i></dt>
<dd><p>This function must be called before any other AIO function. Calling it
is completely voluntary, as it is only meant to help the AIO
implementation perform better.
</p>
<p>Before calling the <code>aio_init</code>, function the members of a variable of
type <code>struct aioinit</code> must be initialized. Then a reference to
this variable is passed as the parameter to <code>aio_init</code> which itself
may or may not pay attention to the hints.
</p>
<p>The function has no return value and no error cases are defined. It is
a extension which follows a proposal from the SGI implementation in
Irix 6. It is not covered by POSIX.1b or Unix98.
</p></dd></dl>
<hr size="6">
<a name="Control-Operations"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Configuration-of-AIO" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Duplicating-Descriptors" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Control-Operations-on-Files"></a>
<h2 class="section">13.11 Control Operations on Files</h2>
<a name="index-control-operations-on-files"></a>
<a name="index-fcntl-function"></a>
<p>This section describes how you can perform various other operations on
file descriptors, such as inquiring about or setting flags describing
the status of the file descriptor, manipulating record locks, and the
like. All of these operations are performed by the function <code>fcntl</code>.
</p>
<p>The second argument to the <code>fcntl</code> function is a command that
specifies which operation to perform. The function and macros that name
various flags that are used with it are declared in the header file
‘<tt>fcntl.h</tt>’. Many of these flags are also used by the <code>open</code>
function; see <a href="#Opening-and-Closing-Files">Opening and Closing Files</a>.
<a name="index-fcntl_002eh-2"></a>
</p>
<dl>
<dt><a name="index-fcntl"></a><u>Function:</u> int <b>fcntl</b><i> (int <var>filedes</var>, int <var>command</var>, …)</i></dt>
<dd><p>The <code>fcntl</code> function performs the operation specified by
<var>command</var> on the file descriptor <var>filedes</var>. Some commands
require additional arguments to be supplied. These additional arguments
and the return value and error conditions are given in the detailed
descriptions of the individual commands.
</p>
<p>Briefly, here is a list of what the various commands are.
</p>
<dl compact="compact">
<dt> <code>F_DUPFD</code></dt>
<dd><p>Duplicate the file descriptor (return another file descriptor pointing
to the same open file). See section <a href="#Duplicating-Descriptors">Duplicating Descriptors</a>.
</p>
</dd>
<dt> <code>F_GETFD</code></dt>
<dd><p>Get flags associated with the file descriptor. See section <a href="#Descriptor-Flags">File Descriptor Flags</a>.
</p>
</dd>
<dt> <code>F_SETFD</code></dt>
<dd><p>Set flags associated with the file descriptor. See section <a href="#Descriptor-Flags">File Descriptor Flags</a>.
</p>
</dd>
<dt> <code>F_GETFL</code></dt>
<dd><p>Get flags associated with the open file. See section <a href="#File-Status-Flags">File Status Flags</a>.
</p>
</dd>
<dt> <code>F_SETFL</code></dt>
<dd><p>Set flags associated with the open file. See section <a href="#File-Status-Flags">File Status Flags</a>.
</p>
</dd>
<dt> <code>F_GETLK</code></dt>
<dd><p>Get a file lock. See section <a href="#File-Locks">File Locks</a>.
</p>
</dd>
<dt> <code>F_SETLK</code></dt>
<dd><p>Set or clear a file lock. See section <a href="#File-Locks">File Locks</a>.
</p>
</dd>
<dt> <code>F_SETLKW</code></dt>
<dd><p>Like <code>F_SETLK</code>, but wait for completion. See section <a href="#File-Locks">File Locks</a>.
</p>
</dd>
<dt> <code>F_GETOWN</code></dt>
<dd><p>Get process or process group ID to receive <code>SIGIO</code> signals.
See section <a href="#Interrupt-Input">Interrupt-Driven Input</a>.
</p>
</dd>
<dt> <code>F_SETOWN</code></dt>
<dd><p>Set process or process group ID to receive <code>SIGIO</code> signals.
See section <a href="#Interrupt-Input">Interrupt-Driven Input</a>.
</p></dd>
</dl>
<p>This function is a cancellation point in multi-threaded programs. This
is a problem if the thread allocates some resources (like memory, file
descriptors, semaphores or whatever) at the time <code>fcntl</code> is
called. If the thread gets canceled these resources stay allocated
until the program ends. To avoid this calls to <code>fcntl</code> should be
protected using cancellation handlers.
</p></dd></dl>
<hr size="6">
<a name="Duplicating-Descriptors"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Control-Operations" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Descriptor-Flags" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Duplicating-Descriptors-1"></a>
<h2 class="section">13.12 Duplicating Descriptors</h2>
<a name="index-duplicating-file-descriptors"></a>
<a name="index-redirecting-input-and-output"></a>
<p>You can <em>duplicate</em> a file descriptor, or allocate another file
descriptor that refers to the same open file as the original. Duplicate
descriptors share one file position and one set of file status flags
(see section <a href="#File-Status-Flags">File Status Flags</a>), but each has its own set of file descriptor
flags (see section <a href="#Descriptor-Flags">File Descriptor Flags</a>).
</p>
<p>The major use of duplicating a file descriptor is to implement
<em>redirection</em> of input or output: that is, to change the
file or pipe that a particular file descriptor corresponds to.
</p>
<p>You can perform this operation using the <code>fcntl</code> function with the
<code>F_DUPFD</code> command, but there are also convenient functions
<code>dup</code> and <code>dup2</code> for duplicating descriptors.
</p>
<a name="index-unistd_002eh-3"></a>
<a name="index-fcntl_002eh-3"></a>
<p>The <code>fcntl</code> function and flags are declared in ‘<tt>fcntl.h</tt>’,
while prototypes for <code>dup</code> and <code>dup2</code> are in the header file
‘<tt>unistd.h</tt>’.
</p>
<dl>
<dt><a name="index-dup"></a><u>Function:</u> int <b>dup</b><i> (int <var>old</var>)</i></dt>
<dd><p>This function copies descriptor <var>old</var> to the first available
descriptor number (the first number not currently open). It is
equivalent to <code>fcntl (<var>old</var>, F_DUPFD, 0)</code>.
</p></dd></dl>
<dl>
<dt><a name="index-dup2"></a><u>Function:</u> int <b>dup2</b><i> (int <var>old</var>, int <var>new</var>)</i></dt>
<dd><p>This function copies the descriptor <var>old</var> to descriptor number
<var>new</var>.
</p>
<p>If <var>old</var> is an invalid descriptor, then <code>dup2</code> does nothing; it
does not close <var>new</var>. Otherwise, the new duplicate of <var>old</var>
replaces any previous meaning of descriptor <var>new</var>, as if <var>new</var>
were closed first.
</p>
<p>If <var>old</var> and <var>new</var> are different numbers, and <var>old</var> is a
valid descriptor number, then <code>dup2</code> is equivalent to:
</p>
<table><tr><td> </td><td><pre class="smallexample">close (<var>new</var>);
fcntl (<var>old</var>, F_DUPFD, <var>new</var>)
</pre></td></tr></table>
<p>However, <code>dup2</code> does this atomically; there is no instant in the
middle of calling <code>dup2</code> at which <var>new</var> is closed and not yet a
duplicate of <var>old</var>.
</p></dd></dl>
<dl>
<dt><a name="index-F_005fDUPFD"></a><u>Macro:</u> int <b>F_DUPFD</b></dt>
<dd><p>This macro is used as the <var>command</var> argument to <code>fcntl</code>, to
copy the file descriptor given as the first argument.
</p>
<p>The form of the call in this case is:
</p>
<table><tr><td> </td><td><pre class="smallexample">fcntl (<var>old</var>, F_DUPFD, <var>next-filedes</var>)
</pre></td></tr></table>
<p>The <var>next-filedes</var> argument is of type <code>int</code> and specifies that
the file descriptor returned should be the next available one greater
than or equal to this value.
</p>
<p>The return value from <code>fcntl</code> with this command is normally the value
of the new file descriptor. A return value of <em>-1</em> indicates an
error. The following <code>errno</code> error conditions are defined for
this command:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The <var>old</var> argument is invalid.
</p>
</dd>
<dt> <code>EINVAL</code></dt>
<dd><p>The <var>next-filedes</var> argument is invalid.
</p>
</dd>
<dt> <code>EMFILE</code></dt>
<dd><p>There are no more file descriptors available—your program is already
using the maximum. In BSD and GNU, the maximum is controlled by a
resource limit that can be changed; see section <a href="libc_22.html#Limits-on-Resources">Limiting Resource Usage</a>, for
more information about the <code>RLIMIT_NOFILE</code> limit.
</p></dd>
</dl>
<p><code>ENFILE</code> is not a possible error code for <code>dup2</code> because
<code>dup2</code> does not create a new opening of a file; duplicate
descriptors do not count toward the limit which <code>ENFILE</code>
indicates. <code>EMFILE</code> is possible because it refers to the limit on
distinct descriptor numbers in use in one process.
</p></dd></dl>
<p>Here is an example showing how to use <code>dup2</code> to do redirection.
Typically, redirection of the standard streams (like <code>stdin</code>) is
done by a shell or shell-like program before calling one of the
<code>exec</code> functions (see section <a href="libc_26.html#Executing-a-File">Executing a File</a>) to execute a new
program in a child process. When the new program is executed, it
creates and initializes the standard streams to point to the
corresponding file descriptors, before its <code>main</code> function is
invoked.
</p>
<p>So, to redirect standard input to a file, the shell could do something
like:
</p>
<table><tr><td> </td><td><pre class="smallexample">pid = fork ();
if (pid == 0)
{
char *filename;
char *program;
int file;
…
file = TEMP_FAILURE_RETRY (open (filename, O_RDONLY));
dup2 (file, STDIN_FILENO);
TEMP_FAILURE_RETRY (close (file));
execv (program, NULL);
}
</pre></td></tr></table>
<p>There is also a more detailed example showing how to implement redirection
in the context of a pipeline of processes in <a href="libc_27.html#Launching-Jobs">Launching Jobs</a>.
</p>
<hr size="6">
<a name="Descriptor-Flags"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Duplicating-Descriptors" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#File-Status-Flags" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="File-Descriptor-Flags"></a>
<h2 class="section">13.13 File Descriptor Flags</h2>
<a name="index-file-descriptor-flags"></a>
<p><em>File descriptor flags</em> are miscellaneous attributes of a file
descriptor. These flags are associated with particular file
descriptors, so that if you have created duplicate file descriptors
from a single opening of a file, each descriptor has its own set of flags.
</p>
<p>Currently there is just one file descriptor flag: <code>FD_CLOEXEC</code>,
which causes the descriptor to be closed if you use any of the
<code>exec…</code> functions (see section <a href="libc_26.html#Executing-a-File">Executing a File</a>).
</p>
<p>The symbols in this section are defined in the header file
‘<tt>fcntl.h</tt>’.
<a name="index-fcntl_002eh-4"></a>
</p>
<dl>
<dt><a name="index-F_005fGETFD"></a><u>Macro:</u> int <b>F_GETFD</b></dt>
<dd><p>This macro is used as the <var>command</var> argument to <code>fcntl</code>, to
specify that it should return the file descriptor flags associated
with the <var>filedes</var> argument.
</p>
<p>The normal return value from <code>fcntl</code> with this command is a
nonnegative number which can be interpreted as the bitwise OR of the
individual flags (except that currently there is only one flag to use).
</p>
<p>In case of an error, <code>fcntl</code> returns <em>-1</em>. The following
<code>errno</code> error conditions are defined for this command:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The <var>filedes</var> argument is invalid.
</p></dd>
</dl>
</dd></dl>
<dl>
<dt><a name="index-F_005fSETFD"></a><u>Macro:</u> int <b>F_SETFD</b></dt>
<dd><p>This macro is used as the <var>command</var> argument to <code>fcntl</code>, to
specify that it should set the file descriptor flags associated with the
<var>filedes</var> argument. This requires a third <code>int</code> argument to
specify the new flags, so the form of the call is:
</p>
<table><tr><td> </td><td><pre class="smallexample">fcntl (<var>filedes</var>, F_SETFD, <var>new-flags</var>)
</pre></td></tr></table>
<p>The normal return value from <code>fcntl</code> with this command is an
unspecified value other than <em>-1</em>, which indicates an error.
The flags and error conditions are the same as for the <code>F_GETFD</code>
command.
</p></dd></dl>
<p>The following macro is defined for use as a file descriptor flag with
the <code>fcntl</code> function. The value is an integer constant usable
as a bit mask value.
</p>
<dl>
<dt><a name="index-FD_005fCLOEXEC"></a><u>Macro:</u> int <b>FD_CLOEXEC</b></dt>
<dd><a name="index-close_002don_002dexec-_0028file-descriptor-flag_0029"></a>
<p>This flag specifies that the file descriptor should be closed when
an <code>exec</code> function is invoked; see <a href="libc_26.html#Executing-a-File">Executing a File</a>. When
a file descriptor is allocated (as with <code>open</code> or <code>dup</code>),
this bit is initially cleared on the new file descriptor, meaning that
descriptor will survive into the new program after <code>exec</code>.
</p></dd></dl>
<p>If you want to modify the file descriptor flags, you should get the
current flags with <code>F_GETFD</code> and modify the value. Don’t assume
that the flags listed here are the only ones that are implemented; your
program may be run years from now and more flags may exist then. For
example, here is a function to set or clear the flag <code>FD_CLOEXEC</code>
without altering any other flags:
</p>
<table><tr><td> </td><td><pre class="smallexample">/* <span class="roman">Set the <code>FD_CLOEXEC</code> flag of <var>desc</var> if <var>value</var> is nonzero,</span>
<span class="roman">or clear the flag if <var>value</var> is 0.</span>
<span class="roman">Return 0 on success, or -1 on error with <code>errno</code> set.</span> */
int
set_cloexec_flag (int desc, int value)
{
int oldflags = fcntl (desc, F_GETFD, 0);
/* <span class="roman">If reading the flags failed, return error indication now.</span> */
if (oldflags < 0)
return oldflags;
/* <span class="roman">Set just the flag we want to set.</span> */
if (value != 0)
oldflags |= FD_CLOEXEC;
else
oldflags &= ~FD_CLOEXEC;
/* <span class="roman">Store modified flag word in the descriptor.</span> */
return fcntl (desc, F_SETFD, oldflags);
}
</pre></td></tr></table>
<hr size="6">
<a name="File-Status-Flags"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Descriptor-Flags" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Access-Modes" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="File-Status-Flags-1"></a>
<h2 class="section">13.14 File Status Flags</h2>
<a name="index-file-status-flags"></a>
<p><em>File status flags</em> are used to specify attributes of the opening of a
file. Unlike the file descriptor flags discussed in <a href="#Descriptor-Flags">File Descriptor Flags</a>, the file status flags are shared by duplicated file descriptors
resulting from a single opening of the file. The file status flags are
specified with the <var>flags</var> argument to <code>open</code>;
see section <a href="#Opening-and-Closing-Files">Opening and Closing Files</a>.
</p>
<p>File status flags fall into three categories, which are described in the
following sections.
</p>
<ul>
<li>
<a href="#Access-Modes">File Access Modes</a>, specify what type of access is allowed to the
file: reading, writing, or both. They are set by <code>open</code> and are
returned by <code>fcntl</code>, but cannot be changed.
</li><li>
<a href="#Open_002dtime-Flags">Open-time Flags</a>, control details of what <code>open</code> will do.
These flags are not preserved after the <code>open</code> call.
</li><li>
<a href="#Operating-Modes">I/O Operating Modes</a>, affect how operations such as <code>read</code> and
<code>write</code> are done. They are set by <code>open</code>, and can be fetched or
changed with <code>fcntl</code>.
</li></ul>
<p>The symbols in this section are defined in the header file
‘<tt>fcntl.h</tt>’.
<a name="index-fcntl_002eh-5"></a>
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Access-Modes">13.14.1 File Access Modes</a></td><td> </td><td align="left" valign="top"> Whether the descriptor can read or write.
</td></tr>
<tr><td align="left" valign="top"><a href="#Open_002dtime-Flags">13.14.2 Open-time Flags</a></td><td> </td><td align="left" valign="top"> Details of <code>open</code>.
</td></tr>
<tr><td align="left" valign="top"><a href="#Operating-Modes">13.14.3 I/O Operating Modes</a></td><td> </td><td align="left" valign="top"> Special modes to control I/O operations.
</td></tr>
<tr><td align="left" valign="top"><a href="#Getting-File-Status-Flags">13.14.4 Getting and Setting File Status Flags</a></td><td> </td><td align="left" valign="top"> Fetching and changing these flags.
</td></tr>
</table>
<hr size="6">
<a name="Access-Modes"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#File-Status-Flags" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Open_002dtime-Flags" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#File-Status-Flags" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="File-Access-Modes"></a>
<h3 class="subsection">13.14.1 File Access Modes</h3>
<p>The file access modes allow a file descriptor to be used for reading,
writing, or both. (In the GNU system, they can also allow none of these,
and allow execution of the file as a program.) The access modes are chosen
when the file is opened, and never change.
</p>
<dl>
<dt><a name="index-O_005fRDONLY"></a><u>Macro:</u> int <b>O_RDONLY</b></dt>
<dd><p>Open the file for read access.
</p></dd></dl>
<dl>
<dt><a name="index-O_005fWRONLY"></a><u>Macro:</u> int <b>O_WRONLY</b></dt>
<dd><p>Open the file for write access.
</p></dd></dl>
<dl>
<dt><a name="index-O_005fRDWR"></a><u>Macro:</u> int <b>O_RDWR</b></dt>
<dd><p>Open the file for both reading and writing.
</p></dd></dl>
<p>In the GNU system (and not in other systems), <code>O_RDONLY</code> and
<code>O_WRONLY</code> are independent bits that can be bitwise-ORed together,
and it is valid for either bit to be set or clear. This means that
<code>O_RDWR</code> is the same as <code>O_RDONLY|O_WRONLY</code>. A file access
mode of zero is permissible; it allows no operations that do input or
output to the file, but does allow other operations such as
<code>fchmod</code>. On the GNU system, since “read-only” or “write-only”
is a misnomer, ‘<tt>fcntl.h</tt>’ defines additional names for the file
access modes. These names are preferred when writing GNU-specific code.
But most programs will want to be portable to other POSIX.1 systems and
should use the POSIX.1 names above instead.
</p>
<dl>
<dt><a name="index-O_005fREAD"></a><u>Macro:</u> int <b>O_READ</b></dt>
<dd><p>Open the file for reading. Same as <code>O_RDONLY</code>; only defined on GNU.
</p></dd></dl>
<dl>
<dt><a name="index-O_005fWRITE"></a><u>Macro:</u> int <b>O_WRITE</b></dt>
<dd><p>Open the file for writing. Same as <code>O_WRONLY</code>; only defined on GNU.
</p></dd></dl>
<dl>
<dt><a name="index-O_005fEXEC"></a><u>Macro:</u> int <b>O_EXEC</b></dt>
<dd><p>Open the file for executing. Only defined on GNU.
</p></dd></dl>
<p>To determine the file access mode with <code>fcntl</code>, you must extract
the access mode bits from the retrieved file status flags. In the GNU
system, you can just test the <code>O_READ</code> and <code>O_WRITE</code> bits in
the flags word. But in other POSIX.1 systems, reading and writing
access modes are not stored as distinct bit flags. The portable way to
extract the file access mode bits is with <code>O_ACCMODE</code>.
</p>
<dl>
<dt><a name="index-O_005fACCMODE"></a><u>Macro:</u> int <b>O_ACCMODE</b></dt>
<dd><p>This macro stands for a mask that can be bitwise-ANDed with the file
status flag value to produce a value representing the file access mode.
The mode will be <code>O_RDONLY</code>, <code>O_WRONLY</code>, or <code>O_RDWR</code>.
(In the GNU system it could also be zero, and it never includes the
<code>O_EXEC</code> bit.)
</p></dd></dl>
<hr size="6">
<a name="Open_002dtime-Flags"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Access-Modes" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Operating-Modes" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#File-Status-Flags" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Open_002dtime-Flags-1"></a>
<h3 class="subsection">13.14.2 Open-time Flags</h3>
<p>The open-time flags specify options affecting how <code>open</code> will behave.
These options are not preserved once the file is open. The exception to
this is <code>O_NONBLOCK</code>, which is also an I/O operating mode and so it
<em>is</em> saved. See section <a href="#Opening-and-Closing-Files">Opening and Closing Files</a>, for how to call
<code>open</code>.
</p>
<p>There are two sorts of options specified by open-time flags.
</p>
<ul>
<li>
<em>File name translation flags</em> affect how <code>open</code> looks up the
file name to locate the file, and whether the file can be created.
<a name="index-file-name-translation-flags"></a>
<a name="index-flags_002c-file-name-translation"></a>
</li><li>
<em>Open-time action flags</em> specify extra operations that <code>open</code> will
perform on the file once it is open.
<a name="index-open_002dtime-action-flags"></a>
<a name="index-flags_002c-open_002dtime-action"></a>
</li></ul>
<p>Here are the file name translation flags.
</p>
<dl>
<dt><a name="index-O_005fCREAT"></a><u>Macro:</u> int <b>O_CREAT</b></dt>
<dd><p>If set, the file will be created if it doesn’t already exist.
<a name="index-create-on-open-_0028file-status-flag_0029"></a>
</p></dd></dl>
<dl>
<dt><a name="index-O_005fEXCL"></a><u>Macro:</u> int <b>O_EXCL</b></dt>
<dd><p>If both <code>O_CREAT</code> and <code>O_EXCL</code> are set, then <code>open</code> fails
if the specified file already exists. This is guaranteed to never
clobber an existing file.
</p></dd></dl>
<dl>
<dt><a name="index-O_005fNONBLOCK"></a><u>Macro:</u> int <b>O_NONBLOCK</b></dt>
<dd><a name="index-non_002dblocking-open"></a>
<p>This prevents <code>open</code> from blocking for a “long time” to open the
file. This is only meaningful for some kinds of files, usually devices
such as serial ports; when it is not meaningful, it is harmless and
ignored. Often opening a port to a modem blocks until the modem reports
carrier detection; if <code>O_NONBLOCK</code> is specified, <code>open</code> will
return immediately without a carrier.
</p>
<p>Note that the <code>O_NONBLOCK</code> flag is overloaded as both an I/O operating
mode and a file name translation flag. This means that specifying
<code>O_NONBLOCK</code> in <code>open</code> also sets nonblocking I/O mode;
see section <a href="#Operating-Modes">I/O Operating Modes</a>. To open the file without blocking but do normal
I/O that blocks, you must call <code>open</code> with <code>O_NONBLOCK</code> set and
then call <code>fcntl</code> to turn the bit off.
</p></dd></dl>
<dl>
<dt><a name="index-O_005fNOCTTY"></a><u>Macro:</u> int <b>O_NOCTTY</b></dt>
<dd><p>If the named file is a terminal device, don’t make it the controlling
terminal for the process. See section <a href="libc_27.html#Job-Control">Job Control</a>, for information about
what it means to be the controlling terminal.
</p>
<p>In the GNU system and 4.4 BSD, opening a file never makes it the
controlling terminal and <code>O_NOCTTY</code> is zero. However, other
systems may use a nonzero value for <code>O_NOCTTY</code> and set the
controlling terminal when you open a file that is a terminal device; so
to be portable, use <code>O_NOCTTY</code> when it is important to avoid this.
<a name="index-controlling-terminal_002c-setting"></a>
</p></dd></dl>
<p>The following three file name translation flags exist only in the GNU system.
</p>
<dl>
<dt><a name="index-O_005fIGNORE_005fCTTY"></a><u>Macro:</u> int <b>O_IGNORE_CTTY</b></dt>
<dd><p>Do not recognize the named file as the controlling terminal, even if it
refers to the process’s existing controlling terminal device. Operations
on the new file descriptor will never induce job control signals.
See section <a href="libc_27.html#Job-Control">Job Control</a>.
</p></dd></dl>
<dl>
<dt><a name="index-O_005fNOLINK"></a><u>Macro:</u> int <b>O_NOLINK</b></dt>
<dd><p>If the named file is a symbolic link, open the link itself instead of
the file it refers to. (<code>fstat</code> on the new file descriptor will
return the information returned by <code>lstat</code> on the link’s name.)
<a name="index-symbolic-link_002c-opening"></a>
</p></dd></dl>
<dl>
<dt><a name="index-O_005fNOTRANS"></a><u>Macro:</u> int <b>O_NOTRANS</b></dt>
<dd><p>If the named file is specially translated, do not invoke the translator.
Open the bare file the translator itself sees.
</p></dd></dl>
<p>The open-time action flags tell <code>open</code> to do additional operations
which are not really related to opening the file. The reason to do them
as part of <code>open</code> instead of in separate calls is that <code>open</code>
can do them <i>atomically</i>.
</p>
<dl>
<dt><a name="index-O_005fTRUNC"></a><u>Macro:</u> int <b>O_TRUNC</b></dt>
<dd><p>Truncate the file to zero length. This option is only useful for
regular files, not special files such as directories or FIFOs. POSIX.1
requires that you open the file for writing to use <code>O_TRUNC</code>. In
BSD and GNU you must have permission to write the file to truncate it,
but you need not open for write access.
</p>
<p>This is the only open-time action flag specified by POSIX.1. There is
no good reason for truncation to be done by <code>open</code>, instead of by
calling <code>ftruncate</code> afterwards. The <code>O_TRUNC</code> flag existed in
Unix before <code>ftruncate</code> was invented, and is retained for backward
compatibility.
</p></dd></dl>
<p>The remaining operating modes are BSD extensions. They exist only
on some systems. On other systems, these macros are not defined.
</p>
<dl>
<dt><a name="index-O_005fSHLOCK"></a><u>Macro:</u> int <b>O_SHLOCK</b></dt>
<dd><p>Acquire a shared lock on the file, as with <code>flock</code>.
See section <a href="#File-Locks">File Locks</a>.
</p>
<p>If <code>O_CREAT</code> is specified, the locking is done atomically when
creating the file. You are guaranteed that no other process will get
the lock on the new file first.
</p></dd></dl>
<dl>
<dt><a name="index-O_005fEXLOCK"></a><u>Macro:</u> int <b>O_EXLOCK</b></dt>
<dd><p>Acquire an exclusive lock on the file, as with <code>flock</code>.
See section <a href="#File-Locks">File Locks</a>. This is atomic like <code>O_SHLOCK</code>.
</p></dd></dl>
<hr size="6">
<a name="Operating-Modes"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Open_002dtime-Flags" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Getting-File-Status-Flags" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#File-Status-Flags" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="I_002fO-Operating-Modes"></a>
<h3 class="subsection">13.14.3 I/O Operating Modes</h3>
<p>The operating modes affect how input and output operations using a file
descriptor work. These flags are set by <code>open</code> and can be fetched
and changed with <code>fcntl</code>.
</p>
<dl>
<dt><a name="index-O_005fAPPEND"></a><u>Macro:</u> int <b>O_APPEND</b></dt>
<dd><p>The bit that enables append mode for the file. If set, then all
<code>write</code> operations write the data at the end of the file, extending
it, regardless of the current file position. This is the only reliable
way to append to a file. In append mode, you are guaranteed that the
data you write will always go to the current end of the file, regardless
of other processes writing to the file. Conversely, if you simply set
the file position to the end of file and write, then another process can
extend the file after you set the file position but before you write,
resulting in your data appearing someplace before the real end of file.
</p></dd></dl>
<dl>
<dt><a name="index-O_005fNONBLOCK-1"></a><u>Macro:</u> int <b>O_NONBLOCK</b></dt>
<dd><p>The bit that enables nonblocking mode for the file. If this bit is set,
<code>read</code> requests on the file can return immediately with a failure
status if there is no input immediately available, instead of blocking.
Likewise, <code>write</code> requests can also return immediately with a
failure status if the output can’t be written immediately.
</p>
<p>Note that the <code>O_NONBLOCK</code> flag is overloaded as both an I/O
operating mode and a file name translation flag; see section <a href="#Open_002dtime-Flags">Open-time Flags</a>.
</p></dd></dl>
<dl>
<dt><a name="index-O_005fNDELAY"></a><u>Macro:</u> int <b>O_NDELAY</b></dt>
<dd><p>This is an obsolete name for <code>O_NONBLOCK</code>, provided for
compatibility with BSD. It is not defined by the POSIX.1 standard.
</p></dd></dl>
<p>The remaining operating modes are BSD and GNU extensions. They exist only
on some systems. On other systems, these macros are not defined.
</p>
<dl>
<dt><a name="index-O_005fASYNC"></a><u>Macro:</u> int <b>O_ASYNC</b></dt>
<dd><p>The bit that enables asynchronous input mode. If set, then <code>SIGIO</code>
signals will be generated when input is available. See section <a href="#Interrupt-Input">Interrupt-Driven Input</a>.
</p>
<p>Asynchronous input mode is a BSD feature.
</p></dd></dl>
<dl>
<dt><a name="index-O_005fFSYNC"></a><u>Macro:</u> int <b>O_FSYNC</b></dt>
<dd><p>The bit that enables synchronous writing for the file. If set, each
<code>write</code> call will make sure the data is reliably stored on disk before
returning. </p>
<p>Synchronous writing is a BSD feature.
</p></dd></dl>
<dl>
<dt><a name="index-O_005fSYNC"></a><u>Macro:</u> int <b>O_SYNC</b></dt>
<dd><p>This is another name for <code>O_FSYNC</code>. They have the same value.
</p></dd></dl>
<dl>
<dt><a name="index-O_005fNOATIME"></a><u>Macro:</u> int <b>O_NOATIME</b></dt>
<dd><p>If this bit is set, <code>read</code> will not update the access time of the
file. See section <a href="libc_14.html#File-Times">File Times</a>. This is used by programs that do backups, so
that backing a file up does not count as reading it.
Only the owner of the file or the superuser may use this bit.
</p>
<p>This is a GNU extension.
</p></dd></dl>
<hr size="6">
<a name="Getting-File-Status-Flags"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Operating-Modes" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#File-Locks" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#File-Status-Flags" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Getting-and-Setting-File-Status-Flags"></a>
<h3 class="subsection">13.14.4 Getting and Setting File Status Flags</h3>
<p>The <code>fcntl</code> function can fetch or change file status flags.
</p>
<dl>
<dt><a name="index-F_005fGETFL"></a><u>Macro:</u> int <b>F_GETFL</b></dt>
<dd><p>This macro is used as the <var>command</var> argument to <code>fcntl</code>, to
read the file status flags for the open file with descriptor
<var>filedes</var>.
</p>
<p>The normal return value from <code>fcntl</code> with this command is a
nonnegative number which can be interpreted as the bitwise OR of the
individual flags. Since the file access modes are not single-bit values,
you can mask off other bits in the returned flags with <code>O_ACCMODE</code>
to compare them.
</p>
<p>In case of an error, <code>fcntl</code> returns <em>-1</em>. The following
<code>errno</code> error conditions are defined for this command:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The <var>filedes</var> argument is invalid.
</p></dd>
</dl>
</dd></dl>
<dl>
<dt><a name="index-F_005fSETFL"></a><u>Macro:</u> int <b>F_SETFL</b></dt>
<dd><p>This macro is used as the <var>command</var> argument to <code>fcntl</code>, to set
the file status flags for the open file corresponding to the
<var>filedes</var> argument. This command requires a third <code>int</code>
argument to specify the new flags, so the call looks like this:
</p>
<table><tr><td> </td><td><pre class="smallexample">fcntl (<var>filedes</var>, F_SETFL, <var>new-flags</var>)
</pre></td></tr></table>
<p>You can’t change the access mode for the file in this way; that is,
whether the file descriptor was opened for reading or writing.
</p>
<p>The normal return value from <code>fcntl</code> with this command is an
unspecified value other than <em>-1</em>, which indicates an error. The
error conditions are the same as for the <code>F_GETFL</code> command.
</p></dd></dl>
<p>If you want to modify the file status flags, you should get the current
flags with <code>F_GETFL</code> and modify the value. Don’t assume that the
flags listed here are the only ones that are implemented; your program
may be run years from now and more flags may exist then. For example,
here is a function to set or clear the flag <code>O_NONBLOCK</code> without
altering any other flags:
</p>
<table><tr><td> </td><td><pre class="smallexample">/* <span class="roman">Set the <code>O_NONBLOCK</code> flag of <var>desc</var> if <var>value</var> is nonzero,</span>
<span class="roman">or clear the flag if <var>value</var> is 0.</span>
<span class="roman">Return 0 on success, or -1 on error with <code>errno</code> set.</span> */
int
set_nonblock_flag (int desc, int value)
{
int oldflags = fcntl (desc, F_GETFL, 0);
/* <span class="roman">If reading the flags failed, return error indication now.</span> */
if (oldflags == -1)
return -1;
/* <span class="roman">Set just the flag we want to set.</span> */
if (value != 0)
oldflags |= O_NONBLOCK;
else
oldflags &= ~O_NONBLOCK;
/* <span class="roman">Store modified flag word in the descriptor.</span> */
return fcntl (desc, F_SETFL, oldflags);
}
</pre></td></tr></table>
<hr size="6">
<a name="File-Locks"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Getting-File-Status-Flags" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Interrupt-Input" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="File-Locks-1"></a>
<h2 class="section">13.15 File Locks</h2>
<a name="index-file-locks"></a>
<a name="index-record-locking"></a>
<p>The remaining <code>fcntl</code> commands are used to support <em>record
locking</em>, which permits multiple cooperating programs to prevent each
other from simultaneously accessing parts of a file in error-prone
ways.
</p>
<a name="index-exclusive-lock"></a>
<a name="index-write-lock"></a>
<p>An <em>exclusive</em> or <em>write</em> lock gives a process exclusive access
for writing to the specified part of the file. While a write lock is in
place, no other process can lock that part of the file.
</p>
<a name="index-shared-lock"></a>
<a name="index-read-lock"></a>
<p>A <em>shared</em> or <em>read</em> lock prohibits any other process from
requesting a write lock on the specified part of the file. However,
other processes can request read locks.
</p>
<p>The <code>read</code> and <code>write</code> functions do not actually check to see
whether there are any locks in place. If you want to implement a
locking protocol for a file shared by multiple processes, your application
must do explicit <code>fcntl</code> calls to request and clear locks at the
appropriate points.
</p>
<p>Locks are associated with processes. A process can only have one kind
of lock set for each byte of a given file. When any file descriptor for
that file is closed by the process, all of the locks that process holds
on that file are released, even if the locks were made using other
descriptors that remain open. Likewise, locks are released when a
process exits, and are not inherited by child processes created using
<code>fork</code> (see section <a href="libc_26.html#Creating-a-Process">Creating a Process</a>).
</p>
<p>When making a lock, use a <code>struct flock</code> to specify what kind of
lock and where. This data type and the associated macros for the
<code>fcntl</code> function are declared in the header file ‘<tt>fcntl.h</tt>’.
<a name="index-fcntl_002eh-6"></a>
</p>
<dl>
<dt><a name="index-struct-flock"></a><u>Data Type:</u> <b>struct flock</b></dt>
<dd><p>This structure is used with the <code>fcntl</code> function to describe a file
lock. It has these members:
</p>
<dl compact="compact">
<dt> <code>short int l_type</code></dt>
<dd><p>Specifies the type of the lock; one of <code>F_RDLCK</code>, <code>F_WRLCK</code>, or
<code>F_UNLCK</code>.
</p>
</dd>
<dt> <code>short int l_whence</code></dt>
<dd><p>This corresponds to the <var>whence</var> argument to <code>fseek</code> or
<code>lseek</code>, and specifies what the offset is relative to. Its value
can be one of <code>SEEK_SET</code>, <code>SEEK_CUR</code>, or <code>SEEK_END</code>.
</p>
</dd>
<dt> <code>off_t l_start</code></dt>
<dd><p>This specifies the offset of the start of the region to which the lock
applies, and is given in bytes relative to the point specified by
<code>l_whence</code> member.
</p>
</dd>
<dt> <code>off_t l_len</code></dt>
<dd><p>This specifies the length of the region to be locked. A value of
<code>0</code> is treated specially; it means the region extends to the end of
the file.
</p>
</dd>
<dt> <code>pid_t l_pid</code></dt>
<dd><p>This field is the process ID (see section <a href="libc_26.html#Process-Creation-Concepts">Process Creation Concepts</a>) of the
process holding the lock. It is filled in by calling <code>fcntl</code> with
the <code>F_GETLK</code> command, but is ignored when making a lock.
</p></dd>
</dl>
</dd></dl>
<dl>
<dt><a name="index-F_005fGETLK"></a><u>Macro:</u> int <b>F_GETLK</b></dt>
<dd><p>This macro is used as the <var>command</var> argument to <code>fcntl</code>, to
specify that it should get information about a lock. This command
requires a third argument of type <code>struct flock *</code> to be passed
to <code>fcntl</code>, so that the form of the call is:
</p>
<table><tr><td> </td><td><pre class="smallexample">fcntl (<var>filedes</var>, F_GETLK, <var>lockp</var>)
</pre></td></tr></table>
<p>If there is a lock already in place that would block the lock described
by the <var>lockp</var> argument, information about that lock overwrites
<code>*<var>lockp</var></code>. Existing locks are not reported if they are
compatible with making a new lock as specified. Thus, you should
specify a lock type of <code>F_WRLCK</code> if you want to find out about both
read and write locks, or <code>F_RDLCK</code> if you want to find out about
write locks only.
</p>
<p>There might be more than one lock affecting the region specified by the
<var>lockp</var> argument, but <code>fcntl</code> only returns information about
one of them. The <code>l_whence</code> member of the <var>lockp</var> structure is
set to <code>SEEK_SET</code> and the <code>l_start</code> and <code>l_len</code> fields
set to identify the locked region.
</p>
<p>If no lock applies, the only change to the <var>lockp</var> structure is to
update the <code>l_type</code> to a value of <code>F_UNLCK</code>.
</p>
<p>The normal return value from <code>fcntl</code> with this command is an
unspecified value other than <em>-1</em>, which is reserved to indicate an
error. The following <code>errno</code> error conditions are defined for
this command:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The <var>filedes</var> argument is invalid.
</p>
</dd>
<dt> <code>EINVAL</code></dt>
<dd><p>Either the <var>lockp</var> argument doesn’t specify valid lock information,
or the file associated with <var>filedes</var> doesn’t support locks.
</p></dd>
</dl>
</dd></dl>
<dl>
<dt><a name="index-F_005fSETLK"></a><u>Macro:</u> int <b>F_SETLK</b></dt>
<dd><p>This macro is used as the <var>command</var> argument to <code>fcntl</code>, to
specify that it should set or clear a lock. This command requires a
third argument of type <code>struct flock *</code> to be passed to
<code>fcntl</code>, so that the form of the call is:
</p>
<table><tr><td> </td><td><pre class="smallexample">fcntl (<var>filedes</var>, F_SETLK, <var>lockp</var>)
</pre></td></tr></table>
<p>If the process already has a lock on any part of the region, the old lock
on that part is replaced with the new lock. You can remove a lock
by specifying a lock type of <code>F_UNLCK</code>.
</p>
<p>If the lock cannot be set, <code>fcntl</code> returns immediately with a value
of <em>-1</em>. This function does not block waiting for other processes
to release locks. If <code>fcntl</code> succeeds, it return a value other
than <em>-1</em>.
</p>
<p>The following <code>errno</code> error conditions are defined for this
function:
</p>
<dl compact="compact">
<dt> <code>EAGAIN</code></dt>
<dt> <code>EACCES</code></dt>
<dd><p>The lock cannot be set because it is blocked by an existing lock on the
file. Some systems use <code>EAGAIN</code> in this case, and other systems
use <code>EACCES</code>; your program should treat them alike, after
<code>F_SETLK</code>. (The GNU system always uses <code>EAGAIN</code>.)
</p>
</dd>
<dt> <code>EBADF</code></dt>
<dd><p>Either: the <var>filedes</var> argument is invalid; you requested a read lock
but the <var>filedes</var> is not open for read access; or, you requested a
write lock but the <var>filedes</var> is not open for write access.
</p>
</dd>
<dt> <code>EINVAL</code></dt>
<dd><p>Either the <var>lockp</var> argument doesn’t specify valid lock information,
or the file associated with <var>filedes</var> doesn’t support locks.
</p>
</dd>
<dt> <code>ENOLCK</code></dt>
<dd><p>The system has run out of file lock resources; there are already too
many file locks in place.
</p>
<p>Well-designed file systems never report this error, because they have no
limitation on the number of locks. However, you must still take account
of the possibility of this error, as it could result from network access
to a file system on another machine.
</p></dd>
</dl>
</dd></dl>
<dl>
<dt><a name="index-F_005fSETLKW"></a><u>Macro:</u> int <b>F_SETLKW</b></dt>
<dd><p>This macro is used as the <var>command</var> argument to <code>fcntl</code>, to
specify that it should set or clear a lock. It is just like the
<code>F_SETLK</code> command, but causes the process to block (or wait)
until the request can be specified.
</p>
<p>This command requires a third argument of type <code>struct flock *</code>, as
for the <code>F_SETLK</code> command.
</p>
<p>The <code>fcntl</code> return values and errors are the same as for the
<code>F_SETLK</code> command, but these additional <code>errno</code> error conditions
are defined for this command:
</p>
<dl compact="compact">
<dt> <code>EINTR</code></dt>
<dd><p>The function was interrupted by a signal while it was waiting.
See section <a href="libc_24.html#Interrupted-Primitives">Primitives Interrupted by Signals</a>.
</p>
</dd>
<dt> <code>EDEADLK</code></dt>
<dd><p>The specified region is being locked by another process. But that
process is waiting to lock a region which the current process has
locked, so waiting for the lock would result in deadlock. The system
does not guarantee that it will detect all such conditions, but it lets
you know if it notices one.
</p></dd>
</dl>
</dd></dl>
<p>The following macros are defined for use as values for the <code>l_type</code>
member of the <code>flock</code> structure. The values are integer constants.
</p>
<dl compact="compact">
<dd><a name="index-F_005fRDLCK"></a>
</dd>
<dt> <code>F_RDLCK</code></dt>
<dd><p>This macro is used to specify a read (or shared) lock.
</p>
<a name="index-F_005fWRLCK"></a>
</dd>
<dt> <code>F_WRLCK</code></dt>
<dd><p>This macro is used to specify a write (or exclusive) lock.
</p>
<a name="index-F_005fUNLCK"></a>
</dd>
<dt> <code>F_UNLCK</code></dt>
<dd><p>This macro is used to specify that the region is unlocked.
</p></dd>
</dl>
<p>As an example of a situation where file locking is useful, consider a
program that can be run simultaneously by several different users, that
logs status information to a common file. One example of such a program
might be a game that uses a file to keep track of high scores. Another
example might be a program that records usage or accounting information
for billing purposes.
</p>
<p>Having multiple copies of the program simultaneously writing to the
file could cause the contents of the file to become mixed up. But
you can prevent this kind of problem by setting a write lock on the
file before actually writing to the file.
</p>
<p>If the program also needs to read the file and wants to make sure that
the contents of the file are in a consistent state, then it can also use
a read lock. While the read lock is set, no other process can lock
that part of the file for writing.
</p>
<p>Remember that file locks are only a <em>voluntary</em> protocol for
controlling access to a file. There is still potential for access to
the file by programs that don’t use the lock protocol.
</p>
<hr size="6">
<a name="Interrupt-Input"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#File-Locks" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#IOCTLs" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Interrupt_002dDriven-Input"></a>
<h2 class="section">13.16 Interrupt-Driven Input</h2>
<a name="index-interrupt_002ddriven-input"></a>
<p>If you set the <code>O_ASYNC</code> status flag on a file descriptor
(see section <a href="#File-Status-Flags">File Status Flags</a>), a <code>SIGIO</code> signal is sent whenever
input or output becomes possible on that file descriptor. The process
or process group to receive the signal can be selected by using the
<code>F_SETOWN</code> command to the <code>fcntl</code> function. If the file
descriptor is a socket, this also selects the recipient of <code>SIGURG</code>
signals that are delivered when out-of-band data arrives on that socket;
see <a href="libc_16.html#Out_002dof_002dBand-Data">Out-of-Band Data</a>. (<code>SIGURG</code> is sent in any situation
where <code>select</code> would report the socket as having an “exceptional
condition”. See section <a href="#Waiting-for-I_002fO">Waiting for Input or Output</a>.)
</p>
<p>If the file descriptor corresponds to a terminal device, then <code>SIGIO</code>
signals are sent to the foreground process group of the terminal.
See section <a href="libc_27.html#Job-Control">Job Control</a>.
</p>
<a name="index-fcntl_002eh-7"></a>
<p>The symbols in this section are defined in the header file
‘<tt>fcntl.h</tt>’.
</p>
<dl>
<dt><a name="index-F_005fGETOWN"></a><u>Macro:</u> int <b>F_GETOWN</b></dt>
<dd><p>This macro is used as the <var>command</var> argument to <code>fcntl</code>, to
specify that it should get information about the process or process
group to which <code>SIGIO</code> signals are sent. (For a terminal, this is
actually the foreground process group ID, which you can get using
<code>tcgetpgrp</code>; see <a href="libc_27.html#Terminal-Access-Functions">Functions for Controlling Terminal Access</a>.)
</p>
<p>The return value is interpreted as a process ID; if negative, its
absolute value is the process group ID.
</p>
<p>The following <code>errno</code> error condition is defined for this command:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The <var>filedes</var> argument is invalid.
</p></dd>
</dl>
</dd></dl>
<dl>
<dt><a name="index-F_005fSETOWN"></a><u>Macro:</u> int <b>F_SETOWN</b></dt>
<dd><p>This macro is used as the <var>command</var> argument to <code>fcntl</code>, to
specify that it should set the process or process group to which
<code>SIGIO</code> signals are sent. This command requires a third argument
of type <code>pid_t</code> to be passed to <code>fcntl</code>, so that the form of
the call is:
</p>
<table><tr><td> </td><td><pre class="smallexample">fcntl (<var>filedes</var>, F_SETOWN, <var>pid</var>)
</pre></td></tr></table>
<p>The <var>pid</var> argument should be a process ID. You can also pass a
negative number whose absolute value is a process group ID.
</p>
<p>The return value from <code>fcntl</code> with this command is <em>-1</em>
in case of error and some other value if successful. The following
<code>errno</code> error conditions are defined for this command:
</p>
<dl compact="compact">
<dt> <code>EBADF</code></dt>
<dd><p>The <var>filedes</var> argument is invalid.
</p>
</dd>
<dt> <code>ESRCH</code></dt>
<dd><p>There is no process or process group corresponding to <var>pid</var>.
</p></dd>
</dl>
</dd></dl>
<hr size="6">
<a name="IOCTLs"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Interrupt-Input" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Generic-I_002fO-Control-operations"></a>
<h2 class="section">13.17 Generic I/O Control operations</h2>
<a name="index-generic-i_002fo-control-operations"></a>
<a name="index-IOCTLs"></a>
<p>The GNU system can handle most input/output operations on many different
devices and objects in terms of a few file primitives - <code>read</code>,
<code>write</code> and <code>lseek</code>. However, most devices also have a few
peculiar operations which do not fit into this model. Such as:
</p>
<ul>
<li>
Changing the character font used on a terminal.
</li><li>
Telling a magnetic tape system to rewind or fast forward. (Since they
cannot move in byte increments, <code>lseek</code> is inapplicable).
</li><li>
Ejecting a disk from a drive.
</li><li>
Playing an audio track from a CD-ROM drive.
</li><li>
Maintaining routing tables for a network.
</li></ul>
<p>Although some such objects such as sockets and terminals
<a name="DOCF2" href="libc_fot.html#FOOT2">(2)</a> have special functions of their own, it would
not be practical to create functions for all these cases.
</p>
<p>Instead these minor operations, known as <em>IOCTL</em>s, are assigned code
numbers and multiplexed through the <code>ioctl</code> function, defined in
<code>sys/ioctl.h</code>. The code numbers themselves are defined in many
different headers.
</p>
<dl>
<dt><a name="index-ioctl"></a><u>Function:</u> int <b>ioctl</b><i> (int <var>filedes</var>, int <var>command</var>, …)</i></dt>
<dd>
<p>The <code>ioctl</code> function performs the generic I/O operation
<var>command</var> on <var>filedes</var>.
</p>
<p>A third argument is usually present, either a single number or a pointer
to a structure. The meaning of this argument, the returned value, and
any error codes depends upon the command used. Often <em>-1</em> is
returned for a failure.
</p>
</dd></dl>
<p>On some systems, IOCTLs used by different devices share the same numbers.
Thus, although use of an inappropriate IOCTL <em>usually</em> only produces
an error, you should not attempt to use device-specific IOCTLs on an
unknown device.
</p>
<p>Most IOCTLs are OS-specific and/or only used in special system utilities,
and are thus beyond the scope of this document. For an example of the use
of an IOCTL, see <a href="libc_16.html#Out_002dof_002dBand-Data">Out-of-Band Data</a>.
</p><hr size="6">
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Low_002dLevel-I_002fO" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="libc_14.html#File-System-Interface" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="libc.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="libc_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="libc_42.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="libc_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<p>
<font size="-1">
This document was generated by <em>root</em> on <em>April 20, 2012</em> using <a href="http://www.nongnu.org/texi2html/"><em>texi2html 1.82</em></a>.
</font>
<br>
</p>
</body>
</html>
|