ProvaPratica 2012.09.19

From Sistemi Operativi
Revision as of 11:03, 16 May 2014 by Coci (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

URL-> http://www.cs.unibo.it/~renzo/so/pratiche/2012.09.19.pdf


[C Esercizio 1]

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>

#define BUFSIZE 1024

long int n = 0;

void handler(int sig_num){

	signal(SIGUSR1, handler);
	fprintf(stderr, "\n%ld Bytes copied until now\n", n);
	
}

int main(){

	int nread, nwritten,
	    i = 0;
	char buffer[BUFSIZE],
	     cool_gui[] = "-\\|/";

	signal(SIGUSR1, handler);
	fprintf(stderr, "Copying   ");

	while( (nread = read(STDIN_FILENO, buffer, BUFSIZE)) != 0 ){
		if(nread < 0){
			perror("Read error: ");
			exit(EXIT_FAILURE);
		}

		nwritten = write(STDOUT_FILENO, buffer, nread);
                if(nwritten < 0){
                        perror("Write error: ");
                        exit(EXIT_FAILURE);
                }

		n += nwritten
		fprintf(stderr, "\b\b%c ",cool_gui[i%4]);
		(i == INT_MAX) ? (i=0) : (i++); 	
	}
			
	fprintf(stderr, "\nDone\n");

	return EXIT_SUCCESS;

}

-Eduardo

#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/eventfd.h>

uint64_t counter = 0;
int seconds = 1;

void sig_handler(int signo){	
		double ratio = (double) counter / (double) seconds;
		fprintf(stderr, "Finora sono stati scritti %lf byte/s\n", ratio);
}

int main(){
	int efd;
	int anim_counter = 0;
	char buf;
	char animation[] = {'-','\\', '|', '/' };
	signal(SIGUSR1, sig_handler);
	efd = eventfd(0, 0);
	fprintf(stderr, " ");
	if(fork()){//processo nonno
		while(1){
		fprintf(stderr, "\b%c", animation[anim_counter]);
		anim_counter = (anim_counter+1)%4;
		usleep(100000);
		}
	}
	else {
		if(fork()){//padre	
			while(read(STDIN_FILENO, &buf, 1)){
				write(STDOUT_FILENO, &buf, 1);
				write(efd, &counter, sizeof(uint64_t));
				counter++;
				}
			}
		else {//nipote
			sleep(1);
			while( read(efd, &counter, sizeof(uint64_t)) ){
				fprintf(stderr, "\n%ds: %llu bytes\n",seconds, counter);
				seconds++;
				sleep(1);
				}
			}	
	wait(NULL);
	}
	return 1;
}

-Coci (avevo provato a farlo con la pipe, ma dava dei problemi)


[Bash Esercizio 3] In due versioni:
1. one-liner, per amore di leggibilità

 IFS=$(echo -ne "\n\b") && file `find $DIRECTORY` | sed -rn "s/(.*):\ +`file -b $FILENAME`/\1/p"

Purtroppo se esistono troppi file nell'albero della directory non funziona (Argument list too long)
2. script

#!/bin/bash

SUCCESS=0
FAILURE=1

#Check arguments
if [[ $# -ne 2 ]]
then
	echo "Usage: `basename $0` file directory"
	exit $FAILURE
fi

IFS=$(echo -e "\n\b")        #set newline as separator
file_type=$(file -b $1)
file_list=$(find $2) 

for item in $file_list
do
	item_type=$(file -b $item)
	if [[ $item_type == $file_type ]]
	then
		echo $item
	fi
done

exit $SUCCESS

La riga di codice per settare il separatore l'ho trovata qui: http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html . Non mi è chiaro perchè il separatore debba essere "\n\b" e non solo "\n", ma ho provato in varie salse, e questo è l'unico modo in cui lo script funziona.
-Eduardo