Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Does my C code work? Details: Your parallel sort ( psort ) will take two command - line arguments. The input file will consist of
Does my C code work?
Details: Your parallel sort psort will take two commandline arguments.
The input file will consist of records; within each record is a key. The key is the first four bytes of the record. The records are fixedsize, and are each bytes which includes the key
As part of testing of your program, you should generate your own test inputs of different size to cover various corner cases, and large input sizes.
A successful sort will read all the records into memory from the input file, sort them, and then write them out to the output file.
You also have to force writes to disk by calling fsync on the output file before finishing.
You can assume that this is a onepass sort, ie the data can fit into memory. You do not have to implement a multipass sort.
Your code should compile and should be compiled with the following flags: Wall Werror pthread O The last one is important: it turns on the optimizer! In fact, for fun, try timing your code with and without O and marvel at the difference.
Your code will first be measured for correctness, ensuring that it sorts input files correctly.
If you pass the correctness tests, your code will be tested for performance
Code:
#include
#include
#include
#include
#include
#include
#include
#include
#define RECORDSIZE
typedef struct
char dataRECORDSIZE;
Record;
typedef struct
Record records;
int start;
int end;
ThreadArg;
void mergeRecord records int start, int mid, int end
int n mid start ;
int n end mid;
Record left Record mallocn sizeofRecord;
Record right Record mallocn sizeofRecord;
for int i ; i n; i
lefti recordsstart i;
for int i ; i n; i
righti recordsmid i;
int i j k start;
while i n && j n
if memcmpleftidata, rightjdata,
recordsk lefti;
else
recordsk rightj;
while i n
recordsk lefti;
while j n
recordsk rightj;
freeleft;
freeright;
void threadsortvoid arg
ThreadArg threadArg ThreadArg arg;
Record records threadArgrecords;
int start threadArgstart;
int end threadArgend;
if start end
int mid start end start;
ThreadArg argrecords start, mid;
ThreadArg argrecords mid end;
pthreadt tid tid;
pthreadcreate&tid NULL, threadsort, &arg;
pthreadcreate&tid NULL, threadsort, &arg;
pthreadjointid NULL;
pthreadjointid NULL;
mergerecords start, mid, end;
return NULL;
int mainint argc, char argv
if argc
fprintfstderr "Usage: s
argv;
exitEXITFAILURE;
const char inputfile argv;
const char outputfile argv;
int fd openinputfile, ORDONLY;
if fd
perroropen input file";
exitEXITFAILURE;
struct stat sb;
if fstatfd &sb
perrorfstat;
exitEXITFAILURE;
sizet filesize sbstsize;
sizet numrecords filesize RECORDSIZE;
Record records mmapNULL filesize, PROTREAD PROTWRITE, MAPPRIVATE, fd;
if records MAPFAILED
perrormmap;
exitEXITFAILURE;
closefd;
ThreadArg arg records numrecords ;
pthreadt tid;
pthreadcreate&tid, NULL, threadsort, &arg;
pthreadjointid NULL;
int ofd openoutputfile, OWRONLY OCREAT OTRUNC, SIRUSR SIWUSR;
if ofd
perroropen output file";
exitEXITFAILURE;
if writeofd records, filesize filesize
perrorwrite;
exitEXITFAILURE;
if fsyncofd
perrorfsync;
exitEXITFAILURE;
closeofd;
munmaprecords filesize;
return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started