Ricerca…
Sintassi
- pid_t getpid (void);
- pid_t getppid (void);
- pid_t fork (void);
- pid_t waitpid (pid pid, int * wstatus, opzioni int);
- int execv (const char * percorso, char * const argv []);
Parametri
| Funzione, parametro (i), valore di ritorno | Descrizione | 
|---|---|
| fork() | nome della funzione | 
| nessuna | n / A | 
| Restituisce PID, 0 o -1 | Il processo chiamante riceve il PID del nuovo processo di creazione o -1 in caso di errore. Il bambino (il processo appena creato) riceve 0. In caso di errore, impostare errnosuEAGAINoENOMEM | 
| - | - | 
| execv() | nome della funzione | 
| const char *path | Stringa contenente il nome dell'eseguibile (potrebbe includere il percorso) | 
| char *const argv[] | Matrice del puntatore a stringa come argomento | 
| Restituisce -1 in caso di fallimento | In caso di successo questa funzione non torna. | 
| - | - | 
Crea un processo figlio e attendi fino alla sua uscita
 Questo programma mostra come eseguire un altro processo usando fork() e aspetta la sua terminazione usando waitpid() : 
- fork()crea una copia identica del processo corrente. Il processo originale è il processo padre, mentre quello appena creato è il processo figlio. Entrambi i processi continuano esattamente dopo il- fork().
- waitpid()blocca fino a quando il processo figlio non termina o termina e restituisce il codice di uscita e il motivo di terminazione.
#include <unistd.h>     /* for fork(), getpid() */
#include <sys/types.h>  /* for waitpid() */
#include <sys/wait.h>   /* for waitpid() */
#include <stdlib.h>     /* for exit() */
#include <stdio.h>      /* for printf(), perror() */
int
main(int argc, char *argv[])
{
    /* Create child process.
     *
     * On success, fork() returns the process ID of the child (> 0) to the
     * parent and 0 to the child. On error, -1 is returned.
     */
    pid_t child_pid = fork();
    if (child_pid < 0) {
        perror("fork() failed");
        exit(EXIT_FAILURE);
    } else if (child_pid == 0) {
        /* Print message from child process.
         *
         * getpid() returns the PID (process identifier) of current process,
         * which is typically int but doesn't have to be, so we cast it.
         *
         * getppid() returns the PID of the parent process.
         */
        printf("from child: pid=%d, parent_pid=%d\n",
                 (int)getpid(), (int)getppid());
        /* We can do something here, e.g. load another program using exec().
         */
        exit(33);
    } else if (child_pid > 0) {
        /* Print message from parent process.
         */
        printf("from parent: pid=%d child_pid=%d\n",
                  (int)getpid(), (int)child_pid); 
        
        /* Wait until child process exits or terminates.
         *
         * The return value of waitpid() is PID of the child process, while
         * its argument is filled with exit code and termination reason.
         */
        int status;
        pid_t waited_pid = waitpid(child_pid, &status, 0);
        if (waited_pid < 0) {
            perror("waitpid() failed");
            exit(EXIT_FAILURE);
        } else if (waited_pid == child_pid) {
            if (WIFEXITED(status)) {
                /* WIFEXITED(status) returns true if the child has terminated 
                 * normally. In this case WEXITSTATUS(status) returns child's
                 * exit code.
                 */
                printf("from parent: child exited with code %d\n",
                          WEXITSTATUS(status));
            }
        }
    }
    exit(EXIT_SUCCESS);
}
Esempio di output:
from parent: pid=2486 child_pid=2487
from child: pid=2487, parent_pid=2486
from parent: child exited with code 33
Copiato da qui , originariamente creato da M.Geiger.
Modified text is an extract of the original Stack Overflow Documentation
        Autorizzato sotto CC BY-SA 3.0
        Non affiliato con Stack Overflow