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

标签 c linux mmap fork

确切地说,我认为我已经完成了我的项目,直到由于 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) 的手册页(我的粗体):

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> 的情况下

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

相关文章:

c - Pro*C 将动态数组传递给 PL/SQL 过程

c - 如何将 CMake 的输出重定向到文件而不是 Windows cmd 提示符

C初学者执行,从用户那里获取整数并计算其中有多少次1

c - 为什么我在使用 printf 时会出现段错误?

linux - 如何从 MAKEFILE 中的字符串列表中提取具有特定模式的字符串?

c - OSX 内存分配缓慢

linux - 如何使用 bash 命令创建 100% 的内存峰值

c - 嵌入式 linux,应用程序状态卡住,重新启动

c - 在 C 中使用 mmap 读取二进制文件时出现段错误

linux - 查询 mmap() 分配的大小