|
|
1.1 ! root 1: /* ! 2: * IPC benchmark, ! 3: * read and reply using UNIX domain datagram sockets. ! 4: * ! 5: * Process forks and exchanges messages using a ! 6: * UNIX domain datagram socket in a request-response fashion. ! 7: */ ! 8: ! 9: #include <stdio.h> ! 10: #include <signal.h> ! 11: ! 12: #include <sys/types.h> ! 13: #include <sys/socket.h> ! 14: #include <sys/time.h> ! 15: #include <sys/un.h> ! 16: #include <sys/resource.h> ! 17: ! 18: struct sockaddr_un sun; ! 19: struct sockaddr_un myname; ! 20: ! 21: int catchsig(); ! 22: char *malloc(); ! 23: ! 24: main(argc, argv) ! 25: char *argv[]; ! 26: { ! 27: register char *buf; ! 28: register int i, msgs = 0; ! 29: int msglen = 0, ms; ! 30: int pid, s, sunlen; ! 31: ! 32: if (argc < 3) { ! 33: printf("usage: %s #msgs msglen\n", argv[0]); ! 34: exit(1); ! 35: } ! 36: msgs = atoi(argv[1]); ! 37: msglen = atoi(argv[2]); ! 38: buf = malloc(msglen); ! 39: if (buf == 0) { ! 40: printf("Couldn't allocate data buffer\n"); ! 41: exit(1); ! 42: } ! 43: myname.sun_family = AF_UNIX; ! 44: signal(SIGINT, catchsig); ! 45: s = socket(AF_UNIX, SOCK_DGRAM, 0); ! 46: if (s < 0) { ! 47: perror("socket"); ! 48: exit(1); ! 49: } ! 50: sprintf(myname.sun_path, "unixdg%d", getpid()); ! 51: sunlen = strlen(myname.sun_path) + 2; ! 52: if (bind(s, &myname, sunlen) < 0) { ! 53: perror("bind"); ! 54: exit(1); ! 55: } ! 56: pid = fork(); ! 57: if (pid == 0) ! 58: for (i = 0; i < msgs; i++) { ! 59: sunlen = sizeof (sun); ! 60: if (recvfrom(s, buf, msglen, 0, &sun, &sunlen) < 0) ! 61: perror("recvfrom (child)"); ! 62: sunlen = strlen(myname.sun_path) + 2; ! 63: if (sendto(s, buf, msglen, 0, &myname, sunlen) < 0) ! 64: perror("sendto (child)"); ! 65: } ! 66: else ! 67: for (i = 0; i < msgs; i++) { ! 68: sunlen = strlen(myname.sun_path) + 2; ! 69: if (sendto(s, buf, msglen, 0, &myname, sunlen) < 0) ! 70: perror("sendto (parent)"); ! 71: sunlen = sizeof (sun); ! 72: if (recvfrom(s, buf, msglen, 0, &sun, &sunlen) < 0) ! 73: perror("recvfrom (parent)"); ! 74: } ! 75: close(s); ! 76: unlink(myname.sun_path); ! 77: } ! 78: ! 79: catchsig(s) ! 80: int s; ! 81: { ! 82: ! 83: unlink(myname.sun_path); ! 84: exit(1); ! 85: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.