la parte che gestisce le opzioni a riga di comando, è disponibile nel file
\file{ForkTest.c}.
-Decifrato il numero di figli da creare il ciclo principale del programma
+Decifrato il numero di figli da creare, il ciclo principale del programma
(\texttt{\small 28--40}) esegue in successione la creazione dei processi figli
-(\texttt{\small 29--31}) controllando il successo della chiamata a
-\func{fork}; ciascun figlio (\texttt{\small 29--31}) si limita a stampare il
+controllando il successo della chiamata a \func{fork} (\texttt{\small
+ 29--31}); ciascun figlio (\texttt{\small 29--31}) si limita a stampare il
suo numero di successione, attendere 2 secondi e scrivere un messaggio prima
di uscire. Il processo padre invece (\texttt{\small 29--31}) stampa un
-messaggio di creazione e procede nell'esecuzione del ciclo.
-
-In generale\footnote{anche se nel kernel 2.4.x è stato introdotto un
+messaggio di creazione e procede nell'esecuzione del ciclo. Se eseguiamo il
+comando otterremo:
+\begin{verbatim}
+[piccardi@selidor sources]$ ./forktest 5
+Test for forking 5 child
+Spawned 1 child, pid 840
+Child 1 successfully executing
+Child 2 successfully executing
+Spawned 2 child, pid 841
+Spawned 3 child, pid 842
+Child 3 successfully executing
+Spawned 4 child, pid 843
+Child 4 successfully executing
+Child 5 successfully executing
+Spawned 5 child, pid 844
+[piccardi@selidor sources]$ Child 2 exiting
+Child 1 exiting
+Child 4 exiting
+Child 3 exiting
+Child 5 exiting
+\end{verbatim}
+
+
+Come si vede non si può dire quale processo fra il padre ed il figlio venga
+eseguito per primo\footnote{anche se nel kernel 2.4.x era stato introdotto un
meccanismo che metteva in esecuzione sempre il xxx per primo (TODO
- recuperare le informazioni esatte)} non si può dire quale processo fra il
-padre ed il figlio venga eseguito per primo dopo la chiamata a \func{fork},
-per cui se i due processi devono essere sincronizzati occorre ricorrere ad un
-qualche meccanismo di intercomunicazione.
+ recuperare le informazioni esatte)} dopo la chiamata a \func{fork}, nel caso
+mostrato sopra ad esempio si può notare come dopo la creazione il secondo ed
+il quinto figlio sia stato stati eseguiti per primi, mantre per gli altri
+figli è stato eseguito per primo il padre. In generale l'ordine di esecuzione
+dipenderà dalla situazione particolare in si trova la macchina al momento
+della chiamata, risultando del tutto impredicibile, per questo motivo se i due
+processi devono essere sincronizzati occorrerà ricorrere ad un opportuno
+meccanismo di intercomunicazione.
+
+Si ricordi inoltre che, essendo i segmenti di memoria utilizzati dai singoli
+processi completamente separati, le modifiche delle variabili nei processi
+figli (come l'incremento di \var{i} in \texttt{\small 33}) saranno effettive
+solo per essi, e non hanno alcun effetto sul valore che le stesse variabili
+hanno nel processo padre.
+
+
\subsection{Le funzioni \texttt{wait} e \texttt{waitpid}}
/****************************************************************
*
* Program daytime:
- * Simple TCP client for daytime service (port 13)
+ * Elementary TCP client for daytime service (port 13)
*
* Author: Simone Piccardi
* Apr. 2001
*
* Usage: daytime -h give all info's
*
- * $Id: ElemDaytimeTCPClient.c,v 1.1 2001/09/09 17:39:15 piccardi Exp $
+ * $Id: ElemDaytimeTCPClient.c,v 1.2 2001/09/09 22:45:34 piccardi Exp $
*
****************************************************************/
/*
-/* ElemDaytimeTCPClient.c
+/* ElemDaytimeTCPServer.c
*
- * Copyright (C) 2000 Simone Piccardi
+ * Copyright (C) 2001 Simone Piccardi
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
-//****************************************************************
+/****************************************************************
*
* Program daytimed:
* Elementary TCP server for daytime service (port 13)
* Author: Simone Piccardi
* Apr. 2001
*
- * Usage: daytimed
+ * Usage: daytimed -h give all info
*
- * $Id: ElemDaytimeTCPServer.c,v 1.2 2001/09/09 17:39:15 piccardi Exp $
+ * $Id: ElemDaytimeTCPServer.c,v 1.3 2001/09/09 22:45:34 piccardi Exp $
*
****************************************************************/
/*
+/* ElemEchoTCPClient.c
+ *
+ * Copyright (C) 2001 Simone Piccardi
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
/****************************************************************
*
- * Program SimpleEchoTCPClient:
+ * Program ElemEchoTCPClient.c
* Simple TCP client for echo service (port 7)
*
* Author: Simone Piccardi
*
* Usage: echo -h give all info's
*
- * $Id: ElemEchoTCPClient.c,v 1.2 2001/06/20 22:03:37 piccardi Exp $
+ * $Id: ElemEchoTCPClient.c,v 1.3 2001/09/09 22:45:34 piccardi Exp $
*
****************************************************************/
/*
+/* ErrCode.c
+ *
+ * Copyright (C) 2001 Simone Piccardi
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
/****************************************************************
*
* Program ErrCode.c:
*
* Usage: errcode -h give all info's
*
- * $Id: ErrCode.c,v 1.1 2001/09/09 17:39:15 piccardi Exp $
+ * $Id: ErrCode.c,v 1.2 2001/09/09 22:45:34 piccardi Exp $
*
****************************************************************/
/*
+/* ForkTest.c
+ *
+ * Copyright (C) 2001 Simone Piccardi
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
/****************************************************************
*
* Program ForkTest.c:
*
* Usage: forktest -h give all info's
*
- * $Id: ForkTest.c,v 1.1 2001/09/09 17:39:15 piccardi Exp $
+ * $Id: ForkTest.c,v 1.2 2001/09/09 22:45:34 piccardi Exp $
*
****************************************************************/
/*
/* loop to fork children */
for (i=0; i<nchild; i++) {
if ( (pid = fork()) < 0) {
- printf("Error on %d child creation, %s\n", i, strerror(errno));
+ printf("Error on %d child creation, %s\n", i+1, strerror(errno));
}
if (pid == 0) { /* child */
- printf("Child %d successfully executing\n", i++);
+ printf("Child %d successfully executing\n", ++i);
sleep(2);
printf("Child %d exiting\n", i);
exit(0);
} else { /* parent */
- printf("Spawned %d child, pid %d \n", i, pid);
+ printf("Spawned %d child, pid %d \n", i+1, pid);
}
}
/* normal exit */
OBJ = SockRead.o SockWrite.o
+all: forktest errcode echo echod daytimed iterdaytimed daytime
+
forktest: ForkTest.c
$(CC) $(CFLAGS) $^ -o $@
.PHONY : clean
clean:
- rm -f daytime iterdaytimed daytimed echod echo errcode
+ rm -f daytime iterdaytimed daytimed echod echo errcode forktest
rm -f *~
rm -f *.o
+/* SimpleEchoTCPClient.c
+ *
+ * Copyright (C) 2001 Simone Piccardi
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
/****************************************************************
*
- * Program SimpleEchoTCPClient:
+ * Program echo:
* Simple TCP client for echo service (port 7)
*
* Author: Simone Piccardi
*
* Usage: echo -h give all info's
*
- * $Id: SimpleEchoTCPClient.c,v 1.3 2001/06/17 21:58:46 piccardi Exp $
+ * $Id: SimpleEchoTCPClient.c,v 1.4 2001/09/09 22:45:34 piccardi Exp $
*
****************************************************************/
/*
+/* SimpleEchoTCPServer.c
+ *
+ * Copyright (C) 2001 Simone Piccardi
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
/****************************************************************
*
- * Program echo_tcp_server.c:
+ * Program echod:
* Elementary TCP server for echo service (port 7)
*
* Author: Simone Piccardi
*
* Usage: echod
*
- * $Id: SimpleEchoTCPServer.c,v 1.3 2001/06/12 22:17:22 piccardi Exp $
+ * $Id: SimpleEchoTCPServer.c,v 1.4 2001/09/09 22:45:34 piccardi Exp $
*
****************************************************************/
/*
+/* SockRead.c
+ *
+ * Copyright (C) 2001 Simone Piccardi
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+/****************************************************************
+ *
+ * Routine SockRead
+ * Routine to read an exact number of bytes from a socket
+ *
+ * Author: Simone Piccardi
+ * Jun. 2001
+ *
+ * $Id: SockRead.c,v 1.3 2001/09/09 22:45:34 piccardi Exp $
+ *
+ ****************************************************************/
#include <unistd.h>
#include <errno.h>
+/* SockWrite.c
+ *
+ * Copyright (C) 2001 Simone Piccardi
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+/****************************************************************
+ *
+ * Routine SockWrite
+ * Routine to write an exact number of bytes into a socket
+ *
+ * Author: Simone Piccardi
+ * Jun. 2001
+ *
+ * $Id: SockWrite.c,v 1.3 2001/09/09 22:45:34 piccardi Exp $
+ *
+ ****************************************************************/
#include <unistd.h>
#include <errno.h>