Difference between revisions of "Esercizio 1, prova pratica 29.05.2013"

From Sistemi Operativi
Jump to navigation Jump to search
Line 10: Line 10:
 
#include <stdlib.h>
 
#include <stdlib.h>
 
#include <stdio.h>
 
#include <stdio.h>
 +
#include <stdint.h>
  
 
int main (int argc, char *argv[]) {
 
int main (int argc, char *argv[]) {
 
 
int efd,i, j;
+
int efd, i;  
        /* If doesn't have argument */
+
long long unsigned int val;
if (argc < 2) {
+
    ssize_t s;
fprintf(stderr, "Usage: %s<num>...\n", argv[0]);
 
exit(EXIT_FAILURE);
 
}
 
 
 
 
/* Check for eventfd error */
 
/* Check for eventfd error */
Line 29: Line 27:
 
switch (fork()) {
 
switch (fork()) {
 
 
/* If move is right */
 
 
case 0:
 
case 0:
 +
/* Read parent event */
 +
s = read(efd, &val, sizeof(long long unsigned int));
  
/* First for is used for check until the last argument  */
+
            if (s != sizeof(long long unsigned int)) {
for (j = 1; j < argc; j++) {
+
                perror("Read Error");
printf ("%s\n", argv[j]);
+
exit(EXIT_FAILURE);
 +
        }
  
/* Second for is used to print the 'x' of the argument passed by terminal */
+
/* For is used to print the 'x' of the argument passed by terminal */
for (i = atoi(argv[j]); i > 0; i--) {
+
for (i = (int)val; i > 0; i--) {
printf ("x\n");
+
printf ("x\n");
}
 
 
 
}
 
}
 +
 
exit(EXIT_SUCCESS);
 
exit(EXIT_SUCCESS);
 
 
Line 49: Line 48:
 
exit(EXIT_FAILURE);
 
exit(EXIT_FAILURE);
  
/* Wait child finish */
 
 
default:
 
default:
wait();
+
/* Wait for number */
exit(EXIT_SUCCESS);
+
printf ("Inserisci un intero diverso da 0: \n");
 +
scanf ("%llu", &val);
 +
 
 +
/* Write parent event */
 +
          s = write(efd, &val, sizeof(long long unsigned int));
 +
 
 +
            if (s != sizeof(long long unsigned int)) {
 +
                perror("Write Error");
 +
exit(EXIT_FAILURE);
 +
        }
 +
 +
exit(EXIT_SUCCESS);
 
}
 
}
 
}
 
}
 
 
</source>
 
</source>

Revision as of 20:45, 23 March 2015

Scrivere un programma testeventfd che faccia uso della system call eventfd.
In particolare il programma deve eseguire una fork, quando l'utente digita un numero letto dal processo padre, il processo
figlio deve stampare un numero uguale di x.

Soluzione di Pierg

#include <sys/eventfd.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h> 

int main (int argc, char *argv[]) {
	
	int efd, i; 
	long long unsigned int val;
    	ssize_t s;
	
	/* Check for eventfd error */
	efd = eventfd(0, 0);
	if (efd == -1) {
		perror("Eventfd Error"); 
	}

	/* Use fork to move eventfd form parent to child */
	switch (fork()) {
		
		case 0:
			/* Read parent event */
			s = read(efd, &val, sizeof(long long unsigned int));

            		if (s != sizeof(long long unsigned int)) {
                		perror("Read Error");
				exit(EXIT_FAILURE);
        		}

			/* For is used to print the 'x' of the argument passed by terminal */
			for (i = (int)val; i > 0; i--) {
				printf ("x\n");
			}

			exit(EXIT_SUCCESS);
		
		/* Check for error */
		case -1:
			perror("Fork Error"); 
			exit(EXIT_FAILURE);

		default:
			/* Wait for number */
			printf ("Inserisci un intero diverso da 0: \n");
			scanf ("%llu", &val);

			/* Write parent event */
           		s = write(efd, &val, sizeof(long long unsigned int));

            		if (s != sizeof(long long unsigned int)) {
                		perror("Write Error");
				exit(EXIT_FAILURE);
        		}
	
			exit(EXIT_SUCCESS);
	}
}