Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- void func0() {
- printf( "Fonction 0\n" );
- }
- void func1() {
- printf( "Fonction 1\n" );
- }
- void func2() {
- printf( "Fonction 2\n" );
- }
- int forkn( void ( *array_ptrfunc[] )( void ), int nb_fils, pid_t *pid_array ) {
- int child_done = 0;
- for ( int i = 0; i < nb_fils; i++ ) {
- pid_t fils = fork();
- if ( fils == 0 ) {
- array_ptrfunc[ i ]();
- exit( 0 );
- } else if ( fils > 0) {
- pid_array[ i ] = fils;
- child_done++;
- }
- }
- return ( child_done );
- }
- #define NB_POINTERS 3
- int main( int argc, char** argv ) {
- int nb_fils = 3;
- pid_t ( *pid_array[NB_POINTERS]);
- void ( *array_ptrfunc[] )() = { func0, func1, func2 };
- array_ptrfunc[ 0 ] = func0;
- array_ptrfunc[ 1 ] = func1;
- array_ptrfunc[ 2 ] = func2;
- int res = forkn( &array_ptrfunc, nb_fils, &pid_array );
- for ( int i = 0; i < 10; i++) {
- wait( pid_array[ i ] );
- }
- printf( "Nombre de fils créés : %d\n", res );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement