Zoeken…


Syntaxis

  • pid_t getpid (void);
  • pid_t getppid (void);
  • pid_t fork (void);
  • pid_t waitpid (pid_t pid, int * wstatus, int opties);
  • int execv (const char * path, char * const argv []);

parameters

Functie, parameter (s), retourwaarde Beschrijving
fork() functienaam
geen n / a
Retourneert PID, 0 of -1 Het aanroepende proces ontvangt de PID van het nieuwe proces of -1 bij mislukking. Het kind (de nieuwe werkwijze) ontvangt 0. Bij niet ingesteld errno ofwel EAGAIN of ENOMEM
- -
execv() functienaam
const char *path String met de naam van naar uitvoerbaar (kan pad bevatten)
char *const argv[] Matrix van tekenreeksaanwijzer als argumenten
Retourneert -1 bij mislukking Bij succes keert deze functie niet terug.
- -

Maak een onderliggend proces en wacht tot het wordt afgesloten

Dit programma laat zien hoe een ander proces kan worden uitgevoerd met behulp van fork() en hoe het kan worden beëindigd met waitpid() :

  • fork() maakt een identieke kopie van het huidige proces. Het oorspronkelijke proces is het bovenliggende proces, terwijl het nieuw gemaakte proces het onderliggende proces is. Beide processen gaan precies door na de fork() .

  • waitpid() blokkeert totdat het onderliggende proces wordt afgesloten of beëindigd en de waitpid() en reden voor de beëindiging retourneert.

#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);
}

Voorbeelduitgang:

from parent: pid=2486 child_pid=2487
from child: pid=2487, parent_pid=2486
from parent: child exited with code 33

Overgenomen uit hier , oorspronkelijk gemaakt door M.Geiger.



Modified text is an extract of the original Stack Overflow Documentation
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow