Merge branch 'master' of bitbucket.org:xgiovio/reti
Conflicts: da_vedere test/test.c
This commit is contained in:
BIN
3.nome_cognome_string/nc
Executable file
BIN
3.nome_cognome_string/nc
Executable file
Binary file not shown.
80
3.nome_cognome_string/nc.c
Normal file
80
3.nome_cognome_string/nc.c
Normal file
@@ -0,0 +1,80 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
int main (int index, char** in_data){
|
||||
|
||||
char buf[100];
|
||||
char bufn1[100];
|
||||
char bufn2[100];
|
||||
int num1,num2,sum;
|
||||
|
||||
|
||||
fprintf (stdout,"Digitare 2 numeri separati da uno spazio\n");
|
||||
fscanf (stdin, "%99[^\n]%*c",buf);
|
||||
sscanf (buf,"%s %s", bufn1, bufn2);
|
||||
num1 = atoi(bufn1);
|
||||
num2 = atoi(bufn2);
|
||||
sum = num1 + num2;
|
||||
|
||||
fprintf(stdout,"La somma e' %d\n", sum);
|
||||
|
||||
|
||||
|
||||
fprintf (stdout,"Digitare un nome\n");
|
||||
fscanf (stdin, "%99[^\n]%*c",bufn1);
|
||||
fprintf (stdout,"Digitare un cognome\n");
|
||||
fscanf (stdin, "%99[^\n]",bufn2);
|
||||
|
||||
char bufsum[200];
|
||||
|
||||
|
||||
|
||||
strcpy(bufsum, bufn1);
|
||||
strcat(bufsum, "-");
|
||||
strcat(bufsum, bufn2);
|
||||
|
||||
fprintf(stdout,"%s\n",bufsum);
|
||||
|
||||
|
||||
strcpy(bufsum, bufn2);
|
||||
strcat(bufsum, "-");
|
||||
strcat(bufsum, bufn1);
|
||||
fprintf(stdout,"%s\n",bufsum);
|
||||
|
||||
|
||||
if (strcmp(bufn1,bufn2) == 0){
|
||||
fprintf(stdout,"Stringhe uguali\n");
|
||||
} else {
|
||||
fprintf(stdout,"Stringhe diverse\n");
|
||||
}
|
||||
|
||||
|
||||
char iniziali[3];
|
||||
iniziali[0] = bufn1[0];
|
||||
iniziali[1] = bufn2[0];
|
||||
iniziali[2] = 0;
|
||||
|
||||
|
||||
fprintf(stdout,"Iniziali %s\n",iniziali);
|
||||
|
||||
char code[150];
|
||||
char sumc[150];
|
||||
sprintf (sumc,"%d",sum);
|
||||
|
||||
strcpy(code, iniziali);
|
||||
strcat(code, sumc);
|
||||
|
||||
fprintf(stdout,"Codice %s\n",code);
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
1
3.nome_cognome_string/readme.txt
Normal file
1
3.nome_cognome_string/readme.txt
Normal file
@@ -0,0 +1 @@
|
||||
il programma legge in in input prima due numeri restituendo la somma, poi un nome e congome formando diverse concatenazioni
|
||||
BIN
4.file_read_count/copycount
Executable file
BIN
4.file_read_count/copycount
Executable file
Binary file not shown.
87
4.file_read_count/copycount.c
Normal file
87
4.file_read_count/copycount.c
Normal file
@@ -0,0 +1,87 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
|
||||
int main (int index, char** in_data){
|
||||
|
||||
|
||||
int fd,fd2,ridden,wrote, tot,vocali,cifre,righe;
|
||||
tot = vocali = cifre = righe = 0;
|
||||
char buf;
|
||||
fd = open ("sample",O_RDONLY);
|
||||
fd2 = open ("sample_copied", O_CREAT | O_WRONLY,00700);
|
||||
if (fd < 0){
|
||||
fprintf(stderr,"Error reading file %d\n",errno);
|
||||
exit(-1);
|
||||
}
|
||||
if (fd2 < 0){
|
||||
fprintf(stderr,"Error creating file %d\n",errno);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
for (ridden = read (fd,&buf,1) ; ridden == 1 ;ridden = read (fd,&buf,1)){
|
||||
|
||||
|
||||
if (isdigit(buf) != 0){
|
||||
cifre++; tot++;
|
||||
wrote = write (fd2,&buf,1);
|
||||
if (wrote != 1){
|
||||
fprintf(stderr,"Error writing to file %d\n",errno);
|
||||
exit(-1);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (buf == 'a' || buf == 'e' || buf == 'i' || buf == 'o' || buf == 'u'){
|
||||
vocali++;tot++;
|
||||
wrote = write (fd2,&buf,1);
|
||||
if (wrote != 1){
|
||||
fprintf(stderr,"Error writing to file %d\n",errno);
|
||||
exit(-1);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (buf == '\n'){
|
||||
righe++;tot++;
|
||||
wrote = write (fd2,&buf,1);
|
||||
if (wrote != 1){
|
||||
fprintf(stderr,"Error writing to file %d\n",errno);
|
||||
exit(-1);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
tot++;
|
||||
wrote = write (fd2,&buf,1);
|
||||
if (wrote != 1){
|
||||
fprintf(stderr,"Error writing to file %d\n",errno);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
if (ridden < 0){
|
||||
fprintf(stderr,"Error reading file %d\n",errno);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
fprintf (stdout, "Numero vocali %d\n", vocali);
|
||||
fprintf (stdout, "Numero righe %d\n", righe);
|
||||
fprintf (stdout, "Numero cifre %d\n", cifre);
|
||||
fprintf (stdout, "Numero caratteri %d\n", tot);
|
||||
|
||||
close(fd);close(fd2);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
1
4.file_read_count/readme.txt
Normal file
1
4.file_read_count/readme.txt
Normal file
@@ -0,0 +1 @@
|
||||
il programma legge il file sample e lo ricopia in sample_copied contango righe,vocali,numeri e caratteri
|
||||
6
4.file_read_count/sample
Normal file
6
4.file_read_count/sample
Normal file
@@ -0,0 +1,6 @@
|
||||
aafdsfdsfdfdfdfdf
|
||||
4343
|
||||
fdsd342
|
||||
fdfdsfFJSNJNDJS
|
||||
dfdsfa
|
||||
|
||||
6
4.file_read_count/sample_copied
Executable file
6
4.file_read_count/sample_copied
Executable file
@@ -0,0 +1,6 @@
|
||||
aafdsfdsfdfdfdfdf
|
||||
4343
|
||||
fdsd342
|
||||
fdfdsfFJSNJNDJS
|
||||
dfdsfa
|
||||
|
||||
6
4.file_read_count/sample~
Normal file
6
4.file_read_count/sample~
Normal file
@@ -0,0 +1,6 @@
|
||||
fdsfdsfdfdfdfdf
|
||||
4343
|
||||
fdsd342
|
||||
fdfdsfFJSNJNDJS
|
||||
dfdsf
|
||||
|
||||
BIN
5.list_file_directory/list
Executable file
BIN
5.list_file_directory/list
Executable file
Binary file not shown.
85
5.list_file_directory/list.c
Normal file
85
5.list_file_directory/list.c
Normal file
@@ -0,0 +1,85 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
|
||||
int main (int index, char** in_data){
|
||||
|
||||
if (index < 2){
|
||||
fprintf(stderr,"Please specify a directory into command line\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (index > 2){
|
||||
fprintf(stderr,"Please enter only 1 path\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
|
||||
char * path;
|
||||
path = *(in_data + 1);
|
||||
|
||||
DIR * directory;
|
||||
directory = opendir (path);
|
||||
if ( directory == NULL){ fprintf(stderr, "Not a directory\n"); exit(-1);}
|
||||
|
||||
|
||||
struct dirent * dir_entry;
|
||||
struct stat file_stat;
|
||||
char full_path [2048];
|
||||
|
||||
int fd,ridden;
|
||||
char buf[10];
|
||||
|
||||
for(dir_entry = readdir(directory);
|
||||
dir_entry != NULL;
|
||||
dir_entry = readdir(directory)){
|
||||
|
||||
strcpy (full_path,path);
|
||||
strcat (full_path,"/");
|
||||
strcat (full_path,dir_entry->d_name);
|
||||
|
||||
if (stat (full_path,&file_stat) != 0 ) {fprintf(stderr, "Error retrieving file info\n"); exit(-1);}
|
||||
|
||||
fprintf (stdout,"Name %20s",dir_entry->d_name);
|
||||
fprintf (stdout," / ");
|
||||
fprintf (stdout,"Inode %10d",file_stat.st_ino);
|
||||
fprintf (stdout," / ");
|
||||
fprintf (stdout,"Size %10d",file_stat.st_size);
|
||||
fprintf (stdout," / ");
|
||||
|
||||
if (S_ISREG (file_stat.st_mode)){
|
||||
fprintf (stdout,"%23s","Regular File / ");
|
||||
fd = open (full_path,O_RDONLY);
|
||||
if (fd < 0){
|
||||
fprintf(stderr,"Error reading file %s %d\n",dir_entry->d_name,errno);
|
||||
exit(-1);
|
||||
}
|
||||
ridden = read (fd,buf,10);
|
||||
buf[ridden] = 0;
|
||||
fprintf (stdout,"%10s",buf);
|
||||
|
||||
close (fd);
|
||||
} else {
|
||||
fprintf (stdout,"%23s","Not a Regular File / ");
|
||||
|
||||
}
|
||||
fprintf (stdout,"\n");
|
||||
|
||||
}
|
||||
|
||||
if (errno != 0){
|
||||
fprintf(stderr, "Error reading in the directory\n"); exit(-1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
1
5.list_file_directory/readme.txt
Normal file
1
5.list_file_directory/readme.txt
Normal file
@@ -0,0 +1 @@
|
||||
il programma legge da liena di ocmando il percorso di una directory e fornisce in output statistiche relative ai file: nome,inode,grandezza in byte, check se il file e' regolare e in tal caso i primi 10 caratteri
|
||||
BIN
6.process_and_signal/file_communication
Executable file
BIN
6.process_and_signal/file_communication
Executable file
Binary file not shown.
153
6.process_and_signal/file_communication.c
Normal file
153
6.process_and_signal/file_communication.c
Normal file
@@ -0,0 +1,153 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
|
||||
void sigusr1action (int signal){
|
||||
|
||||
fprintf(stdout,"Parent:Ricevuto SIGUSR1\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)
|
||||
exit(-1);
|
||||
if (duplicate == 0){
|
||||
//child
|
||||
|
||||
|
||||
|
||||
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;
|
||||
|
||||
|
||||
|
||||
sigaction (SIGUSR1, &sigusr1structure, NULL);
|
||||
|
||||
int status;
|
||||
int pid;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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
|
||||
BIN
6.process_and_signal/waitc
Executable file
BIN
6.process_and_signal/waitc
Executable file
Binary file not shown.
72
6.process_and_signal/waitc.c
Normal file
72
6.process_and_signal/waitc.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
|
||||
void sigusr1action (int signal){
|
||||
|
||||
fprintf(stdout,"Parent:Ricevuto SIGUSR1\n");
|
||||
|
||||
}
|
||||
|
||||
void sigchldaction (int signal){
|
||||
|
||||
fprintf(stdout,"Parent:Ricevuto SIGCHLD\n");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main (int index, char** in_data){
|
||||
|
||||
int duplicate;
|
||||
duplicate = fork();
|
||||
if (duplicate < 0)
|
||||
exit(-1);
|
||||
if (duplicate == 0){
|
||||
//child
|
||||
|
||||
int numero;
|
||||
fscanf(stdin,"%d",&numero);
|
||||
kill (getppid(),SIGUSR1);
|
||||
fprintf(stdout,"Child:Process %d terminated\n",getpid());
|
||||
|
||||
}else{
|
||||
// parent
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ clear:
|
||||
rm -f -- per esempio
|
||||
|
||||
|
||||
|
||||
struct ind_addr
|
||||
struct sockaddr_in castato in struct sockaddr
|
||||
|
||||
@@ -25,3 +24,5 @@ funzione int socket()..
|
||||
esercizio con scambo dati tra due processi mediante file e comunicazione mediante segnali
|
||||
|
||||
|
||||
=======
|
||||
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
|
||||
0
test/gino~
Executable file
0
test/gino~
Executable file
19
test/test.c
19
test/test.c
@@ -1,18 +1,25 @@
|
||||
#include <ctype.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
int main (int index, char** in_data){
|
||||
|
||||
int fd;
|
||||
int data;
|
||||
void * buf;
|
||||
buf = malloc (100);
|
||||
|
||||
fd = open ("gino",O_WRONLY | O_APPEND,00700);
|
||||
lseek (fd,2,SEEK_SET);
|
||||
write (fd,(void *)"ehi",3);
|
||||
|
||||
|
||||
|
||||
|
||||
close(fd);
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user