fork.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Tue, 12 May 2009 12:06:26 +0200
changeset 91 1181deef3bd6
parent 77 49e0babccb23
child 138 dff18d1ac2af
permissions -rw-r--r--
fixed a typo in mem2swap.c committer: Markus Bröker <mbroeker@largo.homelinux.org>
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");
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
        execve (cmd, argv, argp);
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);
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
        return pid;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
    }
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
}
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
int main (int argc, char **argv)
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
{
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
    char *args[] = {
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
        "/bin/ls",
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
        "-al",
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
        NULL
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
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
    if (fork_proc (args, NULL) == 0) {
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
        printf ("It just works :P\n");
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
    return EXIT_SUCCESS;
966ad681f25d Short Description
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
}