Difference between revisions of "ProvaPratica 2013.09.13"
Jump to navigation
Jump to search
(Created page with "[C esercizio 1 e 2] <syntaxhighlight lang="C"> /*********************************************************************** * Prova Pratica di Laboratorio di Sistemi Operativi ...") |
|||
Line 160: | Line 160: | ||
} | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | ------ | ||
+ | |||
+ | [Python esercizio 3] | ||
+ | <syntaxhighlight lang="Python"> | ||
+ | #!/usr/bin/env python3 | ||
+ | |||
+ | # Prova Pratica di Laboratorio di Sistemi Operativi | ||
+ | # 13 settembre 2013 | ||
+ | # Esercizio 3 | ||
+ | # | ||
+ | # URL: http://www.cs.unibo.it/~renzo/so/pratiche/2013.09.13.pdf * | ||
+ | # Autore: Eduardo Santarelli | ||
+ | |||
+ | |||
+ | class LineCounter: | ||
+ | |||
+ | def __init__(self, fileList): | ||
+ | # A list containing the char-per-line | ||
+ | # count for the specified files. | ||
+ | # Value for line 1 is in result[0] and so on. | ||
+ | self.result = [] | ||
+ | self.files = fileList # List of files to elaborate. | ||
+ | self.getLinesCount() | ||
+ | |||
+ | # Count the chars for each lines of specified file, | ||
+ | # add the result to the relevant field in self.result, | ||
+ | # if already present, append the value to the | ||
+ | # end of the list if not. | ||
+ | def countLines(self, file): | ||
+ | i = 0 | ||
+ | fd = open(file) | ||
+ | for line in fd: | ||
+ | lineLength = len(line) | ||
+ | # If an entry for the current line does | ||
+ | # not exist, append the value to the end of the list | ||
+ | try: | ||
+ | self.result[i] += lineLength | ||
+ | except IndexError: | ||
+ | self.result.append(lineLength) | ||
+ | i = i+1 | ||
+ | fd.close() | ||
+ | |||
+ | # Call self.countLines() for each file in self.files | ||
+ | # in order to build the self.result list | ||
+ | def getLinesCount(self): | ||
+ | for entry in self.files: | ||
+ | self.countLines(entry) | ||
+ | |||
+ | # Print the items in self.result, each preceded by the | ||
+ | # corresponding line number. | ||
+ | def printLines(self): | ||
+ | i = 1 | ||
+ | for value in self.result: | ||
+ | print("{:d}: {:d}".format(i, value)) | ||
+ | i = i+1 | ||
+ | |||
+ | |||
+ | import os | ||
+ | |||
+ | lc = LineCounter(os.listdir(os.getcwd())) | ||
+ | lc.printLines() | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 19:31, 9 March 2014
[C esercizio 1 e 2]
/***********************************************************************
* Prova Pratica di Laboratorio di Sistemi Operativi *
* 13 settembre 2013 *
* Esercizio 1+2 *
* *
* URL: http://www.cs.unibo.it/~renzo/so/pratiche/2013.09.13.pdf *
* Autore: Eduardo Santarelli *
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
/* An entry in the hash table. */
typedef struct fileEntry{
const char *filename;
struct fileEntry *next;
} fileEntry;
/* Global variables */
fileEntry **table = NULL;
int table_size;
/* Hash function. Calculates XOR of 8-bytes blocks *
* (actually, sizeof(long int)) read from the input file */
unsigned int hash(const char *fname){
FILE *f;
unsigned long int hash_val = 0;
unsigned long int tmp = 0;
f = fopen(fname, "r");
fread(&hash_val, sizeof(unsigned long int), 1, f);
while((fread(&tmp, sizeof(unsigned long int), 1, f) == 1))
hash_val = hash_val^tmp;
fclose(f);
return hash_val;
}
/* Creates a table of struct fileEntry elements, based on the hash values of the files *
* detected by scandir. If a collision is detected, the new entry is pointed by the *
* 'next' field of the current last element*/
void populate_table(struct dirent *dir_list[], int n){
int i;
table_size = 2*n;
table = (fileEntry**)calloc(table_size, sizeof(fileEntry));
if(!table) abort();
for(i=0; i<n; i++){
unsigned int index;
fileEntry *entry = NULL;
entry = (fileEntry*)malloc(sizeof(fileEntry));
if(!entry) abort();
index = hash(dir_list[i]->d_name)%table_size;
if(table[index] == NULL){
entry->filename = dir_list[i]->d_name;
entry->next = NULL;
table[index] = entry;
}
else{
entry->filename = dir_list[i]->d_name;
entry->next = table[index];
table[index] = entry;
}
}
}
/* Compares two files */
int fcompare(const char *f1, const char *f2){
FILE *f, *g;
int file1, file2;
f = fopen(f1, "r");
g = fopen(f2, "r");
while(fread(&file1, sizeof(int), 1, f) && fread(&file2, sizeof(int), 1, g)){
if(file1!=file2){
fclose(f);
fclose(g);
return 0;
}
}
fclose(f);
fclose(g);
return 1;
}
/* This function scans the file table. If an entry contains more than one file, *
* it compares all them. If a duplicate is found, the first file is removed, *
* and a hard link is created with the deleted file's name, pointing to the *
* second, identical, file. */
void deduplicate(){
int i;
for(i=0; i<table_size; i++){
while(table[i] != NULL && table[i]->next != NULL){
fileEntry *file1, *file2;
file1 = table[i];
file2 = table[i]->next;
while(file2 != NULL){
if(fcompare(file1->filename, file2->filename)){
unlink(file1->filename);
link(file2->filename, file1->filename);
printf("%s -> %s\n", file1->filename, file2->filename);
break;
}
file2 = file2->next;
}
table[i] = table[i]->next;
}
}
}
/*selector function for scandir() */
int exclude(const struct dirent *dir){
if(dir->d_type != DT_REG)
return 0;
else
return 1;
}
int main(int argc, char *argv[]){
struct dirent **entries = NULL;
int num_files;
num_files = scandir(".", &entries, exclude, alphasort);
populate_table(entries, num_files);
deduplicate();
return EXIT_SUCCESS;
}
[Python esercizio 3]
#!/usr/bin/env python3
# Prova Pratica di Laboratorio di Sistemi Operativi
# 13 settembre 2013
# Esercizio 3
#
# URL: http://www.cs.unibo.it/~renzo/so/pratiche/2013.09.13.pdf *
# Autore: Eduardo Santarelli
class LineCounter:
def __init__(self, fileList):
# A list containing the char-per-line
# count for the specified files.
# Value for line 1 is in result[0] and so on.
self.result = []
self.files = fileList # List of files to elaborate.
self.getLinesCount()
# Count the chars for each lines of specified file,
# add the result to the relevant field in self.result,
# if already present, append the value to the
# end of the list if not.
def countLines(self, file):
i = 0
fd = open(file)
for line in fd:
lineLength = len(line)
# If an entry for the current line does
# not exist, append the value to the end of the list
try:
self.result[i] += lineLength
except IndexError:
self.result.append(lineLength)
i = i+1
fd.close()
# Call self.countLines() for each file in self.files
# in order to build the self.result list
def getLinesCount(self):
for entry in self.files:
self.countLines(entry)
# Print the items in self.result, each preceded by the
# corresponding line number.
def printLines(self):
i = 1
for value in self.result:
print("{:d}: {:d}".format(i, value))
i = i+1
import os
lc = LineCounter(os.listdir(os.getcwd()))
lc.printLines()