fork.c
author Markus Brökers <mbroeker@largo.homelinux.org>
Tue, 17 Aug 2010 14:47:41 +0200
changeset 138 dff18d1ac2af
parent 77 49e0babccb23
child 139 cb1d3f4cf18e
permissions -rw-r--r--
Compatibility: We support Linux and BSD
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");
138
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 77
diff changeset
    21
        return execve (cmd, argv, argp);
56
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
    case -1:
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
        perror ("FORK");
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
        return errno;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
    default:
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
        printf ("Pid started as %d\n", pid);
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
        wait (&pid);
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
        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
    29
56
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
        return pid;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    }
138
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 77
diff changeset
    32
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 77
diff changeset
    33
    // should never happen
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 77
diff changeset
    34
    return -1;
56
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
}
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
int main (int argc, char **argv)
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
{
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
    char *args[] = {
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
        "/bin/ls",
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
        "-al",
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
        NULL
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
    };
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
    if (fork_proc (args, NULL) == 0) {
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
        printf ("It just works :P\n");
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
    }
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
    return EXIT_SUCCESS;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
}