fork.c
author Markus Brökers <mbroeker@largo.homelinux.org>
Mon, 30 Aug 2010 15:21:47 +0200
changeset 146 0ffa20c26b2d
parent 139 cb1d3f4cf18e
permissions -rw-r--r--
gauss.c: set a proper accuracy value: 1E-6

/**
 * fork.c
 * Copyright (C) 2008 Markus Broeker
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>

int fork_proc (char **argv, char **argp)
{
    int pid;
    char *cmd = argv[0];

    pid = fork ();
    switch (pid) {
    case 0:
        printf ("Starting new Process\n");
        execve (cmd, argv, argp);
        exit (EXIT_FAILURE);
    case -1:
        perror ("FORK");
        return errno;
    default:
        printf ("Pid started as %d\n", pid);
        wait (&pid);
        printf ("Exit-Status = %d\n", pid);

        return pid;
    }

    // should never happen
    return -1;
}

int main (int argc, char **argv)
{
    char *args[] = {
        "/bin/ls",
        "-al",
        NULL
    };

    if (fork_proc (args, NULL) == 0) {
        printf ("It just works :P\n");
    }

    return EXIT_SUCCESS;
}