1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <sys/time.h>
    4. #include <time.h>
    5. #include <sched.h>
    6. #include <sys/types.h>
    7. #include <unistd.h>
    8. //pipe()
    9. int main()
    10. {
    11. int x, i, fd[2], p[2];
    12. char send = 's';
    13. char receive;
    14. pipe(fd);
    15. pipe(p);
    16. struct timeval tv;
    17. struct sched_param param;
    18. param.sched_priority = 0;
    19. while ((x = fork()) == -1);
    20. if (x==0)
    21. {
    22. sched_setscheduler(getpid(), SCHED_FIFO, &param);
    23. gettimeofday(&tv, NULL);
    24. printf("Before Context Switch Time%u s, %u usn", tv.tv_sec, tv.tv_usec);
    25. for (i = 0; i < 10000; i++)
    26. {
    27. read(fd[0], &receive, 1);
    28. write(p[1], &send, 1);
    29. }
    30. exit(0);
    31. } else
    32. {
    33. sched_setscheduler(getpid(), SCHED_FIFO, &param);
    34. for (i = 0; i < 10000; i++)
    35. {
    36. write(fd[1], &send, 1);
    37. read(p[0], &receive, 1);
    38. }
    39. gettimeofday(&tv, NULL);
    40. printf("After Context SWitch Time%u s, %u usn", tv.tv_sec, tv.tv_usec);
    41. }
    42. return 0;
    43. }