File: shared-fd1.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 (51 lines) | stat: -rw-r--r-- 993 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>

// This example mimics a DMTCP bug in some versions of MPICH2 mpd.
// The parent process forgets to close one end of a socket pair.

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;
  int fd;
  char buf[32];

  if(fork()>0){
    //parent
    // close(sockets[1]); <= forgot to do this
    me = "parent";
    fd = sockets[0];

    while (1)
    {
      sprintf(buf, "%s(%d)", me, count++);
      if(write(fd, buf, sizeof(buf)) != sizeof(buf)) break;
      sleep(2);
    }
  }else{
    //child
    close(sockets[0]);
    me = "child";
    fd = sockets[1];

    while (1)
    {
      if(read(fd, buf, sizeof(buf)) != sizeof(buf)) break;
      printf("%s read: %s\n", me, buf);
    }
  }

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