File: procfd1.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-- 1,002 bytes parent folder | download | duplicates (3)
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/fcntl.h>
#include <sys/socket.h>

//This example mimics a 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[])
{
  char ch;
  const char* me;
  int fd = open("/proc/self/maps", O_RDONLY);
  if (fd == -1) {
    perror("open failed:");
    return 1;
  }


  if(fork()>0){
    me = "parent";
    while (1) {
      int ret = read(fd, &ch, 1);
      if (ret == 0) {
        lseek(fd, 0, SEEK_SET);
      } else if (ret == -1) {
        exit(0);
      }
      //printf("%s: %d\n", me, count++);
      //sleep(1);
    }
  }else{
    me = "child";
    while (1) {
      int ret = read(fd, &ch, 1);
      if (ret == 0) {
        lseek(fd, 0, SEEK_SET);
      } else if (ret == -1) {
        exit(0);
      }
      //printf("%s: %d\n", me, count++);
      sleep(1);
    }
  }

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