{"id":453,"date":"2023-10-26T06:19:32","date_gmt":"2023-10-26T14:19:32","guid":{"rendered":"http:\/\/bitas.net.cn\/?page_id=453"},"modified":"2023-10-26T06:21:54","modified_gmt":"2023-10-26T14:21:54","slug":"319-2","status":"publish","type":"page","link":"http:\/\/bitas.net.cn\/index.php\/sourcecode\/319-2\/","title":{"rendered":"319 \u5185\u5b58\u6620\u5c04\u6587\u4ef6"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h>\n#include &lt;stdlib.h>\n#include &lt;unistd.h>\n#include &lt;fcntl.h>\n#include &lt;sys\/stat.h>\n#include &lt;sys\/mman.h>\n#include &lt;string.h>\n\n#define SEARCH_WORD \"like\"\n\nint main(int argc, char *argv&#91;])\n{\n    size_t page = (size_t)sysconf(_SC_PAGESIZE);\n    printf(\"page size is %ld.\\n\", page);\n\n    int fd = open(\".\/mmap.manual\", O_RDONLY);\n    struct stat st; \n    fstat(fd, &amp;st);\n\n    off_t offset = 0;\n    off_t aligned = offset &amp; ~(page - 1); \n    \/* offset for mmap() must be page aligned *\/\n\n    size_t length = st.st_size - offset;\n\n    void *addr = mmap(NULL, length + offset - aligned, PROT_READ,\n            MAP_PRIVATE, fd, aligned);\n    if (addr == MAP_FAILED) {\n        perror(\"mmap\");\n        exit(EXIT_FAILURE);\n    }   \n\n    int n = 0;\n    char *word = strstr(addr + offset - aligned, SEARCH_WORD);\n    while (word) {\n        n++;\n        word = strstr(word + strlen(SEARCH_WORD), SEARCH_WORD);\n    }   \n    printf(\"there are %d `%s` word.\\n\", n, SEARCH_WORD);\n\n    close(fd);\n    munmap(addr, length);\n    exit(EXIT_SUCCESS);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"parent":127,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-453","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages\/453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/comments?post=453"}],"version-history":[{"count":1,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages\/453\/revisions"}],"predecessor-version":[{"id":454,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages\/453\/revisions\/454"}],"up":[{"embeddable":true,"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/pages\/127"}],"wp:attachment":[{"href":"http:\/\/bitas.net.cn\/index.php\/wp-json\/wp\/v2\/media?parent=453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}