diff --git a/6.process_and_signal/file_communication b/6.process_and_signal/file_communication new file mode 100755 index 0000000..d8b49ca Binary files /dev/null and b/6.process_and_signal/file_communication differ diff --git a/6.process_and_signal/file_communication.c b/6.process_and_signal/file_communication.c index 3ab6823..2a1265f 100644 --- a/6.process_and_signal/file_communication.c +++ b/6.process_and_signal/file_communication.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -9,6 +8,9 @@ #include #include #include +#include +#include + void sigusr1action (int signal){ @@ -16,16 +18,64 @@ void sigusr1action (int signal){ } -void sigchldaction (int signal){ - - fprintf(stdout,"Parent:Ricevuto SIGCHLD\n"); - +void sigusr1caction (int signal){ + fprintf(stdout,"Child:Ricevuto SIGUSR1\n"); + int fd; + fd = open ("share_file", O_RDWR | O_APPEND); + if (fd < 0){ + fprintf(stderr,"Child: Error reading file - Errno%d\n",errno); + kill(getpid(),SIGKILL); + } + char a [100]; + char b [100]; + char op; + lseek(fd,0,SEEK_SET); + char data[2]; + data[1]=0; + char input[100]=""; + for (read(fd,data,1);data[0]!='\n';read(fd,data,1)){ + strcat(input,data); + } + sscanf(input,"%s%*c%s%*c%c",a,b,&op); + + int a_data, b_data,calcolo; + char calcolo_s[100]; + + a_data= atoi(a); + b_data = atoi(b); + + if (op=='+'){ + calcolo = a_data + b_data; + }else{ + if (op=='-'){ + calcolo = a_data - b_data; + }else{ + if (op=='*'){ + calcolo = a_data * b_data; + }else{ + + calcolo = a_data / b_data; + + } + } + } + sprintf(calcolo_s,"%d",calcolo); + write (fd,calcolo_s,strlen(calcolo_s)); + + kill(getppid(),SIGUSR1); } + + int main (int index, char** in_data){ + if (index < 4 || index > 4) { + fprintf(stderr,"Parametri command line non validi\n"); + exit(-1); + } + int duplicate; duplicate = fork(); if (duplicate < 0) @@ -33,39 +83,70 @@ int main (int index, char** in_data){ if (duplicate == 0){ //child - int numero; - fscanf(stdin,"%d",&numero); - kill (getppid(),SIGUSR1); + + + struct sigaction sigusr1cstructure; + sigusr1cstructure.sa_handler = sigusr1caction; + sigusr1cstructure.sa_flags = 0; + + + + sigaction (SIGUSR1, &sigusr1cstructure, NULL); + pause(); + fprintf(stdout,"Child:Process %d terminated\n",getpid()); }else{ // parent + int fd; + fd = open ("share_file", O_CREAT | O_WRONLY | O_EXCL,00700); + if (fd < 0){ + fprintf(stderr,"Parent:Share_file already exists\n"); + fd = open ("share_file", O_WRONLY | O_TRUNC); + if (fd < 0){ + fprintf(stderr,"Parent:Error writing to file - Err no%d\n",errno); + char cmd [100] = "kill -P "; + char pid [10]; + sprintf (pid,"%d",getpid()); + strcat(cmd,pid); + system(cmd); + + } + } + + write (fd,*(in_data+1),strlen(*(in_data+1))); + write (fd," ",1); + write (fd,*(in_data+2),strlen(*(in_data+2))); + write (fd," ",1); + write (fd,*(in_data+3),strlen(*(in_data+3))); + write (fd,"\n",1); + + close(fd); + + struct sigaction sigusr1structure; sigusr1structure.sa_handler = sigusr1action; sigusr1structure.sa_flags = 0; - - struct sigaction sigchldstructure; - sigchldstructure.sa_handler = sigchldaction; - sigchldstructure.sa_flags = 0; sigaction (SIGUSR1, &sigusr1structure, NULL); - sigaction (SIGCHLD, &sigchldstructure, NULL); int status; int pid; - for (pid = waitpid(-1,&status,0);pid > 0;pid = waitpid(-1,&status,0)){ - fprintf(stdout,"Parent:Child %d terminated\n",pid); - } - - fprintf(stdout,"Parent:Process %d terminated\n",getpid()); + kill (duplicate,SIGUSR1); + + for (pid = waitpid(-1,&status,0);pid > 0;pid = waitpid(-1,&status,0)){ + fprintf(stdout,"Parent:Child %d terminated\n",pid); + } + + fprintf(stdout,"Parent:Process %d terminated\n",getpid()); + } - - + return 0; } diff --git a/6.process_and_signal/readme.txt b/6.process_and_signal/readme.txt new file mode 100644 index 0000000..e22bd09 --- /dev/null +++ b/6.process_and_signal/readme.txt @@ -0,0 +1,2 @@ +waitc : il parent crea un figlio. Quest'ultimo riceve un numero da terminale e subito manda un sigusr1 al padre. Il parent attende un usr1 o un sigchld e poi termina. +file_communication : il parent crea un figlio e si comportano come un client server. Da linea di comando si passano tre paramentri: 2 numeri e una operazione. Esempio: 10 5 + . Le operazioni supportate sono + - * /. il file share_file viene utilizzato come file di sharing in cui il server mette i dati e il client la risposta. Il server informa il clinet mediane un usr1 qunado i file sono pronti mentre il client risponde con un usr1 al parent qunado ha completato l'operazione \ No newline at end of file diff --git a/6.process_and_signal/share_file b/6.process_and_signal/share_file new file mode 100755 index 0000000..8bccfef --- /dev/null +++ b/6.process_and_signal/share_file @@ -0,0 +1,2 @@ +152 11 - +141 \ No newline at end of file diff --git a/da_vedere b/da_vedere index c40d1d3..fdbb81f 100644 --- a/da_vedere +++ b/da_vedere @@ -10,4 +10,4 @@ FLAG2 = -o clear: rm -f -- per esempio -vedi union, esercizi stringa, file, little e bigendian, dati esadecimali. +piu file di giannino per i socket, vedi anche fli fopen,etc sugli stream diff --git a/da_vedere~ b/da_vedere~ new file mode 100644 index 0000000..72c4b58 --- /dev/null +++ b/da_vedere~ @@ -0,0 +1,13 @@ +nel make file +@echo compilazione di $< in $@ + +FLAG1= -c + +FLAG2 = -o + + + +clear: + rm -f -- per esempio + +piu file di giannino per i socket