File: stale-fd.c

package info (click to toggle)
dmtcp 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,496 kB
  • sloc: cpp: 33,592; ansic: 28,099; sh: 6,735; makefile: 1,950; perl: 1,690; python: 1,241; asm: 138; java: 13
file content (65 lines) | stat: -rw-r--r-- 1,299 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>

//in this example, a disconnected fd is left open at checkpoint time
//child closes both sides of socketpair
//parent closes one side

int main(int argc, char* argv[])
{
  int count = 1;
  int sockets[2];
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0) {
    perror("socketpair()");
    return -1;
  }

  const char* me;

  if(fork()>0){
    //parent closes one
    close(sockets[0]);
    me = "parent";
    unsigned char data;
    unsigned char count = 0;
    while (1) {
      if (read(sockets[1], &data, 1) != 1) {
        perror("read failed");
        exit(1);
      };
      printf("%s %d\n", me, data);
      assert(count == data);
      count++;
      sleep(2);
    }
  }else{
    unsigned char buf[256];
    unsigned int i;
    for (i = 0; i < sizeof(buf); i++) {
      buf[i] = i;
    }
    printf("%s", buf);
    if (write(sockets[0], buf, sizeof(buf)) != sizeof(buf)) {
      perror("Write failed");
      exit(1);
    };

    //child closes both
    close(sockets[0]);
    close(sockets[1]);
    me = "child";
    while (1) {
      printf("%s %d\n", me, count++);
      sleep(2);
    }
  }


	printf("%s done\n",me);
  return 0;
}