File: fork.c

package info (click to toggle)
python-ptrace 0.9.9-0.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 788 kB
  • sloc: python: 10,167; ansic: 263; makefile: 164
file content (62 lines) | stat: -rw-r--r-- 1,039 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <stdlib.h>

int parent_pid;

void parent(pid_t child_pid)
{
    int status;
    int result;

    printf("[parent:%i] wait for child exit\n", parent_pid);
    result = waitpid(child_pid, &status, 0);
    if (result != child_pid) {
        perror("waitpid");
        exit(1);
    }

    printf("[parent:%i] child exited with status %i\n",
        parent_pid, status);
}

void child()
{
    struct utsname buf;
    int err;
    int pid = getpid();

    printf("[child:%i] uname()\n", pid);

    err = uname(&buf);
    if (err) {
        perror("uname");
        exit(1);
    }

    printf("[child:%i] done, exit.\n", pid);
}

int main()
{
    pid_t pid;

    /* fork process */
    parent_pid = getpid();
    printf("[parent:%i] fork\n", parent_pid);
    pid = fork();
    if (pid < 0) {
        perror("fork");
        exit(1);
    }

    if (pid) {
        parent(pid);
    } else {
        child();
    }
    return 0;
}