Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Instruction Write a program to create a mmap() that is shared by multiple thread

ID: 3858454 • Letter: I

Question

Instruction

Write a 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 exercise, 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.

--------------------------------------------------------------------------

Source Code (help us fix if there are error, it RUNS, but does it satisfy the instructions' requirements):

#include <sys/mman.h> // for mmap()
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

int main (int argc, char * argv[])
{
char *addr;
int fd;
struct stat sb;

if (argc != 2 || strcmp(argv[1], "—help") == 0) {
printf("Usage: %s file ", argv[0]);
}

fd = open(argv[1], O_RDONLY);
if (fd == -1) {
printf("File open failed. ");
exit(EXIT_FAILURE);
}

/*
Obtain size of the file and use it to specify
the size of the buffer to be written.
*/

if (fstat(fd, &sb) == -1) {
printf("fstat error ");
exit(EXIT_FAILURE);
}

addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (addr == MAP_FAILED) {
printf("mmap failed ");
exit (EXIT_FAILURE);
}

if (write(STDOUT_FILENO, addr, sb.st_size) != sb.st_size) {
// Delete the mapping.
if (munmap(addr, sb.st_size) == -1){
printf("munmap failed ");
exit(EXIT_FAILURE);
}
// Close the file
close(fd);
}

exit(EXIT_SUCCESS);
}

---------------------------------------------------------------

Note: If you are planning to write this on paper, please write in printed English, prefer typed on computer.

Explanation / Answer

#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
int main ()
{
int segment_id;
char* memory[3];
int segment_size;
key_t shm_key;
int i=0;
const int shared_segment_size = 0x6400;
/* Allocate a shared memory segment. */
segment_id = shmget (shm_key, shared_segment_size,
IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
/* Attach the shared memory segment. */
memory[3] = (char*) shmat (segment_id, 0, 0);
printf ("shared memory attached at address %p ", memory);
/* Write a string to the shared memory segment. */
sprintf(memory[i], "maddy ");
sprintf(memory[i+1], "73453916 ");
sprintf(memory[i+2], "america ");

/*calling the other process*/
system("./process2");

/* Detach the shared memory segment. */
shmdt (memory);
/* Deallocate the shared memory segment.*/
shmctl (segment_id, IPC_RMID, 0);

return 0;
}
--------------------------

#include <stdio.h>
#include <sys/shm.h>
#include <sys/stat.h>
int main ()
{
int segment_id;
char* memory[3];
int segment_size;
int i=0;
key_t shm_key;
const int shared_segment_size = 0x6400;
/* Allocate a shared memory segment. */
segment_id = shmget (shm_key, shared_segment_size,
S_IRUSR | S_IWUSR);
/* Attach the shared memory segment. */
memory[3] = (char*) shmat (segment_id, 0, 0);
printf ("shared memory22 attached at address %p ", memory);
printf ("name=%s ", memory[i]);
printf ("%s ", memory[i+1]);
printf ("%s ", memory[i+2]);
/* Detach the shared memory segment. */
shmdt (memory);
return 0;
}