Write a C program to create a mmap() that is shared by multiple threads. Use fst
ID: 3856737 • Letter: W
Question
Write a C program to create a mmap() that is shared by multiple threads. Use fstat() to obtain the size of the mapped file to size the mapping. Find out the number of logical processors (or cores) available programmatically by calling sysconf(), as shown, and display this number.
int numCPU = sysconf(_SC_NPROCESSORS_ONLN);
For this problem, however, you are going to create four (4) threads only. Every thread should see the mapping. Each thread will display separate partial contents (sb.st_size/4 bytes) of the file.
Finally, unmap and close the file.
Please show the command line of how to comiple and run the code and the screenshots
Explanation / Answer
#include /* shared memory and mmap() */ #include /* for getopt() */ #include /* errno and perror */ #include /* O_flags */ #include int main(int argc, char **argv) { int perms = 0600; /* permissions */ size_t size = 65536; /* segment size */ int oflags = 0; /* open flags receives -c, -x, -t */ int ropt = 0; /* -r option seen */ int wopt = 0; /* -w option seen */ int shm_fd; /* file descriptor */ int mprot = PROT_READ; /* protection flags to mmap */ int mflags = MAP_SHARED; /* mmap flags */ void *attach; /* assigned memory adddress */ char *path; /* ->first non-option argument */ int c; while ( -1 != (c = getopt(argc,argv,"p:s:cxrtw")) ) { switch (c) { case 'p': /* permissions */ perms = (int) strtoul(optarg, NULL, 0); break; case 's': /* segment size */ size = (size_t) strtoul(optarg, NULL, 0); break; case 'c': /* use O_CREAT */ oflags |= O_CREAT; break; case 'x': /* use O_EXCL */ oflags |= O_EXCL; break; case 't': /* use O_TRUNC */ oflags |= O_TRUNC; break; case 'r': /* use O_RDONLY */ ropt = 1; break; case 'w': /* wait after attaching */ wopt = 1; break; default: /* unknown or missing argument */ return -1; } /* switch */ } /* while */ if (optindRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.