fork.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:50:39 +0200
changeset 71 9ac5a589211e
parent 56 966ad681f25d
child 77 49e0babccb23
permissions -rw-r--r--
Makefile: Little Improvements * not all platforms have gnu find * not all platforms have rm The user may adjust the settings in the header of the makefile committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 * test/demos/fork.c
 * Copyright (C) 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);
    case -1:
        perror ("FORK");
        return errno;
    default:
        printf ("Pid started as %d\n", pid);
        wait (&pid);
        printf ("Exit-Status = %d\n", pid);
        return pid;
    }
}

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