Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <time.h>
- #include <sys/times.h>
- #include <string.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- void generate(char *filename, int size, int bytes) {
- int file = open(filename, O_CREAT | O_EXCL, 0666);
- if (file < 0) {
- file = open(filename, O_RDWR | O_APPEND);
- printf("File already exist, appending \n");
- }
- time_t t;
- srand((unsigned) time(&t));
- for (int i = 0; i < size; i++) {
- char result[bytes];
- for (int j = 0; j < bytes; j++) {
- char randomletter = 'a' + (rand() % 26);
- result[j] = randomletter;
- }
- write(file, result, bytes);
- write(file, "\n", 1);
- }
- close(file);
- }
- void sort(char *filename, int size, int bytes, char *variant) {
- int i = 1;
- if (strcmp(variant, "sys") == 0) {
- int src = open(filename, O_RDWR);
- while (i < size) {
- char *first, *second;
- first = (char *) malloc(sizeof(char) * bytes);
- second = (char *) malloc(sizeof(char) * bytes);
- if (first == NULL || second == NULL) {
- printf("Could not allocate memory!");
- exit(1);
- }
- int pos;
- if (i == 1) {
- pos = 0;
- } else {
- pos = (i - 1) * (bytes + 1);
- }
- lseek(src, pos, SEEK_SET);
- int j = i;
- read(src, first, bytes);
- lseek(src, 1, SEEK_CUR);
- read(src, second, bytes);
- lseek(src, 1, SEEK_CUR);
- lseek(src, -(bytes + 1), SEEK_CUR);
- while (j > 0 && first[0] > second[0]) {
- write(src, first, bytes);
- lseek(src, -((bytes * 2) + 1), SEEK_CUR);
- write(src, second, bytes);
- if (j == 1) {
- break;
- }
- lseek(src, -((bytes * 2) + 1), SEEK_CUR);
- read(src, first, bytes);
- lseek(src, 1, SEEK_CUR);
- j--;
- }
- i++;
- }
- close(src);
- } else if (strcmp(variant, "lib") == 0) {
- FILE *src = fopen(filename, "r+");
- while (i < size) {
- char *first, *second;
- first = (char *) malloc(sizeof(char) * bytes);
- second = (char *) malloc(sizeof(char) * bytes);
- if (first == NULL || second == NULL) {
- printf("Could not allocate memory!");
- exit(1);
- }
- int pos;
- if (i == 1) {
- pos = 0;
- } else {
- pos = (i - 1) * (bytes + 1);
- }
- fseek(src, pos, 0);
- int j = i;
- fread(first, 1, bytes, src);
- fseek(src, 1, 1);
- fread(second, 1, bytes, src);
- fseek(src, 1, 1);
- fseek(src, -(bytes + 1), 1);
- while (j > 0 && first[0] > second[0]) {
- fwrite(first, 1, bytes, src);
- fseek(src, -((bytes * 2) + 1), 1);
- fwrite(second, 1, bytes, src);
- if (j == 1) {
- break;
- }
- fseek(src, -((bytes * 2) + 1), 1);
- fread(first, 1, bytes, src);
- fseek(src, 1, 1);
- j--;
- }
- i++;
- }
- fclose(src);
- }
- }
- void copy(char *srcname, char *destname, int size, int bytes) {
- int src = open(srcname, O_RDONLY);
- int dest = open(destname, O_WRONLY | O_CREAT, 0666);
- for (int i = 0; i < size; i++) {
- char line[bytes];
- read(src, line, bytes);
- off_t off = 1;
- lseek(src, off, SEEK_CUR);
- write(dest, line, bytes);
- write(dest, "\n", 1);
- }
- close(src);
- close(dest);
- }
- void printTimeSpent(struct timespec start, struct timespec end) {
- long msend = end.tv_nsec / 100000;
- int time = (end.tv_sec - start.tv_sec);
- printf("time spent: %i.%lu s \n", time, msend);
- }
- int main(int argc, char **argv) {
- struct timespec realtimestart;
- struct timespec finish;
- struct tms tms_counter;
- double cpu_time_used;
- clock_t cpu_start, cpu_end;
- cpu_start = clock();
- clock_gettime(CLOCK_REALTIME, &realtimestart);
- //PROGRAM START
- if (argc < 2) {
- printf("No argument passed");
- return 0;
- }
- if (strcmp(argv[1], "generate") == 0) {
- int size, bytes;
- sscanf(argv[3], "%d", &size);
- sscanf(argv[4], "%d", &bytes);
- generate(argv[2], size, bytes);
- }
- if (strcmp(argv[1], "sort") == 0) {
- int size, bytes;
- sscanf(argv[3], "%d", &size);
- sscanf(argv[4], "%d", &bytes);
- sort(argv[2], size, bytes, argv[5]);
- }
- if (strcmp(argv[1], "copy") == 0) {
- int size, bytes;
- sscanf(argv[4], "%d", &size);
- sscanf(argv[5], "%d", &bytes);
- copy(argv[2], argv[3], size, bytes);
- }
- //PROGRAM END
- cpu_end = clock();
- clock_gettime(CLOCK_REALTIME, &finish);
- times(&tms_counter);
- printTimeSpent(realtimestart, finish);
- cpu_time_used = ((double) (cpu_end - cpu_start)) / CLOCKS_PER_SEC;
- printf("cpu time: %f s \n", cpu_time_used);
- int processor_time_used = (int) tms_counter.tms_utime;
- printf("processor clock ticks: %u \n", processor_time_used);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement