Project 1: Programming - Introduction to Image Processing and Analysis | CSE 591, Study Guides, Projects, Research of Computer Science

Material Type: Project; Class: Introduction to Image Processing and Analysis; Subject: Computer Science and Engineering; University: Arizona State University - Tempe; Term: Spring 2007;

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 09/02/2009

koofers-user-jpe
koofers-user-jpe 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE591 Programming Project 1
Revision 2 January 29, 2007
Programming project #1 is on mapped memory management. Five system calls are exercised.
1. mmap
2. munmap
3. msync
4. mremap
5. mprotect
The mmap system call can be used to map files into memory. After you have mapped the file
into the virtual address space of a task, it is easy to manipulate it using ordinary memory access
instructions. The munmap system call deletes the mappings for the specified address range, and
causes further references to addresses within the range to generate invalid memory references.
The msync system call flushes changes made to the in core copy of a file that was mapped into
memory using mmap back to disk. mremap expands or shrinks an existing memory mapping.
When using mremap, the old address has to be page aligned. mprotect controls how a section
of memory may be accessed. If an access is disallowed by the protection given it, the program
receives a SIGSEGV signal.
The stat system call returns information (stat structure) about the specified file. In this
assignment, stat is used to obtain the file length stored in the field st_size of the stat structure.
The Linux manual pages (man) provide useful information on all these system calls. You may
also need to check the other system calls needed in this project, such as open, read, write, close,
etc.
You also need to use several C library routines, such as malloc, memset, strlen, etc. malloc
allocates dynamic memory, specifying its size in bytes in its parameter. It returns a pointer to the
allocated memory. The memset function fills memory with a constant byte. strlen returns the
length of a string excluding the terminal ‘\0’. For more information about all these calls, please
check the Linux man pages.
There are three parts to this assignment. The steps of the program are enumerated and listed
below. Your program should include print statements printing out each of these enumerated
statements (or something similar to it) along with its number before you do the step. If the step
indicates that you should print out some value that resulted from the program’s execution, then
you should print that value out following the enumerated print statement (sometimes the value is
printed in the enumerated print statement).
(A) mmap, munmap, msync
The first part of this program creates and initializes a file, reads and prints out its contents, maps
it to a process’ virtual address space, prints out the memory address space contents, changes the
contents of the memory address region, prints out the new contents, synchronizes with the file
and reads and prints out the resulting file contents. The steps are (system calls are in italics and
library routines are in arial font):
(1) Use printf to print **** mmap test program ****
(2) Create or truncate a file named test.txt using an open or creat.
(3) Use write to write the string “abc abc abc abc abc abc” to the file.
(4) Use stat to get the file length.
(5) Use mmap to map the file to an arbitrary address.
pf3

Partial preview of the text

Download Project 1: Programming - Introduction to Image Processing and Analysis | CSE 591 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

CSE591 Programming Project 1

Revision 2 January 29, 2007 Programming project #1 is on mapped memory management. Five system calls are exercised.

**1. mmap

  1. munmap
  2. msync
  3. mremap
  4. mprotect** The mmap system call can be used to map files into memory. After you have mapped the file into the virtual address space of a task, it is easy to manipulate it using ordinary memory access instructions. The munmap system call deletes the mappings for the specified address range, and causes further references to addresses within the range to generate invalid memory references. The msync system call flushes changes made to the in core copy of a file that was mapped into memory using mmap back to disk. mremap expands or shrinks an existing memory mapping. When using mremap , the old address has to be page aligned. mprotect controls how a section of memory may be accessed. If an access is disallowed by the protection given it, the program receives a SIGSEGV signal. The stat system call returns information (stat structure) about the specified file. In this assignment, stat is used to obtain the file length stored in the field st_size of the stat structure. The Linux manual pages (man) provide useful information on all these system calls. You may also need to check the other system calls needed in this project, such as open, read, write, close , etc. You also need to use several C library routines, such as malloc, memset, strlen , etc. malloc allocates dynamic memory, specifying its size in bytes in its parameter. It returns a pointer to the allocated memory. The memset function fills memory with a constant byte. strlen returns the length of a string excluding the terminal ‘\0’. For more information about all these calls, please check the Linux man pages. There are three parts to this assignment. The steps of the program are enumerated and listed below. Your program should include print statements printing out each of these enumerated statements (or something similar to it) along with its number before you do the step. If the step indicates that you should print out some value that resulted from the program’s execution, then you should print that value out following the enumerated print statement (sometimes the value is printed in the enumerated print statement). (A) mmap, munmap, msync The first part of this program creates and initializes a file, reads and prints out its contents, maps it to a process’ virtual address space, prints out the memory address space contents, changes the contents of the memory address region, prints out the new contents, synchronizes with the file and reads and prints out the resulting file contents. The steps are (system calls are in italics and library routines are in arial font): (1) Use printf to print **** mmap test program **** (2) Create or truncate a file named test.txt using an open or creat. (3) Use write to write the string “abc abc abc abc abc abc” to the file. (4) Use stat to get the file length. (5) Use mmap to map the file to an arbitrary address.

(6) Use printf to print out the address the string was mapped to. (7) Use putchar (or another method) to print the contents of memory starting at the address returned by mmap. (8) Change the ‘a’s stored in memory to ‘A’s and use putchar to print the new contents. (9) Close the file. (10) Use msync to synchronize memory and the file. (11) Open the file (12) read the file into a buffer and use puts (or another method) to print out the buffer contents. (13) close the file and unmap the memory. (B) mremap The second part of this program allocates a region of memory, aligns the base of the region to a page boundary, fills it with characters and prints out its base address and size. Then it uses mremap to increase its size, fills the new region with characters and prints out the new base and size. The details are: (1) Use printf to print **** remap test program **** (2) Allocate a region of memory of 5 KB using malloc. (3) Fill the region using memset. (4) Print the original region starting address. (5) Print the original region length. (6) Establish a page-aligned memory region by defining a region that starts at the page boundary that the initial allocation crossed and ending where the initial allocation ended. (7) Print starting address or region after alignment. (8) Print length of region after alignment. (9) Calculate the size of the region starting at the page-aligned base using strlen and print it out. (10) Invoke mremap to expand the region by 1024 bytes. (11) Print out the base of the remapped region. (12) Fill the new region using memset. (13) Calculate the size of the remapped region using strlen and print it out. (C) mprotect The third part of this program allocates a region of memory, with default protection (read and write), writes to it and reads back what was written, changes protection to read only and attempts to read and write to it again causing a protection violation on the write. It also sets up a signal handler to catch a SIGSEGV signal and indicate that a protection violation occurred when writing. The details are: (1) Use printf to print **** mprotect test program **** (2) Allocate a region of memory using malloc. Note: It will get the default PROT_READ | PROT_WRITE protection. (3) Set up a signal handler to catch a SIGSEGV signal that prints out the warning message: “SIGSEGV: You are not allowed to access the protected memory!” (4) Write a character to the memory. (5) Read it back and print a message indicating that is being done. (6) Use mprotect to make the memory region read-only. (7) Read the protected memory and print out a message indicating that this was done. (8) Attempt to write the protected memory leading to a SIGSEGV signal handler message printout. Note: Insert system(“uname -a") and system(“users") at the beginning of your program.