Modify the mmap-write and mmap-read programs such that the former write n ran- d
ID: 3736662 • Letter: M
Question
Modify the mmap-write and mmap-read programs such that the former write n ran-
dom integers to the file and the latter reads and prints all integers. n is an argument to
both programs. It must be greater than zero. Include error handling when parsing the
argument n and for the system calls open() and mmap().
C language under Linux
Example executions:
$ make
$ ./mmap-write /tmp/afile 10
$ cat /tmp/afile
-68 7 24 -10 48 24 -50 -97 52 -60
./mmap-read /tmp/afile 10
-68 7 24 -10 48 24 -50 -97 52 -60
$ ./mmap-write /tmp/afile 0
invalid number of integers
$ ./mmap-write /tmp/afile x
failed to parse number of integers
./mmap-read /tmp/invalidfile 10
open error: No such file or directory
******************************************
Makefile
all: mmap-read.o mmap-write.o
gcc -o mmap-read mmap-read.o
gcc -o mmap-write mmap-write.o
mmap-read.o: mmap-read.c
gcc -c mmap-read.c
mmap-write.o: mmap-write.c
gcc -c mmap-write.c
clean:
rm -f *.o mmap-read mmap-write *~
******************************************
mmap.h
/*(It has this line of code only)*/
#define FILESIZE 256
******************************************
mmap-write.c :
/* Mapped-memory example - Writer program
*/
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "mmap.h"
/* return a uniformly generated random number in the range [low,high]
*/
int random_range (unsigned const low, unsigned const high) {
unsigned const range = high - low + 1;
return low + (int) (((double) range) * rand () / (RAND_MAX + 1.0));
}
/* Create and write to a shared file for communication with another process.
*
* argv[1] = file name
*
* Note: Error processing is omitted
*/
int main (int argc, char* const argv[]) {
int fd;
void* file_memory;
/* seed the random number generator */
srand (time (NULL));
/* open or create a file to hold an unsigned integer */
fd = open (argv[1], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
/* write FILESIZE spaces */
for (int i=0; i<FILESIZE; i++) write (fd, " ", 1);
write (fd, "", 1); /* write a NULL at EOF */
/* create the memory-mapping
1st param=start addr of mapping into memory, NULL means chosen by OS
2nd param=length of map (bytes)
3rd param=protection
4th param=options, MAP_SHARED used for interprocess communications
5th param=file descriptor of mapped file
6th param=offset from start of file where mapping starts
*/
file_memory = mmap (NULL, FILESIZE, PROT_WRITE, MAP_SHARED, fd, 0);
close (fd);
/* write a random integer to memory-mapped area */
sprintf((char*) file_memory, "%d ", random_range (-100, 100));
/* release the memory */
munmap (file_memory, FILESIZE);
}
******************************************
mmap-read.c :
/* Mapped-memory example - Reader program
*/
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include "mmap.h"
int main (int argc, char* const argv[]) {
int fd;
void* file_memory;
int integer;
/* open the file */
fd = open (argv[1], O_RDWR, S_IRUSR | S_IWUSR);
/* create the memory-mapping */
file_memory = mmap (NULL, FILESIZE, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
close (fd);
/* read and print the integer */
sscanf (file_memory, "%d", &integer);
printf ("file contains: %d ", integer);
/* release the memory */
munmap (file_memory, FILESIZE);
return 0;
}
Explanation / Answer
Here is the completed code. Check the Makefile. You can alternatively compile mmap-read.c, mmap-write.c in terminal. See the snapshot
/*******************************/mmap-read.c
/* Mapped-memory example - Reader program
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include "mmap.h"
int main (int argc, char* const argv[]) {
int fd;
void* file_memory;
char integer[256];
int size=atoi(argv[2]);
if(size<1)
{
fprintf(stderr,"Invalid no of integers ");
return 0;
}
/* open the file */
fd = open (argv[1], O_RDWR, S_IRUSR | S_IWUSR);
/* create the memory-mapping */
file_memory = mmap (NULL, FILESIZE, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
close (fd);
int i;
int len;
/* read and print the integer */
printf ("file contains: ");
char buf;
for(i=0;i<size;i++)
{
sscanf (file_memory, "%s", integer);
printf ("%s ", integer);
len=strlen(integer);
file_memory=file_memory+len+1;
}
printf(" ");
/* release the memory */
munmap (file_memory, FILESIZE);
return 0;
}
/***************************/mmap-write.c
/* Mapped-memory example - Writer program
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "mmap.h"
/* return a uniformly generated random number in the range [low,high]
*/
int random_range (unsigned const low, unsigned const high) {
unsigned const range = high - low + 1;
return low + (int) (((double) range) * rand () / (RAND_MAX + 1.0));
}
/* Create and write to a shared file for communication with another process.
*
* argv[1] = file name
*
* Note: Error processing is omitted
*/
int main (int argc, char* const argv[]) {
int fd;
void* file_memory;
int size=atoi(argv[2]);
if(size<1)
{
fprintf(stderr,"Invalid no of integers ");
return 0;
}
/* seed the random number generator */
srand (time (NULL));
/* open or create a file to hold an unsigned integer */
fd = open (argv[1], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
int i;
/* write FILESIZE spaces */
for (i=0; i<FILESIZE; i++) write (fd, " ", 1);
write (fd, "", 1); /* write a NULL at EOF */
/* create the memory-mapping
1st param=start addr of mapping into memory, NULL means chosen by OS
2nd param=length of map (bytes)
3rd param=protection
4th param=options, MAP_SHARED used for interprocess communications
5th param=file descriptor of mapped file
6th param=offset from start of file where mapping starts
*/
file_memory = mmap (NULL, FILESIZE, PROT_WRITE, MAP_SHARED, fd, 0);
close (fd);
/* write a random integer to memory-mapped area */
char buf[256]={0};
char value[4];
for(i=0;i<size;i++){
sprintf(value,"%d ",random_range (-100, 100));
strcat(buf,value);
}
sprintf((char*) file_memory, "%s",buf);
/* release the memory */
munmap (file_memory, FILESIZE);
}
/****************************/mmap.h
/*(It has this line of code only)*/
#define FILESIZE 256
/*******************/compilation and execution
tan@tan-X540LA:~/tanmay/c$ gcc -o mmap-write mmap-write.c -std=c99
tan@tan-X540LA:~/tanmay/c$ gcc -o mmap-read mmap-read.c -std=c99
tan@tan-X540LA:~/tanmay/c$ ./mmap-write afile 10
tan@tan-X540LA:~/tanmay/c$ cat afile
52 -76 -61 60 -52 91 66 44 -76 3 tan@tan-X540./mmap-read afile 10
file contains: 52 -76 -61 60 -52 91 66 44 -76 3
tan@tan-X540LA:~/tanmay/c$ ./mmap-write afile 0
Invalid no of integers
tan@tan-X540LA:~/tanmay/c$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.