CS 322 Operating Systems Programming Assignment 2 Using malloc and free Due: February 15, 11:30 PM

Similar documents
C mini reference. 5 Binary numbers 12

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

COMP 321: Introduction to Computer Systems

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

Project 2 Overview: Part A: User space memory allocation

C for C++ Programmers

Standard File Pointers

CS143 Handout 05 Summer 2011 June 22, 2011 Programming Project 1: Lexical Analysis

Programming Assignment 3 (PA3) - Popular Names

Programming Assignment 4 (PA4) - myxd Milestone Due: Final Due:

EE355 Lab 5 - The Files Are *In* the Computer

File IO and command line input CSE 2451

CSCI 4963/6963 Large-Scale Programming and Testing Homework 1 (document version 1.0) Regular Expressions and Pattern Matching in C

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

cc -o build/test_translate build/test_translate.o b... $

COP 3223 Final Review

ECE551 Midterm Version 1

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Homework #1 From C to Binary

Project 4: Implementing Malloc Introduction & Problem statement

Getting Started. Project 1

EECE.2160: ECE Application Programming

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Homework 3 (document version 1.2) Multi-threading in C using Pthreads

Project #1: Tracing, System Calls, and Processes

CSCi 4061: Intro to Operating Systems Spring 2017 Instructor: Jon Weissman Assignment 1: Simple Make Due: Feb. 15, 11:55 pm

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Exercise 1.1 Hello world

Dynamic Allocation in C

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

CS 11 C track: lecture 5

CS 470 Operating Systems Spring 2013 Shell Project

Programming Tips for CS758/858

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

CS 610: Intermediate Programming: C/C++ Making Programs General An Introduction to Linked Lists

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Assignment 5: MyString COP3330 Fall 2017

Creating a String Data Type in C

COP 3223 Final Review

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Here's how you declare a function that returns a pointer to a character:

Homework #1 From C to Binary

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

1. Introduction. 2. Deliverables

Intermediate Programming, Spring 2017*

Dynamic Allocation in C

Programming Assignment Multi-Threading and Debugging 2

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Programs in memory. The layout of memory is roughly:

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

Recitation 2/18/2012

ECE551 Midterm Version 1

CS201: Lab #4 Writing a Dynamic Storage Allocator

Intermediate Programming, Spring 2017*

Project 3a: Malloc and Free

Programming Assignment #2

Contents. A Review of C language. Visual C Visual C++ 6.0

Recitation: C Review. TA s 20 Feb 2017

CSE 303 Midterm Exam

CS 261 Fall Mike Lam, Professor. Structs and I/O

C Libraries. Bart Childs Complementary to the text(s)

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

CS2 Practical 1 CS2A 22/09/2004

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

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.

ECE551 Midterm. There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly.

My malloc: mylloc and mhysa. Johan Montelius HT2016

C programming basics T3-1 -

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Recitation: Cache Lab & C

211: Computer Architecture Summer 2016

EECE.2160: ECE Application Programming Spring 2018 Programming Assignment #6: Using Arrays to Count Letters in Text Due Wednesday, 4/4/18, 11:59:59 PM

CS 103 Lab The Files are *In* the Computer

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Assignment 5. CS/ECE 354 Spring 2016 DUE: April 22nd (Friday) at 9 am

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Input / Output Functions

ECE 551D Spring 2018 Midterm Exam

CSC209 Review. Yeah! We made it!

Carnegie Mellon. C Boot Camp. Oct 6th, Jack Biggs Raghav Goyal Nikhil Jog

Project 2: Shell with History1

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

CS 107 Introduction to Computing and Programming Fall 2013 Homework Assignment 7 - First Draft Atomic Weights of Compounds with Pointers to Structures

ECE551 Midterm Version 2

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

o Code, executable, and process o Main memory vs. virtual memory

Assignment 1: Lexer and tokenout utility

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Programming Assignment 1 (PA1) - Display Bowtie

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

8. Characters, Strings and Files

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

Printable View of: Week 13: Miscelaneous cool features. Returns from standard functions. returns from standard functions: scanf(), fopen()

Common Misunderstandings from Exam 1 Material

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #47. File Handling

CMSC 412 Project 1: Keyboard and Screen Drivers

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

CSE 333 Midterm Exam Sample Solution 5/10/13

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

Transcription:

CS 322 Operating Systems Programming Assignment 2 Using malloc and free Due: February 15, 11:30 PM Goals To get more experience programming in C To learn how to use malloc and free To lean how to use valgrind to find memory problems To learn how to call functions from the C library Assignment Unix provides a lot of utility programs. One of these is sort. Run man sort to see what it does and all the options available. In this assignment, you will write a much simpler version of Unix s sort utility. Your version will sort lines of text. This program should be invoked as follows: or./textsort file./textsort -3 file textsort is the name of the executable program. In the first line, "file" is the name of the file that contains the text being sorted. The input file file should be sorted and the sorted output printed to the screen; it is assumed that the file is a text file (full of ASCII characters). When only the filename is given, the sorting should be over the first word in each line. If the optional argument is included ( -3 in the example on the second line), the program should sort the text input file using the specified word as the key to sort upon (with -3, the program should find the 3rd word in each line and sort the lines based upon that). It is normal Unix convention to start optional arguments with a -, so here -3 does not mean negative 3, but rather using the optional argument 3. Examples Let's say you have the following file, called short_alma.txt: If you run./textsort short_alma.txt, it should print:!1

because In is alphabetically before Oh, which is before Our, which is before The. If, however, you pass in a flag to sort a different word, you'll get a different output. For example, if you call./textsort -2 short_alma.txt, you should get: In ASCII, uppercase letters appear before lowercase letters and therefore they will get sorted first. As a result, we see Mount before courage before lives before the. Yes, we are assuming -2 means the second word in each line (like most people would, except computer scientists who always want to start at 0). Hints on Sorting You can use C s qsort function to sort an array. Here is a excerpt from the man page: void qsort (void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *)); The qsort() function sorts an array with nmemb elements of size size. The base argument points to the start of the array. The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. If two members compare as equal, their order in the sorted array is undefined. Here are some tips about using qsort. The last parameter to qsort is a function. To pass a function as a parameter in C, just pass in the name of the function. In this case, the function you pass in will be called by qsort to do the pairwise comparison of values in the array you are sorting. The signature of this function should be: int my_compare (const void *, const void *) You can call the function whatever you want; it does not need to be my_compare. The trickier part is understanding what the parameters to your comparison function are. The man page for qsort says: the comparison function "is called with two arguments that point to the objects being compared". That is, they are pointers to the array elements. Therefore, if you are calling qsort with an array of strings, then the elements in your array have type char *. The parameters to the comparison function are pointers to array elements, so that type would be char **. To use the parameters, you need to do two things: 1. Cast the void * to its actual type of char ** 2. Dereference the first pointer so that you have a char *. Then you will be able to just do the comparison. For example: int my_compare (const void *elem1, const void *elem2) {!2

/* Cast to its actual type. */ char **strptr1 = (char **) elem1; char **strptr2 = (char **) elem2; /* Dereference to get the strings */ char *str1 = *strptr1; char *str2 = *strptr2; } /* Then use strcmp to compare the strings */... The same principle applies if the array you are sorting contains something other than char *. The compare function will get a pointer to whatever element type you have and you will need to dereference the pointer to get the actual value to compare. To give yourself confidence that you understand how to call qsort, I recommend that you write a very simple test program in which you define a comparison function. In the main function of this test program, call qsort and print out the sorted array. Other Hints You will be using a number of other functions provided by the C libraries. You need to use the man command to find out how to call these functions and what.h files you need to include. Here, I identify functions you will likely want to use. In your sorting program, you should use fopen() to open the input file, fgets() to read each line of the file, and fclose() when you are done with the input file. If you want to figure out how big the input file is before reading it in, use the stat() function. This will not give you an exact answer but you will be able to make a better guess. To compare strings, use the strcmp() function. To chop lines into words, you can use strtok(). Be careful, though; it is destructive and will change the contents of the lines. Thus, if you use it, make sure to make a copy of the original line for later use. The routine strtol() can be used to transform a string into an integer. To exit, call exit() with a single argument. This argument to exit() is then available to the user to see if the program returned an error (that is, return 1 by calling exit(1) ) or exited cleanly (that is, returned 0 by calling exit(0) ). You can also just return from the main() function and pass the return code that way where appropriate. The routine malloc() is used for memory allocation. If you have a data structure that you need to make bigger, use realloc() to increase the amount of memory for that data structure. You can use calloc() to allocate an array dynamically. Use free() to free memory when it is no longer needed. Your program should not have any memory leaks; all the memory allocated by malloc should be freed before your program ends. Check the return calls of any library function that you use. For many functions, the return value indicates whether the function succeeded or encountered an error.!3

Assumptions and Errors The return code upon success is zero. When the program runs normally and no errors are encountered, you should return 0. Only space characters (that is, what you get when you hit spacebar) will be used to separate words in the input. Thus, you don't have to worry about tabs or other whitespace. However, your program should correctly handle the case where there are two or more spaces between words, that is, it should treat that as one big separator between the words. Max line length will be 128. If you get a line longer than this (detected by the lack of a newline character in the last position), please print Line too long to standard error and exit with return code 1. You should check the arguments of textsort carefully. If more than two arguments are passed, or two are passed but the second does not fit the format of a dash followed by a number, you should EXACTLY print (to standard error): Error: Bad command line parameters and exit with return code 1. Key does not exist on one line of input file: If the specified key does not exist on a particular line of the input file, you should just use the last word of that line as the key. For example, if the user wants to sort on the 4th word ( -4 ), and the sort encounters a line like this ( sample line ), the sort should use the word line to sort that line. Empty line: You should use an empty string to sort any empty lines (that is, lines that are just a newline or spaces and a newline character). File length: May be pretty long! However, you can assume the data set will fit into memory and you shouldn't have to do anything special to handle this. However, if malloc() does fail, please print malloc failed to standard error and exit with code 1. Invalid files: If the user specifies an input file that you cannot open (for whatever reason), the sort should EXACTLY print (to standard error): Error: Cannot open file foo with no extra spaces (if the file was named foo ) and then exit with return code 1. Important: On any error code, you should print the error to the screen using fprintf(), and send the error message to stderr (standard error) and not stdout (standard output). This is accomplished in your C code as follows: fprintf(stderr, whatever the error message is\n ); General Advice Start small, and get things working incrementally. For example, first get a program that simply reads in the input file, one line at a time, and prints out what it reads in. Then, slowly add features and test them as you go. Testing is critical. Testing your code to make sure it works is crucial. Write tests to see if your code handles all the cases you think it should. Be as comprehensive as you can be. Save copies of your code as you get things working. Use valgrind to help you find memory problems. valgrind is a program that will help you find memory errors in your C programs. You need to install it in your Linux VM:!4

sudo apt install valgrind To run valgrind, use a command like the following, where file1.txt is the name of the file being read by textsort. You can also optionally pass in the number used to indicate which word to sort by, just as you would if you were calling textsort from the command line. valgrind --leak-check=yes./textsort file.txt Teams You will work in pairs on this assignment. Here are the teams: Ammal Abbasi, Mimi Shahzad Eeman Abbasi, Crystal Seo Sabrina Accime, Ana Saverchenko Shanzeh Agrawala, Sophie Manum Momal Baloch, Alice Richardson Ranjini Das, Xiaolei Ni Anne Demosthene, Sophie Le Darienne Dewalt, Vivian Le Zineb El Mechrafi, Rebecca Kim Hiwete Fetene, Hyeji Kim Emma Grotto, Miral Khalil Zhiling Hu, Van Trinh Tracy Keya, Jenny Lee Sujin Kim, Zoe Liang Sara Rutkowski, Jane Yoo Grading Code that is turned in that does not compile will be returned ungraded. Grading will be based on correctness, documentation, and coding style in these proportions: 5% Makefile 20% Sorts correctly on a short text file 10% Sorts correctly on a long text file 10% Sorting by other than the first word 5% Word to sort by > number of words on some lines 10% Error checking 20% Correct memory management 10% Comments 10% Coding style Turning in your solution Place your C file(s) and makefile into a single tar.gz file, using your name and your partner s name rather than mine in the name of the file. (First names are enough.) tar -cvzf Barbara_Lerner_Assign1.tar.gz *.c makefile Upload your tar.gz file to Moodle. Only one of you should submit this.!5