add: assigment 2
This commit is contained in:
parent
3a0b8a5c3f
commit
32a6e07e56
11 changed files with 437 additions and 0 deletions
47
assignment2/part1/consumer.c
Normal file
47
assignment2/part1/consumer.c
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <getopt.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int max_lines = -1; // -1 means unlimited
|
||||
int verbose = 0;
|
||||
|
||||
// Parse arguments (-n max_lines, -v verbose)
|
||||
char opt;
|
||||
while ((opt = getopt(argc, argv, "n:v")) != -1) {
|
||||
switch (opt) {
|
||||
case 'n':
|
||||
max_lines = atoi(optarg); break;
|
||||
case 'v':
|
||||
verbose = 1; break;
|
||||
default:
|
||||
printf("Usage: %s [-n max_lines] [-v]\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Read from stdin line by line
|
||||
// Count lines and characters
|
||||
// If verbose, echo lines to stdout
|
||||
char buffer[256];
|
||||
int lines = 0;
|
||||
int chars = 0;
|
||||
while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
|
||||
int size = strlen(buffer);
|
||||
if (verbose)
|
||||
fwrite(buffer, sizeof(char), size, stdout);
|
||||
chars += size;
|
||||
if (size > 0 && buffer[size - 1] == '\n')
|
||||
lines++;
|
||||
if (lines == max_lines)
|
||||
break;
|
||||
}
|
||||
|
||||
// Print statistics to stderr
|
||||
fprintf(stderr, "Lines: %d\n", lines);
|
||||
|
||||
return 0;
|
||||
}
|
||||
44
assignment2/part1/producer.c
Normal file
44
assignment2/part1/producer.c
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <getopt.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
FILE *input = stdin;
|
||||
int buffer_size = 4096;
|
||||
|
||||
// Parse command line arguments
|
||||
// -f filename (optional)
|
||||
// b buffer_size (optional)
|
||||
char opt;
|
||||
while ((opt = getopt(argc, argv, "f:b:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'f':
|
||||
input = fopen(optarg, "r");
|
||||
if (input == NULL) {
|
||||
fprintf(stderr, "Error: Could not open input file\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
break;
|
||||
case 'b':
|
||||
buffer_size = atoi(optarg); break;
|
||||
default:
|
||||
printf("Usage: %s [-f filename] [-b buffer_size]\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Allocate buffer
|
||||
char *buffer = malloc(buffer_size);
|
||||
|
||||
// Read from input and write to stdout
|
||||
while (fgets(buffer, buffer_size, input) != NULL) {
|
||||
fwrite(buffer, sizeof(char), strlen(buffer), stdout);
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
free(buffer);
|
||||
|
||||
return 0;
|
||||
}
|
||||
47
assignment2/part1/test
Executable file
47
assignment2/part1/test
Executable file
|
|
@ -0,0 +1,47 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
echo "Building executables..."
|
||||
|
||||
cd "${FLAKE:-$(dirname $0)}"
|
||||
if command -v nix; then
|
||||
producer="$(nix build --no-link --print-out-paths .#a2-p1-producer)/bin/producer"
|
||||
consumer="$(nix build --no-link --print-out-paths .#a2-p1-consumer)/bin/consumer"
|
||||
else
|
||||
gcc producer.c -o producer
|
||||
gcc consumer.c -o consumer
|
||||
producer="./producer"
|
||||
consumer="./consumer"
|
||||
fi
|
||||
|
||||
echo -e "\nTEST - Sanity Check"
|
||||
|
||||
out="$(seq -s " " 1 10 | "$producer" | "$consumer" -v)"
|
||||
if test "$out" = "$(seq -s " " 1 10)"; then
|
||||
echo "SUCCESS"
|
||||
fi
|
||||
|
||||
echo -e "\nTEST - Smaller Buffer Size"
|
||||
|
||||
out="$(seq -s " " 1 10 | "$producer" -b 16 | "$consumer" -v)"
|
||||
if test "$out" = "$(seq -s " " 1 10)"; then
|
||||
echo "SUCCESS"
|
||||
fi
|
||||
|
||||
echo -e "\nTEST - Large External File"
|
||||
|
||||
head -c 10000 </dev/urandom | od -An -tx >random.txt
|
||||
out="$("$producer" -f random.txt | "$consumer" -v)"
|
||||
if test "$out" = "$(cat random.txt)"; then
|
||||
echo "SUCCESS"
|
||||
fi
|
||||
rm random.txt
|
||||
|
||||
|
||||
echo -e "\nTEST - Maximum Lines (5)"
|
||||
|
||||
head -c 1000 </dev/urandom | od -An -tx >random.txt
|
||||
out="$("$producer" -f random.txt | "$consumer" -v -n 5)"
|
||||
if test "$out" = "$(cat random.txt | head -n 5)"; then
|
||||
echo "SUCCESS"
|
||||
fi
|
||||
rm random.txt
|
||||
Loading…
Add table
Add a link
Reference in a new issue