fork.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Fri, 27 Dec 2013 16:00:09 +0100
changeset 167 7c6b221900bf
parent 139 cb1d3f4cf18e
permissions -rw-r--r--
Proper Datatypes to leave the 80's
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
77
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 56
diff changeset
     2
 * fork.c
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 56
diff changeset
     3
 * Copyright (C) 2008 Markus Broeker
56
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 */
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <stdio.h>
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdlib.h>
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <unistd.h>
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <sys/wait.h>
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
#include <errno.h>
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
int fork_proc (char **argv, char **argp)
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
{
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
    int pid;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
    char *cmd = argv[0];
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
    pid = fork ();
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
    switch (pid) {
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
    case 0:
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
        printf ("Starting new Process\n");
139
cb1d3f4cf18e execve returns a value on error and this catches it
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 138
diff changeset
    21
        execve (cmd, argv, argp);
cb1d3f4cf18e execve returns a value on error and this catches it
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 138
diff changeset
    22
        exit (EXIT_FAILURE);
56
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
    case -1:
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
        perror ("FORK");
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
        return errno;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
    default:
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
        printf ("Pid started as %d\n", pid);
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
        wait (&pid);
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
        printf ("Exit-Status = %d\n", pid);
138
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 77
diff changeset
    30
56
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
        return pid;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
    }
138
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 77
diff changeset
    33
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 77
diff changeset
    34
    // should never happen
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 77
diff changeset
    35
    return -1;
56
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
}
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
int main (int argc, char **argv)
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
{
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
    char *args[] = {
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
        "/bin/ls",
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
        "-al",
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
        NULL
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
    };
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
    if (fork_proc (args, NULL) == 0) {
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
        printf ("It just works :P\n");
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
    }
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
    return EXIT_SUCCESS;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
}