Prove pratiche 2018

From Sistemi Operativi
Revision as of 09:15, 30 August 2020 by Acsor (talk | contribs) (Created page with "== Esame Pratico 21/09/2018 == http://www.cs.unibo.it/~renzo/so/pratiche/2018.09.21.pdf === Esercizio 1 === <source lang="c"> #define _GNU_SOURCE #include <sys/s...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Esame Pratico 21/09/2018

http://www.cs.unibo.it/~renzo/so/pratiche/2018.09.21.pdf

Esercizio 1

       #define _GNU_SOURCE
       #include <sys/signalfd.h>
       #include <signal.h>
       #include <sys/types.h>
       #include <sys/stat.h>
       #include <fcntl.h>
       #include <stdio.h>
       #include <unistd.h>
       #include <time.h>
       #include <string.h>


int main(int argc, char * argv[]){
        sigset_t mask;
        struct signalfd_siginfo fdsi;
        int fd;
        char* t;
        char* filename;

        sigemptyset(&mask);
        sigaddset(&mask, SIGUSR1);
        sigaddset(&mask, SIGUSR2);
        sigprocmask(SIG_SETMASK, &mask, NULL);

        int sigfd = signalfd(-1, &mask, 0);

        for(;;){
                read(sigfd, &fdsi, sizeof(struct signalfd_siginfo));
                if(fdsi.ssi_signo == SIGUSR1){
                        asprintf(&filename, "./%u", fdsi.ssi_pid);
                        fd = open(filename, O_APPEND | O_CREAT | O_WRONLY);
                        time_t stime = time(NULL);
                        t = ctime(&stime);
                        write(fd, t, sizeof(char)*strlen(t));
                        close(fd);
                        }
                if(fdsi.ssi_signo == SIGUSR2){
                        asprintf(&filename, "./%u", fdsi.ssi_pid);
                        fd = open(filename, O_APPEND | O_CREAT | O_WRONLY);
                        time_t stime = time(NULL);
                        t = ctime(&stime);
                        write(fd, t, sizeof(char)*strlen(t));
                        close(fd);
                        }
                }
        }


dovrebbe stampare anche il nome del segnale prima di stampare il tempo ma per il resto va bene


Esercizio 3

#!/usr/bin/env python
import sys
import os
import re
dict = {}

def func(a):
    os.chdir(a)
    for root, dirs, files in os.walk("."):
      for filename in files:
        try:
          fl =file(filename)
          firstline = fl.readline()
          fl.close()
        except IOError: # caso di file che non riesce ad aprire
          #print("could not read", file)
          pass
        if (re.match('^#!', firstline)):
          dict.setdefault(firstline, []) #firstline include anche il carattere \n, quindi quando si stampa va anche a capo
          dict[firstline].append(filename)
    for k in dict.keys():
      print k
      for v in dict[k]:
        print v
      #print(" ")

if __name__ == '__main__':
  if (len(sys.argv)==1):
    dir = "."
  else:
    dir = sys.argv[1]
  func(dir)