Skip to content

Commit

Permalink
reader parallel 3 with errors
Browse files Browse the repository at this point in the history
  • Loading branch information
iluxonchik committed Nov 14, 2014
1 parent eadd7bd commit 8441dbc
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 145 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
# Project Specific Files
*.txt

# Finals folder (for compressed work or binaries)
finals

# temporary save files
*.swp

Expand All @@ -45,4 +48,4 @@ src/writer
src/writer_parent
src/reader_parent_1
src/reader_parent_2

src/reader_parent_3
159 changes: 89 additions & 70 deletions src/reader.c
Original file line number Diff line number Diff line change
@@ -1,93 +1,112 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/file.h>
#include <errno.h>
#include <sys/types.h> // for lseek()
#include <unistd.h> // for lseek()


#include "reader.h"
#include "shared_stuff.h"
#include "reader_constants.h"

static const int reader_f_flags = O_RDONLY;

int reader(int file_num) {
return reader_ranged(file_num, 0, LINES_PER_FILE - 1);
}

int reader(int file_num){
char file_name[64];
int fd, file_value;
sprintf(file_name, FILENAME, file_num); // place file_num in FILENAME

fd = open(file_name, reader_f_flags);
if (fd == -1) { // error opening the file (does not exist?)
printf("open(%s) failed. errno=%d\n", file_name, errno);
return FILE_IS_INVALID;
}

int reader_ranged(int file_num, int first_line, int last_line) {
char file_name[64];
int fd, file_value;
sprintf(file_name, FILENAME, file_num); // place file_num in FILENAME
fd = open(file_name, reader_f_flags);
if (fd == -1) { // error opening the file (does not exist?)
printf("open(%s) failed. errno=%d\n", file_name, errno);
return FILE_IS_INVALID;
}
flock(fd, LOCK_SH);

if ( file_contents_are_valid(fd, WRITER_STRING_LEN, LINES_PER_FILE) == TRUE ){
file_value = FILE_IS_VALID;
} else{
file_value = FILE_IS_INVALID;
}


if ( file_contents_are_valid(fd, WRITER_STRING_LEN,
first_line, last_line) == TRUE ) {
file_value = FILE_IS_VALID;
} else {
file_value = FILE_IS_INVALID;
}

flock(fd, LOCK_UN);

close(fd);
return file_value;
close(fd);
return file_value;
}


int file_contents_are_valid(int fd, int line_length, int line_count)
{
// number of bytes to read at a time from the file
int line_size = line_length * sizeof(char);

char *first_line = (char*) malloc(line_size);

// try to read and validate the first line of the file
if ( read(fd, first_line, line_size) != line_size ) {
// the first line doesn't even have the correct number of characters
free(first_line);
return FALSE; // file is invalid
}
if ( !known_writer_string(first_line, line_length) ){
free(first_line);
return FALSE; // file is invalid
}

// the first line is valid, so we can compare it with all other lines
char *line_buffer = (char*) malloc(line_size);

// compare all lines with the first line
int i;
for ( i = 1 ; read(fd, line_buffer, line_size) == line_size; i++ )
{
if ( strncmp(line_buffer, first_line, line_length) != 0 || i >= line_count ){
free(first_line);
free(line_buffer);
return FALSE; // file is invalid
}
}

free(first_line);
free(line_buffer);

if (i != line_count)
return FALSE;

return TRUE; // file is valid
int file_contents_are_valid(int fd, int line_length, int first_line, int last_line){
int line_count = last_line - first_line + 1;

// number of bytes to read at a time from the file
int line_size = line_length * sizeof(char);
DBG_PRINTF("fd=%d, line_size = %d\n", fd, line_size);

char *first_line_buf = (char*) malloc(line_size);

// try to read and validate the first line of the file
if ( read(fd, first_line_buf, line_size) != line_size ) {
// the first line doesn't even have the correct number of characters
free(first_line_buf);
DBG_PRINT("invalid file detected here\n");
return FALSE; // file is invalid
}

DBG_PRINTF("fd=%d, first_line_buf='%s'\n",fd,first_line_buf);
if ( !known_writer_string(first_line_buf, line_length) ){
free(first_line_buf);
DBG_PRINT("invalid file detected here\n");
return FALSE; // file is invalid
}

// the first line is valid, so we can compare it with all other lines
char *line_buffer = (char*) malloc(line_size);

lseek( fd, first_line * line_size, SEEK_SET );

// compare all lines with the first line
int i;
for ( i = 0 ; read(fd, line_buffer, line_size) == line_size; i++ )
{
if ( strncmp(line_buffer, first_line_buf, line_length) != 0 || i >= line_count ){
free(first_line_buf);
free(line_buffer);
DBG_PRINTF("fd=%d, invalid file detected here; first_line=%d, i=%d\nline_buff='%s'; first_line_buf='%s'\n",
fd,first_line, i, line_buffer, first_line_buf);
return FALSE; // file is invalid
}
}

free(first_line_buf);
free(line_buffer);

if (i != line_count) {
DBG_PRINT("invalid file detected here\n");
return FALSE;
}

return TRUE; // file is valid
}

int known_writer_string(char *str, int str_len){
int i;

for ( i = 0; i < WRITER_STRING_COUNT; i++ ){
if ( strncmp(str, writer_strings[i], str_len) == 0 ){
return TRUE;
}
}

return FALSE;
int i;

for ( i = 0; i < WRITER_STRING_COUNT; i++ ){
if ( strncmp(str, writer_strings[i], str_len) == 0 ){
return TRUE;
}
}
DBG_PRINT("invalid file detected here\n");

return FALSE;
}
16 changes: 14 additions & 2 deletions src/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,30 @@
*/
int reader(int file_num);

/**
* @brief Check the specified and limited file for consistency.
*
* @param file_num The file number
* @param first_line Line to start reading
* @param last_line Line where reading ends
*
* @retval FILE_IS_VALID or FILE_IS_INVALID
*/
int reader_ranged(int file_num, int first_line, int last_line);

/**
* @brief Checks wether the file is valid.
* A file is considered valid if it repeats one of the known writer strings
* line_count times.
*
* @param fd The file descriptor of the file to check
* @param line_length Expected length of each line, in characters
* @param line_count Expected number of lines in the file
* @param first_line First line to start checking
* @param last_line Last line where checking ends
*
* @retval TRUE if the file is valid; FALSE otherwise
*/
int file_contents_are_valid(int fd, int line_length, int line_count);
int file_contents_are_valid(int fd, int line_length, int first_line, int last_line);

/**
* @brief looks for str in writer_strings
Expand Down
2 changes: 1 addition & 1 deletion src/reader_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define READER_2_THREAD_COUNT 3

/* Constant K for Exercise 4 pdf */
#define READER_3_THREAD_COUNT 7
#define READER_3_THREAD_COUNT 2

#define FILE_IS_VALID 0
#define FILE_IS_INVALID -1
Expand Down
2 changes: 1 addition & 1 deletion src/reader_parent_main_2.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void *reader_thread(void *arg) {
int file_num = *((int*) arg);
int *ret = (int*) malloc( sizeof(int) );

if (ret == NULL)
if (ret == NULL || arg == NULL)
exit(-1);

*ret = reader(file_num);
Expand Down
Loading

0 comments on commit 8441dbc

Please sign in to comment.