2012年5月21日 星期一

Linux C Select() - multi input trace


fd_set set;
    FD_ZERO(&set);      /*clear set */
    FD_SET(fd, &set);   /*add fd into set */
    FD_CLR(fd, &set);   /*remove fd from set*/
    FD_ISSET(fd, &set); /*return true if fd is in set*/
------------------------------------------------------------------------
int maxfd = MAX (fd1, fd2)+1;

struct timeval tv;
tv.tv_usec = 0;
tv.tv_sec = 2;


while (1) {
     FD_SET(fd1, &readfs);  /* input 1*/
     FD_SET(fd2, &readfs);  /* input 2 */
     /* block until input becomes available */
     int rul = select(maxfd, &readfs, NULL, NULL,  &tv );


     if (FD_ISSET(fd1))         /* if input 1 has signal*/
       handle_input_from_source1();
     if (FD_ISSET(fd2))         /* if input 2 has signal */
       handle_input_from_source2();
/*
     if(rul == -1)
         return errorout;
     if(rul == 0)
        return timeout;
     
*/
   }

2012年5月20日 星期日

C address pointer scope notice

Define:
(A file) : char * tmp
(B  file): function(char *ptr)
             {
                  ptr = open("/tmpfile.txt","w");
             }
--------------------------------------------
(A file)  code:   call (B file)function( tmp )
                        fclose( tmp )            ==> address is null;
             
=============================
the pointer return from open("/tmpfile.txt","w") exist only in (B  file): function(char *address).

C fopen()

#include <stdio.h>
FILE *fopen(const char *restrict filename, const char *restrict mode);


mode is listed as following:

r or rb
Open file for reading.
w or wb
Truncate to zero length or create file for writing.
a or ab
Append; open or create file for writing at end-of-file.
r+ or rb+ or r+b
Open file for update (reading and writing).
w+ or wb+ or w+b
Truncate to zero length or create file for update.
a+ or ab+ or a+b
Append; open or create file for update, writing at end-of-file


reference from http://pubs.opengroup.org/onlinepubs/009695399/functions/fopen.html