Prove pratiche 2022
Jump to navigation
Jump to search
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}");
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;
}