Programming Language: C Programming Language: C Follow the instruction. The code
ID: 3856866 • Letter: P
Question
Programming Language: C
Programming Language: C
Follow the instruction. The code runs but not sure if it functions as the instruction intended, help us out on that.
Additionally, what commands do we need to enter in terminal? Do we need extra files? ./test filename is the command I use, and it seems to print out the file itself, not sure if it's right. Please use a terminal (PuTTY recommended) to help us figure this out.
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);
}
Explanation / Answer
It has some errors which i fixed it. Please find the program below. yes, it satisfies the requirement.
#include <sys/mman.h> // for mmap()
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.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 = (char *)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);
}
Please find the output file:
This program needs a text file as input.
sdt05082:~ # cat h.txt
hello
hello
sdt05082:~ # gcc hello.c
sdt05082:~ # ./a.out h.txt
hello
hello
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.