Sistemi di Calcolo

Corso di Laurea in Ingegneria Informatica e Automatica - A.A. 2017-2018

HomePage | Avvisi | Diario lezioni | Programma | Materiale didattico | Esami | Forum | Login

Revision [3435]

Last edited on 2017-12-06 17:22:11 by EmilioCoppa
Additions:
if (res > 0)
Deletions:
if (res < s)


Revision [3420]

Edited on 2017-12-02 18:19:08 by EmilioCoppa
Additions:
// 0640: user => rw, group => r, others => nessuno
Deletions:
// 0644: user => rw, group => r, others => nessuno


Revision [3415]

Edited on 2017-11-29 18:25:22 by EmilioCoppa

No differences.

Revision [3414]

Edited on 2017-11-29 18:24:59 by EmilioCoppa
Additions:
Versione ##4##: implementazione comando interno ##mypwd## e ##echo##
for (i=1; argv[i]!=NULL; ++i) {
if (i > 1) printf(" ");
printf("%s", argv[i]);
printf("\n");
if (strcmp(myargv[0],"mypwd") == 0) {
if (strcmp(myargv[0], "echo") == 0) {
Deletions:
Versione ##4##: implementazione comando interno ##mypwd##
void get_cmd_line(char* myargv[]) {
char cmd[MAX_LINE_LEN];
for (i=1; i for (i=1; argv[i]!=NULL; ++i)
printf("%s\n",argv[i]);
get_cmd_line(myargv);
if (strcmp(myargv[0],"mypwd")==0) {
if (strcmp(myargv[0],"echo")==0) {


Revision [3413]

Edited on 2017-11-29 18:17:29 by EmilioCoppa
Deletions:
printf("argv[0] = %s\n", myargv[0]);


Revision [3412]

Edited on 2017-11-29 18:16:36 by EmilioCoppa
Additions:
Versione ##3d##: generalizzazione a comando con un numero arbitrario (ma limitato) di argomenti
Versione ##3e##: versione modularizzata
void get_cmd_line(char * cmd, char* myargv[]) {
if (myargv[0] == NULL) return;
for (i = 1; i < MAX_NUM_ARGS; i++) {
if (i == MAX_NUM_ARGS)
myargv[MAX_NUM_ARGS - 1] = NULL;
get_cmd_line(cmd, myargv);
printf("argv[0] = %s\n", myargv[0]);
Deletions:
Versione ##3c##: generalizzazione a comando con un numero arbitrario (ma limitato) di argomenti
Versione ##3d##: versione modularizzata


Revision [3411]

Edited on 2017-11-29 17:57:12 by EmilioCoppa
Additions:
cmd[strlen(cmd)-1] = '\0'; // man fgets: If a newline is read, it is stored into the buffer.


Revision [3410]

Edited on 2017-11-29 17:15:43 by EmilioCoppa
Additions:
Versione ##3a##: shell che prende comandi senza argomenti
Versione ##3b##: uso di ##fgets## al posto di ##scanf## (unsafe)
Versione ##3c##: comando con un argomento
Versione ##3c##: generalizzazione a comando con un numero arbitrario (ma limitato) di argomenti
Versione ##3d##: versione modularizzata
Versione ##4##: implementazione comando interno ##mypwd##
Deletions:
Shell che prende comandi senza argomenti:
Idem, con fgets invece di scanf:
Comando al più un argomento:
Generalizzazione a un numero arbitrario (ma limitato) di argomenti:
Versione modularizzata:
Aggiunta di comandi interni:


Revision [3409]

Edited on 2017-11-29 17:09:06 by EmilioCoppa
Additions:
Versione ##2## (gestione ##EINTR##):
Deletions:
Versione ##2##:


Revision [3408]

Edited on 2017-11-29 17:08:28 by EmilioCoppa
Additions:
if (res == 0) break; // EOF: read indica la fine del file utilizzando la costante zero
if (write(STDOUT_FILENO, buffer, res) == -1) { // vedere esempio su write per gestire anche qui EINTR correttamente
Deletions:
if (res == 0) break; // EOF
if (write(STDOUT_FILENO, buffer, res) == -1) {


Revision [3407]

Edited on 2017-11-29 17:04:13 by EmilioCoppa
Additions:
int flags = O_WRONLY | O_CREAT | O_TRUNC;
mode_t mode = 0640;
ssize_t byte_scritti = 0;
ssize_t byte_da_scrivere = sizeof(text); // sizeof funziona correttamente solo text e' dichiarato come array e non un puntatore ad una stringa!
printf("Byte da scrivere: %d\n", byte_da_scrivere);
while (byte_scritti < byte_da_scrivere) {
ssize_t res = write(fd, text + byte_scritti, sizeof(text) - byte_scritti);
if (res == -1) { // errore
// analizziamo la causa dell'errore
switch (errno) {
case EINTR:
// se e' arrivato un segnale, non e' un errore fatale possiamo ritentare la scrittura
continue;
break;
default:
// consideriamo le altre cause come errori fatali non recuperabili
perror("errore write");
exit(EXIT_FAILURE);
}
byte_scritti += res;
printf("Byte scritti: %d\n", res);
Deletions:
int fd = open("test.txt", O_WRONLY | O_CREAT | O_TRUNC, 0640);
if (write(fd, text, sizeof(text)) != sizeof(text)) { // verifica che abbiamo scritto scritto tutto quello che volevamo scrivere


Revision [3406]

Edited on 2017-11-29 16:40:27 by EmilioCoppa
Additions:
Versione ##1##:
#include // perror
int flags = O_WRONLY | O_CREAT | O_TRUNC; // O_WRONLY: file aperto in sola scrittura
// O_CREAT: se il file non esiste, verra' creato
// O_TRUNC: se il file esiste, trancare il contenuto
// se il file non esiste, allora dobbiamo indicare i permessi
mode_t mode = 0640; // Una costante che inizia con 0 (e non 0x) in C e' in notazione ottale
// 0644: user => rw, group => r, others => nessuno
// alternativa ad usare numero ottale: usare le costanti definite da POSIX
// mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP; // corrisponde a 0644
int fd = open("test.txt", flags, mode);
Versione ##2##:
Deletions:
= Versione 1=
= Versione 2=


Revision [3405]

Edited on 2017-11-29 16:24:50 by EmilioCoppa
Additions:
if (write(fd, text, sizeof(text)) != sizeof(text)) { // verifica che abbiamo scritto tutto quello che volevamo scrivere


Revision [3404]

Edited on 2017-11-29 16:23:37 by EmilioCoppa
Additions:
#include
#define USE_PERROR 0 // vedere anche quando: valore 1
#if USE_PERROR
perror("causa errore pause");
#else
switch (errno) {
case EINTR:
printf("Interrotta da segnale.\n");
break;
// pause ammette solo EINTR come errore, vedere: man pause
// quindi SOLO per questa system call ha poco considerare casi errno != EINTR
default:
perror("causa errore pause");
#endif
Deletions:
if (res == -1) perror("causa errore pause");


Revision [3403]

Edited on 2017-11-29 16:12:16 by EmilioCoppa
Additions:
res = pause();
printf("return value pause: %d\n", res);
if (res == -1) perror("causa errore pause");
===System call ##open##, ##read##, ##write##, ##close##===
= Versione 1=

if (write(fd, text, sizeof(text)) != sizeof(text)) { // verifica che abbiamo scritto scritto tutto quello che volevamo scrivere
// se non e' cosi', terminiamo...

= Versione 2=

if (write(fd, text, sizeof(text)) != sizeof(text)) { // verifica che abbiamo scritto scritto tutto quello che volevamo scrivere
// se non e' cosi', terminiamo...
Deletions:
// installiamo gestore per segnale SIGINT (generato ca CTRL+C)
res = pause(); // premendo CTRL+C si esce dalla system call, poiché la ricezione di un segnale non ignorato provoca l'uscita dalla system call
printf("%d\n", res);
perror("dopo la pause");
===System call ##open##, ##read##, ##close##===
if (write(fd, text, sizeof(text)) != sizeof(text)) {


Revision [3402]

Edited on 2017-11-29 15:58:42 by EmilioCoppa
Additions:
===System call ##open##, ##read##, ##close##===


Revision [3401]

Edited on 2017-11-29 15:57:46 by EmilioCoppa
Additions:
===System call interrotte da segnali===
Deletions:
==System call interrotte da segnali==


Revision [3400]

Edited on 2017-11-29 15:57:29 by EmilioCoppa
Additions:
== Esempio: gestore SIGCHLD, interruzione system call==
%%(c;sigaction-3.c)
void myhandler(int signo) {
printf("figlio e' terminato\n");
struct sigaction sa = {0};
sa.sa_handler = myhandler;
int res = sigaction(SIGCHLD, &sa, NULL);
printf("[pid=%d] genitore\n", getpid());
pid_t pid = fork();
if (pid == -1) {
perror("errore nella fork");
exit(EXIT_FAILURE);
if (pid == 0) { // processo figlio
printf("[pid=%d] figlio\n", getpid());
_exit(EXIT_SUCCESS);
int s = 30;
res = sleep(s); // genitore potrebbe fare qualcosa di piu' utile
// in questo esempio invoca una system call
if (res < s)
printf("Genitore termina: sleep terminata prematuramente dopo %d secondi\n", s - res);
else
printf("Genitore termina: sleep eseguita interamente per %d secondi\n", s);
== Esempio: gestore SIGINT, interruzione system call ##pause##==


Revision [3391]

Edited on 2017-11-28 19:46:00 by CamilDemetrescu
Additions:
==Scrittura di file==
char text[] = "This is a test\n";
int fd = open("test.txt", O_WRONLY | O_CREAT | O_TRUNC, 0640);
if (write(fd, text, sizeof(text)) != sizeof(text)) {
perror("Error in write");


Revision [3390]

The oldest known version of this page was created on 2017-11-28 19:45:17 by CamilDemetrescu
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki
Page was generated in 0.2374 seconds