Difference between revisions of "Prove pratiche 2022"

From Sistemi Operativi
Jump to navigation Jump to search
 
(7 intermediate revisions by 2 users not shown)
Line 43: Line 43:
  
 
== Esercizio 3 ==
 
== Esercizio 3 ==
 +
 +
=== Proposta 1 ===
 +
 +
Da contorllare [[User:Flecart|Flecart]] ([[User talk:Flecart|talk]]) 14:52, 9 March 2023 (CET)
 +
 +
<syntaxhighlight lang="python">
 +
import sys
 +
import os
 +
import stat
 +
 +
def is_elf(file):
 +
    with open(file, "rb") as f:
 +
        magic = f.read()[:4]
 +
        if magic == b'\x7fELF':
 +
            return True
 +
    return False
 +
 +
def count(dir):
 +
    if not os.path.isdir(dir):
 +
        return 0
 +
 +
    num_bits = 0
 +
    for root, dir, files in os.walk(dir):
 +
        for file in files:
 +
            end_path = os.path.join(root, file)
 +
            if is_elf(end_path):
 +
                num_bits += os.lstat(end_path).st_size
 +
 +
        break; # per non andare in altre directory
 +
 +
    return num_bits
 +
 +
if __name__ == "__main__":
 +
    num_args = len(sys.argv);
 +
   
 +
    num_bits = 0
 +
    for i in range(1, num_args):
 +
        num_bits += count(sys.argv[i])
 +
 +
    if num_args == 1:
 +
        num_bits += count("./")
 +
 +
    print(f"number of bits is : {num_bits}");
 +
 +
</syntaxhighlight>
 +
=== Proposta 2 ===
 +
<syntaxhighlight lang="python">
 +
import os, sys, subprocess, re
 +
print(sum([os.path.getsize(pa+'/'+f) if re.search("ELF\s.*exec", str(subprocess.run(["file", pa+"/"+f], stdout=subprocess.PIPE).stdout)) else 0 for pa in (sys.argv[1:] if len(sys.argv)>1 else ["."]) for f in os.listdir(pa)]),"bytes") if __name__=="__main__" else None
 +
</syntaxhighlight>
  
 
== Esercizio 4 ==
 
== Esercizio 4 ==
 +
 +
 +
= 2022-06-22 =
 +
 +
== Esercizio 1 ==
 +
 +
== Esercizio 2 ==
 +
 +
=== Proposta 1 ===
 +
 +
==== rx ====
 +
<syntaxhighlight lang="C">
 +
#include <stdio.h>
 +
#include <unistd.h>
 +
#include <signal.h>
 +
 +
 +
void received_string(int signo, siginfo_t *info, void *context){
 +
    long msg_long = (long)info->si_value.sival_ptr;
 +
    if(msg_long == 0)
 +
        printf("\n");
 +
    else{
 +
        char msg;
 +
        for(int i=0, last = 0; i<8; i++){
 +
            last = msg_long%256;
 +
            msg_long = msg_long>>8;
 +
            printf("%c", last);
 +
        }
 +
    }
 +
    union sigval v;
 +
    sigqueue(info->si_pid, SIGUSR1, v);
 +
}
 +
 +
int main(int argc, char *argv[]){
 +
    printf("%d\n", getpid());
 +
    struct sigaction act;
 +
    act.sa_sigaction = received_string;
 +
    act.sa_flags = SA_SIGINFO;
 +
    sigaction(SIGUSR1, &act, NULL);
 +
    while(1)
 +
        sleep(20);
 +
    return 0;
 +
}
 +
</syntaxhighlight>
 +
 +
==== tx ====
 +
<syntaxhighlight lang="C">
 +
#include <signal.h>
 +
#include <stdlib.h>
 +
#include <stdio.h>
 +
#include <unistd.h>
 +
 +
int main(int argc, char *argv[]){
 +
    if(argc<3)
 +
        exit(EXIT_FAILURE);
 +
    int rxpid = atoi(argv[1]),
 +
        base = 0,
 +
        i,
 +
        retsig;
 +
    long msg;
 +
    union sigval val;
 +
 +
    sigset_t ret_set;
 +
    sigemptyset(&ret_set);
 +
    sigaddset(&ret_set, SIGUSR1);
 +
    sigprocmask(SIG_BLOCK, &ret_set, NULL);
 +
    do{
 +
        msg = 0;
 +
        for(i=base; i<base+8&&argv[2][i]!=0; i++)
 +
            msg += (long)argv[2][i]<<8*(i%8);
 +
        val.sival_ptr = (void*)msg;
 +
        sigqueue(rxpid, SIGUSR1, val);
 +
        sigwait(&ret_set, &retsig);
 +
        base+=8;
 +
    }while(argv[2][i]!=0);
 +
    val.sival_ptr = 0;
 +
    sigqueue(rxpid, SIGUSR1, val);
 +
    return 0;
 +
}
 +
</syntaxhighlight>
 +
 +
 +
= 2022-01-18 =
 +
 +
== Esercizio 1 ==
 +
 +
=== Proposta 1 (da controllare) ===
 +
[[User:Flecart|Flecart]] ([[User talk:Flecart|talk]]) 21:34, 26 April 2023 (CEST)
 +
 +
<syntaxhighlight lang="C">
 +
#include <stdio.h>
 +
#include <sys/types.h>
 +
#include <sys/wait.h>
 +
#include <sys/stat.h>
 +
#include <unistd.h>
 +
#include <string.h>
 +
#include <sys/types.h>
 +
#include <dirent.h>
 +
#include <limits.h>
 +
#include <stdlib.h>
 +
 +
int transform(char *name) {
 +
    struct stat filestat;
 +
    int ret_val = lstat(name, &filestat);
 +
    if (ret_val == -1) {
 +
        printf("got error when stat file %s \n", name);
 +
        return 1;
 +
    }
 +
 +
    if (S_ISLNK(filestat.st_mode)) {
 +
        char *abspath = realpath(name, NULL);
 +
        printf("realpath of %s is %s\n", name, abspath);
 +
        unlink(name);
 +
        symlink(abspath, name);
 +
        free(abspath);
 +
    }
 +
    return 0;
 +
}
 +
 +
char *concat(char *first, char *second) {
 +
    int first_len = strlen(first);
 +
    char *dest = (char *) malloc(first_len + strlen(second) + 2);
 +
    strcpy(dest, first);
 +
    dest[first_len] = '/';
 +
    dest[first_len + 1] = '\0';
 +
    strcat(dest, second);
 +
    return dest;
 +
}
 +
 +
int main(int argc, char *argv[]) {
 +
    if (argc != 2) {
 +
        printf("Expected 2 arguments, got %d", argc);
 +
        return 1;
 +
    }
 +
 +
    DIR * dir = opendir(argv[1]);
 +
    if (dir == NULL) {
 +
        printf("could not open dir\n");
 +
        return 1;
 +
    }
 +
 +
    struct dirent *d;
 +
    while ((d = readdir(dir)) != NULL) {
 +
        char *endstr = concat(argv[1], d->d_name);
 +
        printf("%s\n", endstr);
 +
        transform(endstr);
 +
        free(endstr);
 +
    }
 +
}
 +
 +
</syntaxhighlight>

Latest revision as of 20:34, 26 April 2023

2022-09-07

Esercizio 1

Proposta 1

Da controllare, 09/03/2023

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

int main(int argc, char *const argv[]) {
    if (argc < 2) {
        fprintf(stderr, "Non ci sono abbastanza parametri per lo script\n");
        return 1;
    }

    while (1) {
        int wstatus;
        if (fork()) {
            execvp(argv[1], argv + 1);
        } else {
            wait(&wstatus);
            int status = WEXITSTATUS(wstatus);
            printf("return value ***************** %d\n", status);
            if (status != 0) {
                break;
            }
        }
    }

    return 0;
}

Esercizio 2

Esercizio 3

Proposta 1

Da contorllare Flecart (talk) 14:52, 9 March 2023 (CET)

import sys
import os
import stat

def is_elf(file):
    with open(file, "rb") as f:
        magic = f.read()[:4]
        if magic == b'\x7fELF':
            return True
    return False

def count(dir):
    if not os.path.isdir(dir):
        return 0

    num_bits = 0 
    for root, dir, files in os.walk(dir):
        for file in files:
            end_path = os.path.join(root, file)
            if is_elf(end_path):
                num_bits += os.lstat(end_path).st_size

        break; # per non andare in altre directory

    return num_bits

if __name__ == "__main__":
    num_args = len(sys.argv);
    
    num_bits = 0
    for i in range(1, num_args):
        num_bits += count(sys.argv[i]) 

    if num_args == 1:
        num_bits += count("./")

    print(f"number of bits is : {num_bits}");

Proposta 2

import os, sys, subprocess, re
print(sum([os.path.getsize(pa+'/'+f) if re.search("ELF\s.*exec", str(subprocess.run(["file", pa+"/"+f], stdout=subprocess.PIPE).stdout)) else 0 for pa in (sys.argv[1:] if len(sys.argv)>1 else ["."]) for f in os.listdir(pa)]),"bytes") if __name__=="__main__" else None

Esercizio 4

2022-06-22

Esercizio 1

Esercizio 2

Proposta 1

rx

#include <stdio.h>
#include <unistd.h>
#include <signal.h>


void received_string(int signo, siginfo_t *info, void *context){
    long msg_long = (long)info->si_value.sival_ptr;
    if(msg_long == 0)
        printf("\n");
    else{
        char msg;
        for(int i=0, last = 0; i<8; i++){
            last = msg_long%256;
            msg_long = msg_long>>8;
            printf("%c", last);
        }
    }
    union sigval v;
    sigqueue(info->si_pid, SIGUSR1, v);
}

int main(int argc, char *argv[]){
    printf("%d\n", getpid());
    struct sigaction act;
    act.sa_sigaction = received_string;
    act.sa_flags = SA_SIGINFO;
    sigaction(SIGUSR1, &act, NULL);
    while(1)
        sleep(20);
    return 0;
}

tx

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

int main(int argc, char *argv[]){
    if(argc<3)
        exit(EXIT_FAILURE);
    int rxpid = atoi(argv[1]),
        base = 0,
        i,
        retsig;
    long msg;
    union sigval val;

    sigset_t ret_set;
    sigemptyset(&ret_set);
    sigaddset(&ret_set, SIGUSR1);
    sigprocmask(SIG_BLOCK, &ret_set, NULL);
    do{
        msg = 0;
        for(i=base; i<base+8&&argv[2][i]!=0; i++)
            msg += (long)argv[2][i]<<8*(i%8);
        val.sival_ptr = (void*)msg;
        sigqueue(rxpid, SIGUSR1, val);
        sigwait(&ret_set, &retsig);
        base+=8;
    }while(argv[2][i]!=0);
    val.sival_ptr = 0;
    sigqueue(rxpid, SIGUSR1, val);
    return 0;
}


2022-01-18

Esercizio 1

Proposta 1 (da controllare)

Flecart (talk) 21:34, 26 April 2023 (CEST)

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <limits.h>
#include <stdlib.h>

int transform(char *name) {
    struct stat filestat;
    int ret_val = lstat(name, &filestat);
    if (ret_val == -1) {
        printf("got error when stat file %s \n", name);
        return 1;
    }

    if (S_ISLNK(filestat.st_mode)) {
        char *abspath = realpath(name, NULL);
        printf("realpath of %s is %s\n", name, abspath);
        unlink(name);
        symlink(abspath, name);
        free(abspath);
    }
    return 0;
}

char *concat(char *first, char *second) {
    int first_len = strlen(first);
    char *dest = (char *) malloc(first_len + strlen(second) + 2);
    strcpy(dest, first);
    dest[first_len] = '/';
    dest[first_len + 1] = '\0';
    strcat(dest, second);
    return dest;
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        printf("Expected 2 arguments, got %d", argc);
        return 1;
    }

    DIR * dir = opendir(argv[1]);
    if (dir == NULL) {
        printf("could not open dir\n");
        return 1;
    }

    struct dirent *d;
    while ((d = readdir(dir)) != NULL) {
        char *endstr = concat(argv[1], d->d_name);
        printf("%s\n", endstr);
        transform(endstr);
        free(endstr);
    }
}