File: mq_fork.c

package info (click to toggle)
lsof 4.99.4%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,924 kB
  • sloc: ansic: 50,680; sh: 8,351; makefile: 1,194; perl: 940; awk: 214
file content (43 lines) | stat: -rw-r--r-- 945 bytes parent folder | download
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
#include <fcntl.h>    /* For O_* constants */
#include <sys/stat.h> /* For mode constants */
#include <sys/types.h>
#include <sys/wait.h>
#include <mqueue.h>
#include <unistd.h>
#include <stdio.h>

int main(int argc, char **argv) {
    int pid;
    char *fname;

    if (argc != 2) {
        fprintf(stderr, "wrong number of arguments: %d\n", argc);
        return 1;
    } else if (argv[1][0] != '/') {
        fprintf(stderr, "name must starts with '/': %c\n", argv[1][0]);
        return 1;
    }

    fname = argv[1];
    mqd_t t = mq_open(fname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, NULL);
    ;

    if (t == -1) {
        perror("mq_open");
        return 1;
    }

    pid = fork();
    if (pid == 0)
        pause();
    else if (pid > 0) {
        printf("%d %d %d\n", getpid(), pid, t);
        fflush(stdout);
        wait(NULL);
        mq_unlink(fname);
    } else {
        perror("fork");
        return 1;
    }
    return 0;
}