ProvaPratica 2012.06.20

From Sistemi Operativi
Revision as of 09:03, 9 May 2017 by FedericoB (talk | contribs) (Aggiunte soluzioni da altre pagine, uniformato)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Link to Exam

Esercizio 1

Soluzione di Gab

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <linux/inotify.h>


int main (int arc, char *argv[])
{
	if(arc<2)
		return 0;
	char *directoryToWatch;
	directoryToWatch = argv[1]+1;
	int notifierDescriptor = inotify_init();
	if(!notifierDescriptor)
	{
		perror("inotify_init");
		return 0;		
	}
	int watcherDescriptor = inotify_add_watch( notifierDescriptor,directoryToWatch, IN_CREATE);
	char eventBuffer[sizeof(struct inotify_event)+256];
	while(read(notifierDescriptor,eventBuffer,sizeof(struct inotify_event)+256)>0)
	{
		struct inotify_event *event = ( struct inotify_event * ) eventBuffer;
		if ( event->mask & IN_CREATE ) 
		{
     		   	if ( event->mask & IN_ISDIR ) 
        			  printf( "directory %s created.\n", event->name );
		   	else
			{
				struct stat fileStructure;			
				char fileName[strlen(directoryToWatch)+strlen(event->name)+1];
				strcpy(fileName,directoryToWatch);
				strcat(fileName,"/");
				strcat(fileName,event->name);
				int result = stat(fileName,&fileStructure);
				if(result<0)
				{
					return 0;				
				}
				if(S_IEXEC && fileStructure.st_mode)
				{	
					int resultFromFork = fork();
					if(!resultFromFork)
					{
						//child
						int result = execl(fileName,event->name,(char *)0);
						if(result<0)
							 printf("an error: %s\n", strerror(errno));
					}	
					else
					{
						
						if(waitpid(-1,NULL,0))
							unlink(fileName);
						
					}				
				}
			}
        	}
	}
	
	inotify_rm_watch( notifierDescriptor, watcherDescriptor );
	close(notifierDescriptor);
	return 0;
}

Soluzione di Davide Boldrin

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <linux/inotify.h>

#define EVENT_SIZE  ( sizeof (struct inotify_event) )
#define EVENT_BUF_LEN     ( 1024 * ( EVENT_SIZE + 16 ) )

int main(int argc, char *argv[]){
while(1){ 
 int length, i = 0;
  int fd;
  int wd;
  char buffer[EVENT_BUF_LEN];
  
  fd = inotify_init();

  if ( fd < 0 ) {
    perror( "inotify_init" );
  }

  wd = inotify_add_watch( fd, argv[1], IN_CREATE | IN_DELETE );

  length = read( fd, buffer, EVENT_BUF_LEN ); 

  if ( length < 0 ) {
    perror( "read" );
  }  

  while ( i < length) {    
	 struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];  
		if ( event->len ) {
      if ( event->mask & IN_CREATE ) {
        if ( event->mask & IN_ISDIR ) {
          printf( "Nuova cartella %s creata.\n", event->name );
        }
        else {
          printf( "Nuovo file %s creato.\n", event->name );
        }
      }
      else if ( event->mask & IN_DELETE ) {
        if ( event->mask & IN_ISDIR ) {
          printf( "Cartella %s eliminata.\n", event->name );
        }
        else {
          printf( "File %s eliminato.\n", event->name );
        }
      }
    }
    i += EVENT_SIZE + event->len;
  }
  

}
}

Qualcuno ha idea di come implementare l'esercizio 2 dello stesso appello!?

Nota del Prof: l'esercizio non prevedeva il controllo delle directory create/eliminate. Se e' stato compreso il funzionamento e non e' solo una traduzione della fonte, allora basta mettere al riconoscimento del file una access o simili pervedere se e' eseguibile, fork/exec per eseguire il file e infine unlink per eliminarlo. Renzo (talk) 16:27, 7 April 2015 (CEST)

Esercizio 2

Soluzione di Maldus

#include <stdio.h>
#include <stdlib.h>
#include <sys/inotify.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#define SIZE sizeof(struct inotify_event) + NAME_MAX + 1

char buf[SIZE];	/*creo un buffer in grado di contenere una sola struttura inotify_event; dovrebbe essere più grande?*/

int main( int argc , char* argv[]){
	int fd , w , status;
	pid_t id;
	struct inotify_event* event;
        if( argc < 2 ){
		printf("not enough arguments\n") ;
		exit(1) ;
	}
	fd = inotify_init();
	if( fd == -1) perror("inotify_init:");
	w = inotify_add_watch( fd , argv[1] , IN_CREATE | IN_DELETE_SELF );
	if( w == -1 ) perror("inotify_add_watch:");
	printf("%s sotto controllo:\n", argv[1]) ;
	chdir(argv[1]);
	while(1){
		read( fd , buf , SIZE );
		event = (struct inotify_event*) &buf[0] ;
		if( event->mask & IN_DELETE_SELF) break;
		else if(event->mask & IN_CREATE){
			if(!(event->mask & IN_ISDIR)){
				printf("Creato %s\n" , event->name);
				if( access(event->name, X_OK)== 0){
					if( fork() ){
						wait(&status);
						if( unlink(event->name) == -1 ) perror("unlink:");
					}
					else{
						execl( event->name , event->name , NULL);
						perror("execl:");
						exit(1);
						
					}
				}
			}
		}
	}
	printf("directory eliminata\n") ;
	return 0 ;
}

Esercizio 3

Soluzione di Maldus

import fnmatch, os , sys

sum=0

if len(sys.argv)<2:
        print("not enough arguments")
        sys.exit(2)

for file in os.listdir('.'):
        if fnmatch.fnmatch(file, sys.argv[1]) and os.path.isfile(file):
                sum=sum + os.stat(file).st_size

print("totale: %i" %sum)