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

From Sistemi Operativi
Jump to navigation Jump to search
Line 26: Line 26:
 
}
 
}
  
/* Use fork to move eventfd form parent ot child */
+
/* Use fork to move eventfd form parent to child */
 
switch (fork()) {
 
switch (fork()) {
 
 

Revision as of 15:17, 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>

int main (int argc, char *argv[]) {
	
	int efd,i, j;
        /* If doesn't have argument */
	if (argc < 2) {
		fprintf(stderr, "Usage: %s<num>...\n", argv[0]);
		exit(EXIT_FAILURE);
	}
	
	/* 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()) {
		
		/* If move is right */
		case 0:

			/* First for is used for check until the last argument  */
			for (j = 1; j < argc; j++) {
				printf ("%s\n", argv[j]);

				/* Second for is used to print the 'x' of the argument passed by terminal */
				for (i = atoi(argv[j]); i > 0; i--) {
					printf ("x\n");
				}
			
			}
			exit(EXIT_SUCCESS);
		
		/* Check for error */
		case -1:
			perror("Fork Error"); 
			exit(EXIT_FAILURE);

		/* Wait child finish */
		default:
			wait();
			exit(EXIT_SUCCESS);	
	}
}