msg_send_libos.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <shim_unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/ipc.h>
  6. #include <sys/msg.h>
  7. #include <sys/time.h>
  8. #include <sys/types.h>
  9. #include <sys/wait.h>
  10. #include <unistd.h>
  11. #define PAYLOAD_SIZE 4
  12. struct msgbuf {
  13. long mtype;
  14. char mtext[PAYLOAD_SIZE];
  15. };
  16. #define TEST_TIMES 1000
  17. #define DO_BENCH 1
  18. int msqid;
  19. /* server always sends messages */
  20. void server(void) {
  21. struct timeval tv1, tv2;
  22. struct msgbuf buf;
  23. int i;
  24. buf.mtype = 1;
  25. gettimeofday(&tv1, NULL);
  26. for (i = 0; i < TEST_TIMES; i++) {
  27. if (msgsnd(msqid, &buf, PAYLOAD_SIZE, 0) < 0) {
  28. perror("msgsnd");
  29. exit(1);
  30. }
  31. #ifndef DO_BENCH
  32. printf("Message: \"%s\" sent\n", buf.mtext);
  33. #endif
  34. }
  35. msgpersist(msqid, MSGPERSIST_STORE);
  36. gettimeofday(&tv2, NULL);
  37. printf("time spent on %d msgsnd: %llu microsecond\n", TEST_TIMES,
  38. (tv2.tv_sec * 1000000ull + tv2.tv_usec) - (tv1.tv_sec * 1000000ull + tv1.tv_usec));
  39. }
  40. /* client always sends messages */
  41. void client(void) {
  42. struct timeval tv1, tv2;
  43. struct msgbuf buf;
  44. int ret;
  45. gettimeofday(&tv1, NULL);
  46. msgpersist(msqid, MSGPERSIST_LOAD);
  47. for (int i = 0; i < TEST_TIMES; i++) {
  48. if ((ret = msgrcv(msqid, &buf, PAYLOAD_SIZE, 1, 0)) < 0) {
  49. perror("msgrcv");
  50. exit(1);
  51. }
  52. #ifndef DO_BENCH
  53. buf.mtext[ret] = 0;
  54. printf("Client received: \"%s\"\n", buf.mtext);
  55. #endif
  56. }
  57. gettimeofday(&tv2, NULL);
  58. printf("time spent on %d msgrcv: %llu microsecond\n", TEST_TIMES,
  59. (tv2.tv_sec * 1000000ull + tv2.tv_usec) - (tv1.tv_sec * 1000000ull + tv1.tv_usec));
  60. msgctl(msqid, IPC_RMID, NULL);
  61. }
  62. int main(int argc, char** argv) {
  63. if ((msqid = msgget(IPC_PRIVATE, 0600 | IPC_CREAT)) < 0) {
  64. perror("msgget");
  65. exit(1);
  66. }
  67. server();
  68. client();
  69. return 0;
  70. }