Friday, February 12, 2010

systems programming (UNIX, C)

How to toggle lettercases.

struct termios info;
    int   rv = tcgetattr(0, &info);
    if(info.c_oflag & OLCUC)
            info.c_oflag &=  ~OLCUC;
    else
        info.c_oflag |= OLCUC;
    tcsetattr(0, TCSANOW, &info);

______________________________________________________________

How can we measure CPU time ( user + kernel) of a program in UNIX.

1. Use clock() system call to measure CPU time.
      The clock() function returns the  amount  of  CPU  time  (in
     microseconds)  used  since  the first call to clock() in the
     calling process.
      example
          time_t time = clock();
          int i=0;
          while(i<10000){
              printf("%d\n", i);
              i++;
          }
         time = clock();
         printf( "\nCPU time : %ld\n" , time);// prints CPU time  taken  to print numbers from 0 to 999


2.  Use time command
     example
     bash$     time gcc file.c
3.  Use getitimer and setitimer with ITIMER_REALPROF

No comments:

Post a Comment