diff --git a/lab3/Makefile b/lab3/Makefile new file mode 100644 index 00000000..8d8cd2a5 --- /dev/null +++ b/lab3/Makefile @@ -0,0 +1,27 @@ +CC = gcc +CFLAGS = -Wall -std=gnu99 -g # "curl-config --cflags" output is empty +LD = gcc +LDFLAGS = -std=gnu99 -g +LDLIBS = -lz -lcurl -pthread # "curl-config --libs" output + +SRCS = paster2.c zutil.c crc.c +OBJS = paster2.o zutil.o crc.o +TARGETS= paster2 + +all: ${TARGETS} + +paster2: $(OBJS) + $(LD) -o $@ $^ $(LDLIBS) $(LDFLAGS) + +%.o: %.c + $(CC) $(CFLAGS) -c $< + +%.d: %.c + gcc -MM -MF $@ $< + +-include $(SRCS:.c=.d) + +.PHONY: clean +clean: + rm -f *~ *.d *.o *~ $(TARGETS) *.png +~ \ No newline at end of file diff --git a/lab3/crc.c b/lab3/crc.c new file mode 100644 index 00000000..905ad7b0 --- /dev/null +++ b/lab3/crc.c @@ -0,0 +1,54 @@ +/******************************************************************** + * @file: crc.c + * @brief: PNG crc calculation + * Reference: https://www.w3.org/TR/PNG-CRCAppendix.html + */ + +/* Table of CRCs of all 8-bit messages. */ +unsigned long crc_table[256]; + +/* Flag: has the table been computed? Initially false. */ +int crc_table_computed = 0; + +/* Make the table for a fast CRC. */ +void make_crc_table(void) +{ + unsigned long c; + int n, k; + + for (n = 0; n < 256; n++) { + c = (unsigned long) n; + for (k = 0; k < 8; k++) { + if (c & 1) + c = 0xedb88320L ^ (c >> 1); + else + c = c >> 1; + } + crc_table[n] = c; + } + crc_table_computed = 1; +} + +/* Update a running CRC with the bytes buf[0..len-1]--the CRC + should be initialized to all 1's, and the transmitted value + is the 1's complement of the final running CRC (see the + crc() routine below)). */ + +unsigned long update_crc(unsigned long crc, unsigned char *buf, int len) +{ + unsigned long c = crc; + int n; + + if (!crc_table_computed) + make_crc_table(); + for (n = 0; n < len; n++) { + c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8); + } + return c; +} + +/* Return the CRC of the bytes buf[0..len-1]. */ +unsigned long crc(unsigned char *buf, int len) +{ + return update_crc(0xffffffffL, buf, len) ^ 0xffffffffL; +} diff --git a/lab3/crc.h b/lab3/crc.h new file mode 100644 index 00000000..7f581a9f --- /dev/null +++ b/lab3/crc.h @@ -0,0 +1,10 @@ +/** + * @file: crc.h + * @brief: crc calculation functions for PNG file + */ + +#pragma once + +void make_crc_table(void); +unsigned long update_crc(unsigned long crc, unsigned char *buf, int len); +unsigned long crc(unsigned char *buf, int len); diff --git a/lab3/lab3_eceubuntu1.csv b/lab3/lab3_eceubuntu1.csv new file mode 100644 index 00000000..2603ac06 --- /dev/null +++ b/lab3/lab3_eceubuntu1.csv @@ -0,0 +1,40 @@ +B,P,C,X,N,Time +5,1,1,0,1,11.031589 +5,1,5,0,1,16.740917 +5,5,1,0,1,2.379882 +5,5,5,0,1,3.559238 +10,1,1,0,1,10.849649 +10,1,5,0,1,14.668302 +10,1,10,0,1,18.287099 +10,5,1,0,1,2.185177 +10,5,5,0,1,3.189513 +10,5,10,0,1,4.437578 +10,10,1,0,1,1.179276 +10,10,5,0,1,1.513533 +10,10,10,0,1,1.901437 +5,1,1,200,1,10.478608 +5,1,5,200,1,10.845563 +5,5,1,200,1, +5,5,5,200,1,2.95628 +10,1,1,200,1,10.488353 +10,1,5,200,1,10.824044 +10,1,10,200,1,10.901582 +10,5,1,200,1, +10,5,5,200,1,2.508308 +10,5,10,200,1,2.5136 +10,10,1,200,1, +10,10,5,200,1, +10,10,10,200,1,1.720996 +5,1,1,400,1,20.07438 +5,1,5,400,1,10.747398 +5,5,1,400,1, +5,5,5,400,1,4.502687 +10,1,1,400,1,20.104304 +10,1,5,400,1,10.760721 +10,1,10,400,1,10.815564 +10,5,1,400,1, +10,5,5,400,1,4.515022 +10,5,10,400,1,2.949063 +10,10,1,400,1, +10,10,5,400,1, +10,10,10,400,1,2.562455 \ No newline at end of file diff --git a/lab3/paster2.c b/lab3/paster2.c new file mode 100644 index 00000000..8cd7dc9b --- /dev/null +++ b/lab3/paster2.c @@ -0,0 +1,707 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "crc.h" +#include "zutil.h" + +#define IMG_URL "http://ece252-1.uwaterloo.ca:2530/image?img=" +#define IMG_URL2 "http://ece252-2.uwaterloo.ca:2530/image?img=" +#define IMG_URL3 "http://ece252-3.uwaterloo.ca:2530/image?img=" +#define ECE252_HEADER "X-Ece252-Fragment: " + +#define PNG_SIG_SIZE 8 /* number of bytes of png image signature data */ +#define CHUNK_LEN_SIZE 4 /* chunk length field size in bytes */ +#define CHUNK_TYPE_SIZE 4 /* chunk type field size in bytes */ +#define CHUNK_CRC_SIZE 4 /* chunk CRC field size in bytes */ +#define DATA_IHDR_SIZE 13 /* IHDR chunk data field size */ + +#define NUM_SEMS 11 +#define SEM_PROC 1 +#define BUF_SIZE 10240 /* 1024*10 = 10K */ +#define MAX_SIZE 1000000 + +int B = 1; +int PROD_NUM = 1; +int CONS_NUM = 1; +int DELAY = 0; +int IMG_NUM = 1; + +sem_t *sems; + +typedef unsigned char U8; +typedef unsigned int U32; +typedef unsigned long int U64; + + +// URL : http://ece252-1.uwaterloo.ca:2530/image?img=n&part=m +// n - image number +// m - fragment number + +// B - buffer size +// P - # of producers +// C - # of consumers +// X - # of millseconds consumer sleeps before processing data +// N - image number + +// ./paster2 B P C X N +// B≥1,P=1,C=1,X≥0,andN=1,2,3 +// use gettimeofday (record time before creating first process + after last image segment is consumed and all.png generated) +// output: paster2 execution time: