Reputation:
Exactly, I thought that I finished my project until compiling isn't accepted on ubuntu because of mmap()
. I'm trying to access(read) files by using fork()
. It's okey. But, When I want to count number of read files, entered folder(directories) and child, I powned! How and What can I use or change mmap()
because I get error: ‘MAP_ANON’ undeclared (first use in this function)|
. On mac, It's okey but on ubuntu error. Thank you for helps.
#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;
}
Upvotes: 1
Views: 8081
Reputation: 25179
From the man page for mmap(2)
(my bold):
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 synonymMAP_ANON
),MAP_DENYWRITE
,MAP_EXECUTABLE
,MAP_FILE
,MAP_GROWSDOWN
,MAP_HUGETLB
,MAP_LOCKED
,MAP_NONBLOCK
,MAP_NORESERVE
,MAP_POPULATE
, andMAP_STACK
.
So you will need to define _BSD_SOURCE
, _SVID_SOURCE
or (if I read that right) _GNU_SOURCE
at the top of the file, but in any event prior to
#include <sys/mman.h>
As per @mafso's comment, best to do this prior to any system header include (not least in case an another header itself includes <sys/mman.h>
Upvotes: 3