Lab 09 - Virtual Memory

Similar documents
ECE 650 Systems Programming & Engineering. Spring 2018

Applications of. Virtual Memory in. OS Design

Memory management. Single process. Multiple processes. How to: All memory assigned to the process Addresses defined at compile time

Memory Mapped I/O. Michael Jantz. Prasad Kulkarni. EECS 678 Memory Mapped I/O Lab 1

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

ECEN 449 Microprocessor System Design. Review of C Programming

Shared Memory Memory mapped files

POSIX Shared Memory. Linux/UNIX IPC Programming. Outline. Michael Kerrisk, man7.org c 2017 November 2017

Introduction to Linux, for Embedded Engineers Tutorial on Virtual Memory. Feb. 22, 2007 Tetsuyuki Kobayashi Aplix Corporation. [translated by ikoma]

CSE 333 SECTION 3. POSIX I/O Functions

Virtual Memory: Systems

Signal Example 1. Signal Example 2

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Summer 2016 Programming Assignment 1 Introduction The purpose of this

MMAP AND PIPE. UNIX Programming 2015 Fall by Euiseong Seo

Machine Problem 1: A Simple Memory Allocator

Programmation Système Cours 5 Memory Mapping

Operating systems. Lecture 9

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017)

CSCI 4061: Virtual Memory

Process Address Spaces and Binary Formats

Process Address Spaces and Binary Formats

Lab 4. Out: Friday, February 25th, 2005

Programming Assignment Multi-Threading and Debugging 2

CS 322 Operating Systems Programming Assignment 4 Writing a memory manager Due: November 16, 11:30 PM

File Descriptors and Piping

Outline. 1 Details of paging. 2 The user-level perspective. 3 Case study: 4.4 BSD 1 / 19

Princeton University. Computer Science 217: Introduction to Programming Systems. Dynamic Memory Management

The course that gives CMU its Zip! I/O Nov 15, 2001

OPERATING SYSTEMS: Lesson 2: Operating System Services

File Systems: Consistency Issues

Files. Eric McCreath

CSE 333 SECTION 3. POSIX I/O Functions

Windows architecture. user. mode. Env. subsystems. Executive. Device drivers Kernel. kernel. mode HAL. Hardware. Process B. Process C.

Chapter 4. File Systems. Part 1

CMSC 412 Project #4 Virtual Memory Due Friday April 11, 2014, at 5:00pm

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

1. Overview This project will help you understand address spaces and virtual memory management.

Project 2 Overview: Part A: User space memory allocation

CSCE 548 Building Secure Software Dirty COW Race Condition Attack

Princeton University Computer Science 217: Introduction to Programming Systems. Dynamic Memory Management

Processes COMPSCI 386

Project #1 Exceptions and Simple System Calls

CSC209 Review. Yeah! We made it!

Processes and Threads

Lab 03 - x86-64: atoi

RCU. ò Walk through two system calls in some detail. ò Open and read. ò Too much code to cover all FS system calls. ò 3 Cases for a dentry:

VFS, Continued. Don Porter CSE 506

OS COMPONENTS OVERVIEW OF UNIX FILE I/O. CS124 Operating Systems Fall , Lecture 2

Memory management. Johan Montelius KTH

CS 322 Operating Systems Programming Assignment 4 Writing a memory manager Due: April 5, 11:30 PM

CMPSC 311 Exam 2. March 27, 2015

Process Management! Goals of this Lecture!

Basic OS Progamming Abstrac7ons

W4118: OS Overview. Junfeng Yang

CS 3013 Operating Systems WPI, A Term Assigned: Friday, August 31, 2007 Due: Monday, September 17, 2007

Operating Systems 2010/2011

Basic OS Progamming Abstrac2ons

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 23

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

Design Overview of the FreeBSD Kernel CIS 657

Design Overview of the FreeBSD Kernel. Organization of the Kernel. What Code is Machine Independent?

ENGR 3950U / CSCI 3020U (Operating Systems) Simulated UNIX File System Project Instructor: Dr. Kamran Sartipi

Section 2: Processes

경희대학교컴퓨터공학과 조진성. UNIX System Programming

Creating a Shell or Command Interperter Program CSCI411 Lab

Introduction. This project will focus primarily on processes.

Process Management 1

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 23

Introduction to File Systems. CSE 120 Winter 2001

PROJECT 2 - MEMORY ALLOCATOR Computer Systems Principles. October 1, 2010

CSCI0330 Intro Computer Systems Doeppner. Project Shell 2. Due: November 8, 2017 at 11:59pm. 1 Introduction 2

CS 1550 Project 3: File Systems Directories Due: Sunday, July 22, 2012, 11:59pm Completed Due: Sunday, July 29, 2012, 11:59pm

fork System-Level Function

Process Management! Goals of this Lecture!

Recitation 8: Tshlab + VM

Pebbles Kernel Specification September 26, 2004

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

User Programs. Computer Systems Laboratory Sungkyunkwan University

Software Vulnerabilities. Jeff Foster University of Maryland, College Park

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

CS354/CS350 Operating Systems Winter 2004

Computer Science 330 Operating Systems Siena College Spring Lab 5: Unix Systems Programming Due: 4:00 PM, Wednesday, February 29, 2012

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CSCI0330 Intro Computer Systems Doeppner. Maze. Due: Wednesday, September 20, 2017 at 11:59pm. 1 Introduction 1. 2 Assignment 1.

Virtual Memory: Systems

PRINCIPLES OF OPERATING SYSTEMS

Operating Systems (234123) Spring 2017 (Homework Wet 1) Homework 1 Wet

Programming Assignment #4

OS Lab Tutorial 1. Spawning processes Shared memory

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

CSC 1600 Unix Processes. Goals of This Lecture

CS 5460/6460 Operating Systems

Interrupts, Fork, I/O Basics

Processes often need to communicate. CSCB09: Software Tools and Systems Programming. Solution: Pipes. Recall: I/O mechanisms in C

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Section 3: File I/O, JSON, Generics. Meghan Cowan

Foundations of Computer Systems

Operating System Design

Project 2--User Programs

Transcription:

Lab 09 - Virtual Memory Due: November 19, 2017 at 4:00pm 1 mmapcopy 1 1.1 Introduction 1 1.1.1 A door predicament 1 1.1.2 Concepts and Functions 2 1.2 Assignment 3 1.2.1 mmap copy 3 1.2.2 Tips 3 1.2.3 Testing 4 1.2.4 Debugging 4 2 Fractals 4 2.1 Introduction 4 2.2 Assignment 4 2.2.1 The Starting Line 4 2.2.2 Running the Program 5 2.2.3 Performance 6 2.2.4 Shared Memory Using fork and mmap 6 3 Getting Checked Off 7 Note : This lab s stencil code comes with two directories. The mmapcopy directory will be used for the first portion of the lab and has its own Makefile. The second portion of the lab (after the checkpoint) will be done in the fractal directory. 1 mmapcopy 1.1 Introduction 1.1.1 A door predicament Mike and Sulley are trying to reassemble Boo s door in order to see her again. However, Mike has a pedicure appointment and they must finish as quickly as possible! Luckily, there are many machines that

can reassemble shredded doors, but unluckily they re very slow. A naive solution is to only use one machine and slowly build the door, while a better solution would be to utilize many machines to re-assemble different parts of the door at once. Your task is to use virtual memory and concurrency to expedite the door building machines. 1.1.2 Concepts and Functions Before virtual memory, programmers would have to manually manage the amount of physical memory their processes had access to. This meant one often had to be very careful with loading too many variables or program text into memory at any one time. Virtual memory solves these problems by abstracting away the management of physical memory for the programmer. It provides a full 32-bit (or 64-bit) virtual address space to every process. This means that each application can assume that it has the exclusive use a large amount of physical memory, while the operating system provides it with whatever memory it actually needs, and handles the translation between virtual and physical addresses during a memory access. Virtual memory allows programmers to worry less about memory management, and more about writing effective code. But virtual memory can be used by the programmer to improve I/O operations as well, skipping the kernel s buffer cache. Consider the following pseudocode: char buf[bigenough]; fd = open(file, O_RDWR); for(i = 0; i < num_records; i++) { read(fd, buf, sizeof(buf)); use(buf); } In this bit of code, we successively read and then use portions of a file, in chunks of size BigEnough. The OS goes to disk with every read call, and uses the I/O buffer to put the file into an OS specific section of memory. Then, the file is read from the OS s section of memory into the user program s virtual memory. But using a virtual memory mapping can reduce the number of buffers used in this process: the operating system instead maps data needed from external storage to a program s address space using virtual memory. The mmap system call is used to invoke this process: void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) mmap() creates a new mapping of size length bytes and returns a pointer to the location of that new mapping, which is created at a virtual page boundary near addr, if addr is not NULL. If addr is NULL, then the pointer returned will be chosen by the OS (this is the most portable behavior and is often preferred). The new mapping contains the contents of the open file given by the file descriptor fd beginning from byte offset of that file. prot and flags are bit vectors which determine characteristics of the mapping. In particular, prot determines memory protection:

PROT_READ indicates that the mapping may be read from. PROT_WRITE indicates that the mapping may be written to. PROT_EXEC indicates that the mapping may be executed. PROT_NONE indicates that the mapping may not be accessed. flags determines behavior of the mapping. For example: MAP_SHARED indicates that updates to the mapping should be visible to other processes that map the same file. In addition, writes to the mapping will write back to that file. MAP_PRIVATE indicates that updates to the mapping should not be visible to other processes that map the same file, and that changes made in the mapping should not be written back to the file. MAP_ANONYMOUS indicates that the new mapping should not be backed by any file, causing the fd and offset arguments to be ignored. Instead the new mapping points to an anonymous file whose bytes are initialized to zero. 1.2 Assignment 1.2.1 mmap copy Your first assignment is to code a program to copy files using mmap() instead of read() and write(). The main() routine in the provided stencil mmapcopy.c explains the steps for doing this. Note: Don t forget to clean up and delete a mapping once it is no longer needed. You can use the system call munmap() to do this. 1.2.2 Tips mmap creates a new mapping in the virtual address space of the calling process. By mapping the file descriptors of the corresponding source and destination files, you can use memcpy to copy from one virtual address to the other. View man pages for more information. lseek is an useful system call to reposition the offset of an open file descriptor. You can use it to determine the size of a file. munmap system call deletes the mappings for the specified address. Make sure to call this on any virtual address you called before your program returns. ftruncate system call forces a given file to be of a specific length. Use this system call to ensure that the length of the destination file is the exact same size as the source file. You should look at slide XXVIII-42 if you get stuck during this portion of the lab. Warning : If you find yourself getting a bus error try using a for-loop to copy data 1 word (8 bytes) at a time instead of using memcpy. This is related to a bug in the Linux kernel.

1.2.3 Testing To test your code, run the command./mmapcopy <path to source file> <path to new file>. You can use the provided image cit.jpg to ensure your program is working. The executable cs0330_file_equals checks if two files (passed as its arguments) are equal. 1.2.4 Debugging Remember to surround system calls with error checking- this will help you find bugs when doing the lab!. 2 Fractals 2.1 Introduction Previously in your computer science career, you have written primarily sequential programs: programs that execute instructions one-by-one in a specified order. You have also written programs that perform distinct tasks simultaneously using fork(). The first of these programming paradigms does not permit parallel execution of distinct tasks, but has no problems with memory access because all program execution comes from a single thread; the second of these paradigms does allow simultaneous execution of tasks, but does not enable memory to be shared between different threads of execution. But with concurrent programming techniques, a program can both execute distinct tasks in parallel as well as share memory between those threads of execution. In this part of the lab, you will use fork() and mmap() to generate fractals that will be used as the pattern on Boo s door so that Mike and Sully don t lose track of it. 2.2 Assignment 2.2.1 The Starting Line We have provided a program that generates a fractal called the Mandelbrot set using a single thread of execution. You will need to edit this program to perform the computation concurrently. The program you will be working with consists of code from the following files: image.h - the image.h file contains struct definitions and method headers used to generate fractal data. color_t This struct is used to represent the color of each pixel in the fractal image. Image colors are typically represented in a three-dimensional color space, where the

dimensions represent the amounts of red, green, and blue light that produce the color. A color_t represents those coordinates as unsigned char values, which range from 0 to 255. int save_image_data(char *file_name, color_t *image_data, int width, int height) This function saves the color data stored in the image_data array into the file_name. void generate_fractal_region(color_t *image_data, float width, float height, float start_y, unsigned int rows) This function generates color data for a particular subset of the pixels of the fractal and stores that data in the image_data array. Out of height rows of pixels, this function generates data for a number of rows given by the value of rows, beginning with row start_y. image.c - the image.c file contains definitions for the functions declared in image.h. You should not need to be familiar with the contents of this file to complete this lab. colors.h - the colors.h file contains declarations of three functions that generate a color channel value (an integer between 0 and 255). These functions are used in image.c to generate color data for each pixel of the fractal. colors.c - the colors.c file contains definitions of the color generation functions declared in colors.h. The colors of the fractal generated by the program depend on these functions - change them to change program output. fractal.h - the fractal.h file contains a declaration for the generate_fractal() function. fractal.c - the fractal.c file contains the definition for the generate_fractal() function, generating the fractal using a single line of execution. main.c - the main.c file contains the program main() function. This function parses the argv array for program arguments and calls generate_fractal(), the function you will be filling in (see below). You are additionally provided with the file fractal_forked.c which provides an empty definition for generate_fractal(). 2.2.2 Running the Program After you ve compiled the program by running make, you can run the executable binary fractal with the following options: -f <file> and --file <file> change the file in which the fractal is saved. -d <width> <height> or --dimensions <width> <height> set the dimensions of the output image.

By default, fractal will store a 2000 2000 image in the file out.png. View this image using your favorite image viewer or editor; if you don t have one, run okular out.png to view the image. 2.2.3 Performance In its present state, the generate fractal() function of fractal.c does not utilize any form of concurrent execution. If you make the program and run time./fractal, you ll notice that it takes a few seconds to execute. Performance of this program can be improved by distributing the task of fractal data generation amongst several subprocesses. Your task for this lab is to re-implement the generate_fractal() method of the fractals program in the files fractal_forked.c using concurrent programming techniques to improve program performance. 2.2.4 Shared Memory Using fork and mmap pid_t fork() void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) int munmap(void *addr, size_t length) int waitpid(pid_t pid, int *status, int options) As you ve seen previously, fork() is a system call which duplicates the calling process; the resulting child process receives a copy of its parent process memory and stack, and begins execution from the point where fork() returns. Because memory is not ordinarily shared between the parent and child processes, filling in part of the color_t array in each child process will uselessly generate an incomplete image in each child process. To successfully generate the desired image, some mechanism by which the processes can share memory is needed. Virtual memory provides such a mechanism with the system call mmap(). As you experienced above, this system call creates a new mapping in a process virtual address space - that is, it creates a new region of memory for use by the process. You will find that a combination of the flags described above will set up a convenient region of shared memory between processes; this is exactly what is needed here. It doesn t matter where the mapping is located, so addr can be NULL. Note : mmap() returns (void *) -1 if an error occurs. Once you ve set up the shared memory region and fork() ed off each child process, the parent must wait until each child has completed its task before it saves the fractal data. To do this, you ll use the waitpid() system call to wait for each child process. To do so, you ll need to save the process ID of each child process.

Once the memory mapping is no longer needed, make sure to clean up and delete the mapping. Do this after you ve saved the fractal data. You should also do this in each child process before exiting cleaning up before exiting is always good practice. Task: use fork(), mmap(), and munmap() to edit fractal_forked.c so that work done to generate the fractal is divided amongst several child processes. This functionality should be abstracted to an arbitrary number of child processes; use the workers argument to generate fractal(). By default, this argument is 4, but this value can be changed by passing the flag -n <workers> or --workers <workers> as an argument to the fractals program. You need not handle the case where the height of the generated image is not divisible by the number of child processes. You can build the fractals program with the command make forked, which includes fractal_forked.c instead of fractal.c. Doing so creates the executable fractal_forked. 3 Getting Checked Off Try to get the runtime of fractal_forked to be under 1.4 seconds. Once you ve completed the lab, run 33lab_checkoff lab09 to submit and check off your lab. You may run this command as many times as you like; we will use only the most recent grade and handin time recorded by this program. Remember to read the course missive for information about course requirements and policies regarding labs and assignments.