Answered step by step
Verified Expert Solution
Link Copied!

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 command-line 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 fixed-size, and are each 100 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 one-pass sort, i.e., the data can fit into memory. You do not have to implement a multi-pass 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 RECORD_SIZE 100
typedef struct {
char data[RECORD_SIZE];
} Record;
typedef struct {
Record *records;
int start;
int end;
} ThreadArg;
void merge(Record *records, int start, int mid, int end){
int n1= mid - start +1;
int n2= end - mid;
Record *left =(Record *)malloc(n1* sizeof(Record));
Record *right =(Record *)malloc(n2* sizeof(Record));
for (int i =0; i < n1; i++)
left[i]= records[start + i];
for (int i =0; i < n2; i++)
right[i]= records[mid +1+ i];
int i =0, j =0, k = start;
while (i < n1 && j < n2){
if (memcmp(left[i].data, right[j].data, 4)<=0){
records[k++]= left[i++];
} else {
records[k++]= right[j++];
}
}
while (i < n1){
records[k++]= left[i++];
}
while (j < n2){
records[k++]= right[j++];
}
free(left);
free(right);
}
void *thread_sort(void *arg){
ThreadArg *threadArg =(ThreadArg *)arg;
Record *records = threadArg->records;
int start = threadArg->start;
int end = threadArg->end;
if (start < end){
int mid = start +(end - start)/2;
ThreadArg arg1={records, start, mid};
ThreadArg arg2={records, mid +1, end};
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, thread_sort, &arg1);
pthread_create(&tid2, NULL, thread_sort, &arg2);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
merge(records, start, mid, end);
}
return NULL;
}
int main(int argc, char *argv[]){
if (argc !=3){
fprintf(stderr, "Usage: %s
", argv[0]);
exit(EXIT_FAILURE);
}
const char *input_file = argv[1];
const char *output_file = argv[2];
int fd = open(input_file, O_RDONLY);
if (fd <0){
perror("open input file");
exit(EXIT_FAILURE);
}
struct stat sb;
if (fstat(fd, &sb)<0){
perror("fstat");
exit(EXIT_FAILURE);
}
size_t file_size = sb.st_size;
size_t num_records = file_size / RECORD_SIZE;
Record *records = mmap(NULL, file_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,0);
if (records == MAP_FAILED){
perror("mmap");
exit(EXIT_FAILURE);
}
close(fd);
ThreadArg arg ={records,0, num_records -1};
pthread_t tid;
pthread_create(&tid, NULL, thread_sort, &arg);
pthread_join(tid, NULL);
int ofd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (ofd <0){
perror("open output file");
exit(EXIT_FAILURE);
}
if (write(ofd, records, file_size)!= file_size){
perror("write");
exit(EXIT_FAILURE);
}
if (fsync(ofd)<0){
perror("fsync");
exit(EXIT_FAILURE);
}
close(ofd);
munmap(records, file_size);
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Informix Database Administrators Survival Guide

Authors: Joe Lumbley

1st Edition

0131243144, 978-0131243149

More Books

Students also viewed these Databases questions