没错,我以为我完成了我的项目,直到编译由于mmap()而在ubuntu上不被接受。我试图使用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)的手册页(我的粗体):
仅当定义了_BSD_SOURCE_SVID_SOURCE时,才会定义某些标志常量。(要求_GNU_SOURCE也就足够了,而且要求宏更符合逻辑,因为这些标志都是Linux特有的。)相关标志是:MAP_32BITMAP_ANONYMOUS(以及同义词MAP_ANON),MAP_DENYWRITEMAP_EXECUTABLEMAP_FILEMAP_GROWSDOWNMAP_HUGETLBMAP_LOCKEDMAP_NONBLOCKMAP_NORESERVEMAP_POPULATEMAP_STACK_BSD_SOURCE_SVID_SOURCE,和_GNU_SOURCE
因此,您需要在文件顶部定义<sys/mman.h>、或(如果我读对了)或,但无论如何

#include <sys/mman.h>

根据@mafso的评论,最好在任何系统头包含(尤其是在另一个头本身包含

关于c - linux上的mmap错误(使用someelse),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29264322/

10-12 04:15
查看更多