linux 上的 mmap 错误(使用其他东西)

mmap error on linux (using somethingelse)

确切地说,我以为我已经完成了我的项目,直到 ubuntu 上不接受编译,因为 mmap()。我正在尝试使用 fork() 访问(读取)文件。没关系。但是,当我想计算读取文件、输入文件夹(目录)和子文件夹的数量时,我迷路了!我可以使用或更改 mmap() 的方式和内容,因为我得到了 error: ‘MAP_ANON’ undeclared (first use in this function)| .在 mac 上没问题,但在 ubuntu 上出错。谢谢你的帮助。

    #define _XOPEN_SOURCE 700
    #define _POSIX_C_SOURCE 200809L

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/wait.h>
    #include <dirent.h>
    #include <errno.h>
    #include <ftw.h>
    #include <ctype.h>
    #include <sys/mman.h>



    #define MAX_PATH_LEN        2048


    static int *wordCount = 0;
    static int *childCount = 0;
    static int *folderCount = 0;



    int relatedWord(const char *arr);

    int checkWord(const char arr[], int size);

    void err_sys(const char *msg);

    int disp(const char *filepath, const struct stat *finfo, int flag, struct FTW *ftw);


    int main(int argc, char *argv[])
    {
        //struct stat finfo;

        //int count = 0;

        wordCount = mmap(NULL, sizeof *wordCount, PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANON, -1, 0);
        childCount = mmap(NULL, sizeof *childCount, PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANON, -1, 0);
        folderCount = mmap(NULL, sizeof *folderCount, PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANON, -1, 0);

        if (argc != 2) {
            fprintf(stderr, "Wrong number of arguments!\nUsage: dirwalk6 <path>\n");
            exit(EXIT_FAILURE);
        }

        if (nftw(argv[1], disp, 20, 0) < 0)
            err_sys("ntfw");

        printf( "\nTotal words = %d\n\n", *wordCount);
        printf( "\nTotal folders = %d\n\n", *folderCount);
        printf( "\nTotal childs = %d\n\n", *childCount);


        return 0;
    }

int disp(const char *filepath, const struct stat *finfo, int flag, struct FTW *ftw)
{

    int count = 0; /* number of words */

    switch (flag) {
        case FTW_F: /* determining file */

            printf("%*s%s\n", ftw->level * 4, "", filepath);

            pid_t pid;

            pid=fork();

            if(pid < 0)
            {
                perror("Error corresponding to fork()");
            }
            else if (pid == 0)
            {

                count += relatedWord(filepath);
                *wordCount += count;
                *childCount += 1;
                exit(1);

            }
            else
            {
                while( pid != wait(0) ) ;
            }
            // printf( "word = %d,   file = %s \n", count,filepath);

            break;
        case FTW_D:  /* determining folder */
            printf("%*s%s\n", ftw->level * 4, "", filepath + ftw->base);
            *folderCount += 1;
            break;
    }

    return 0;
}

来自 mmap(2) 的手册页(我的粗体):

Certain flags constants are defined only if either _BSD_SOURCE or _SVID_SOURCE is defined. (Requiring _GNU_SOURCE also suffices, and requiring that macro specifically would have been more logical, since these flags are all Linux specific.) The relevant flags are: MAP_32BIT, MAP_ANONYMOUS (and the synonym MAP_ANON), MAP_DENYWRITE, MAP_EXECUTABLE, MAP_FILE, MAP_GROWSDOWN, MAP_HUGETLB, MAP_LOCKED, MAP_NONBLOCK, MAP_NORESERVE, MAP_POPULATE, and MAP_STACK.

因此您需要在文件顶部定义 _BSD_SOURCE_SVID_SOURCE 或(如果我没看错的话)_GNU_SOURCE,但无论如何要先于

#include <sys/mman.h>

根据@mafso 的评论,最好在任何系统 header 包含之前执行此操作(尤其是在另一个 header 本身包含 <sys/mman.h>

的情况下