You will write a simple C program (location: task-5/solution-5.c) which sorts given binary objects. This program will be compiled and invoked in the following way:

$ gcc -Werror -Wall -o fastsort solution-5.c # Compilation
$ ./fastsort inputfile outputfile # Execution

The built executable (i.e., fastsort) takes two command line arguments: an input file (named inputfile) containing the binary objects to be sorted and an output file (named outputfile) to store the sorted results.

Input: The inputfile contains binary data1 that needs to be sorted. The binary data consists of a series of 100-byte binary objects (aka blobs). Each object is a key-value pair, where the first four bytes represent an unsigned integer key and the remaining 96 bytes is the corresponding value. Each value (i.e., the remaining 96 bytes) actually represents a record consisting of 24 unsigned integers. Therefore, each binary object looks something as follows (where each letter represents 4 bytes):

kRRRRRRRRRRRRRRRRRRRRRRRR

How to generate such input files?: Input files are generated by a given program called task-5/generate.c, which can be complied as follows:

$ gcc -Wall -Werror -o generate generate.c
$ ./generate -h
usage: ./generate [-s random_seed] [-n number_of_objects] [-o generated_filename]

The generate program takes three optional arguments:

-s: This option specifies a random seed that this program will use as the seed for a new sequence of (pseudo-)random integers. This allows you to generate input files with different data while testing your fastsort.

-n: This option specifies how many binary objects to write to the generated filename file. Each object is of 100 bytes.

-o: This options specifies the filename where the number of objects binary objects will be stored.

A sample execution of generate is shown below. Note that the generated file (test.in) can be used as the input file to your fastsort.

$ ./generate -s 0 -n 100 -o test.in

Output: Your sorting program (named fastsort) must take in one of these generated files and sorts it based on the 4-byte key (the remainder of the object (i.e., the record) should be kept with the same key). The output is written to the specified output file. While writing the output, you should only write the sorted binary objects, no additional characters (e.g., '\n).

NOTE: All these programs related to this task need the header file sort.h during their compilation. This header is located at task-5/sort.h.

Assumptions:

  • 32-bit integer: You may assume all the integers are 32-bit. In addition, the keys are unsigned 32-bit integers.
  • File length: May be pretty long! However, there is no need to implement a fancy two-pass sort or anything like that; the data from a file will fit into memory.

Error checking: Your program should do appropriate error checking.

  • Invalid files: If the user specifies an input or output file that your program cannot open (for whatever reason), your program should EXACTLY print: "Error: Cannot open file %s\n", where %s refers to the filename and then exit with exit(1).
  • Too few or many arguments passed to program: If the user runs your program without any arguments, or with few/many arguments, or in some other way passes incorrect options, then you must print the appropriate usage string of fastsort and exit with exit(1).
  • For all other errors, your program must print the following error message: "An error has occurred\n" and then exit with the error code 1, e.g., using exit(1).
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.