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
This commit is contained in:
BIN
6.process_and_signal/file_communication
Executable file
BIN
6.process_and_signal/file_communication
Executable file
Binary file not shown.
@@ -1,6 +1,5 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
@@ -9,6 +8,9 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
|
||||||
|
|
||||||
void sigusr1action (int signal){
|
void sigusr1action (int signal){
|
||||||
|
|
||||||
@@ -16,16 +18,64 @@ void sigusr1action (int signal){
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sigchldaction (int signal){
|
void sigusr1caction (int signal){
|
||||||
|
|
||||||
fprintf(stdout,"Parent:Ricevuto SIGCHLD\n");
|
|
||||||
|
|
||||||
|
|
||||||
|
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){
|
int main (int index, char** in_data){
|
||||||
|
|
||||||
|
if (index < 4 || index > 4) {
|
||||||
|
fprintf(stderr,"Parametri command line non validi\n");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
int duplicate;
|
int duplicate;
|
||||||
duplicate = fork();
|
duplicate = fork();
|
||||||
if (duplicate < 0)
|
if (duplicate < 0)
|
||||||
@@ -33,39 +83,70 @@ int main (int index, char** in_data){
|
|||||||
if (duplicate == 0){
|
if (duplicate == 0){
|
||||||
//child
|
//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());
|
fprintf(stdout,"Child:Process %d terminated\n",getpid());
|
||||||
|
|
||||||
}else{
|
}else{
|
||||||
// parent
|
// 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;
|
struct sigaction sigusr1structure;
|
||||||
sigusr1structure.sa_handler = sigusr1action;
|
sigusr1structure.sa_handler = sigusr1action;
|
||||||
sigusr1structure.sa_flags = 0;
|
sigusr1structure.sa_flags = 0;
|
||||||
|
|
||||||
|
|
||||||
struct sigaction sigchldstructure;
|
|
||||||
sigchldstructure.sa_handler = sigchldaction;
|
|
||||||
sigchldstructure.sa_flags = 0;
|
|
||||||
|
|
||||||
|
|
||||||
sigaction (SIGUSR1, &sigusr1structure, NULL);
|
sigaction (SIGUSR1, &sigusr1structure, NULL);
|
||||||
sigaction (SIGCHLD, &sigchldstructure, NULL);
|
|
||||||
|
|
||||||
int status;
|
int status;
|
||||||
int pid;
|
int pid;
|
||||||
|
|
||||||
for (pid = waitpid(-1,&status,0);pid > 0;pid = waitpid(-1,&status,0)){
|
kill (duplicate,SIGUSR1);
|
||||||
fprintf(stdout,"Parent:Child %d terminated\n",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());
|
}
|
||||||
|
|
||||||
|
fprintf(stdout,"Parent:Process %d terminated\n",getpid());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
2
6.process_and_signal/readme.txt
Normal file
2
6.process_and_signal/readme.txt
Normal file
@@ -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
|
||||||
2
6.process_and_signal/share_file
Executable file
2
6.process_and_signal/share_file
Executable file
@@ -0,0 +1,2 @@
|
|||||||
|
152 11 -
|
||||||
|
141
|
||||||
@@ -10,4 +10,4 @@ FLAG2 = -o
|
|||||||
clear:
|
clear:
|
||||||
rm -f -- per esempio
|
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
|
||||||
|
|||||||
13
da_vedere~
Normal file
13
da_vedere~
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user