Prova pratica 2016.09.13

From Sistemi Operativi
Jump to navigation Jump to search

Link al testo (è sbagliata la data nel titolo ma il testo è quello giusto)

Esercizio 1

Scrivere un programma che mostri il funzionamento del file locking.
In particolare il programma cplk avra’ due argomenti che I pathname di due file. Il contenuto del primo file deve
essere copiato nel secondo usando la system call sendfile.
Prima di iniziare la copia occorre usare la system call fcntl per bloccare il file di output (write lock).
Completata la copia occorre chiamare nuovamente la fcntl per sbloccare il file.
Inserire un ritardo (sleep) con valore casuale da 1 a 10 secondi sia prima sia dopo aver sbloccato il file e
stampe di controllo che indichino quando il file viene bloccato e sbloccato.
In questo modo lanciando piu’ esecuzioni concorrenti con output sullo stesso file deve apparire che l’accesso in
scrittura e’ mutualmente esclusivo.
Es. di output:
$ cplk cplk.c out
locked attende da 1 a 10 sec
unlocked attende da 1 a 10 sec
$

Soluzione di FedericoB

#define _GNU_SOURCE

#include <unistd.h>
#include <stdlib.h>
#include <sys/sendfile.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <time.h>

int main(int argc, char* argv[]) {
    if (argc == 3) {
        //get file descriptors
        int fdFile2 = open(argv[2], O_WRONLY);
        int fdFile1 = open(argv[1], O_RDONLY);
        //create structure for file locking
        struct flock flock;
        struct stat stat_buf;
        fstat(fdFile1, &stat_buf);
        //type = write lock
        flock.l_type = F_WRLCK;
        flock.l_start = 0;
        //set l_start relative of beginning of file
        flock.l_whence = SEEK_SET;
        flock.l_len = stat_buf.st_size;
        flock.l_pid = getpid();
        fcntl(fdFile2, F_SETLKW, &flock);
        //print "locked"
        printf("locked\n");
        //use sendfile for copy, use fdFile1 offset and copy all file 1
        sendfile(fdFile2, fdFile1, NULL, stat_buf.st_size);
        //wait for a random time between 1 and 10 seconds
        srand(time(NULL));
        int r = rand() % 10 + 1;
        sleep(r);
        //unclock second file
        flock.l_type = F_UNLCK;
        fcntl(fdFile2, F_SETLKW, &flock);
        //print "unlocked"
        printf("unlocked\n");
        //wait for a random time between 1 and 10 second
        r = rand() % 10 + 1;
        sleep(r);
        return 0;
    } else {
        printf("wrong number of arguments!");
        return -1;
    }
}

Esercizio 2

Esercizio 3

Bash

Soluzione di G.C.

#!/bin/bash

if [ $# -lt 1 ] ; then
	echo "Argument error"
	exit
fi
a=0
find $1 -type f,p,d,c,b,s | (while read line
do
	a=$((a+1))
done

echo "Number: $a")

Python3

Soluzione di G.C.

import sys
import os

def countFile(d):
	count = 1
	for x in os.listdir(d):
		if os.path.isdir(os.path.join(d,x)):
			count += countFile(os.path.join(d,x))
		else:
			if not os.path.islink(os.path.join(d,x)):
				count+=1
	return count

if __name__ == '__main__':
	if len(sys.argv) < 2:
		print("Argument Error")
		sys.exit()
	directory = sys.argv[1]
	count = countFile(directory)
	print("Count: ",count)