Linux System ProgrammingÔĶÁ±Ê¼ÇÖ® read(....)
¹ØÓÚread(...)·µ»ØÖµµÄÕýÈ·Åжϣºp30
File I/O µÄ read(...)º¯ÊýÓ÷¨£º
ÓÐÎÊÌâµÄ´úÂ룬ֻÅжϷµ»ØÖµÎª-1µÄÇé¿ö¡£
unsigned long word;
ssize_t nr;
/* read a couple bytes into 'word' from 'fd' */
nr = read (fd, &word, sizeof (unsigned long));
if (nr == -1)
/* error */
Indeed, a call to read( ) can result in many possibilities:
• The call returns a value equal to len. All len read bytes are stored in buf. The
results are as intended.
• The call returns a value less than len, but greater than zero. The read bytes are
stored in buf. This can occur because a signal interrupted the read midway, an
error occurred in the middle of the read, more than zero, but less than len bytes’
worth of data was available, or EOF was reached before len bytes were read.
Reissuing the read (with correspondingly updated buf and len values) will read the
remaining bytes into the rest of the buffer, or indicate the cause of the problem.
• The call returns 0. This indicates EOF. There is nothing to read.
• The call blocks because no data is currently available. This won’t happen in nonblocking
mode.
• The call returns -1, and errno is set to EINTR. This indicates that a signal was
received before any bytes were read. The call can be reissued.
• The call returns -1, and errno is set to EAGAIN. This indicates that the read would
block because no data is currently available, and that the request should be reissued
later. This happens only in nonblocking mode.
• The call returns -1, and errno is set to a value other than EINTR or EAGAIN. This
indicates a more serious error.
ÕýÈ·×ö·¨£º
ssize_t ret;
while (len != 0 && (ret = read (fd, buf, len)) != 0) {
if (ret == -1) {
if (errno == EINTR)
continue;
perror ("read");
break;
}
len -= ret;
buf += ret;
}
Ïà¹ØÎĵµ£º
Ò»£ºÇ°ÑÔ
×î½üÔÚÑо¿androidµÄsensor driver£¬Ö÷ÒªÊÇE-compass£¬ÆäÖÐÓõ½ÁËLinux input×Óϵͳ.ÔÚÍøÉÏÒ²¿´Á˺ܶàÕâ·½ÃæµÄ×ÊÁÏ£¬¸Ð¾õ»¹ÊÇÕâÆª·ÖÎöµÄ±È½ÏϸÖÂ͸³¹£¬Òò´Ë×ªÔØÒ»ÏÂÒÔ±ã×Ô¼ºÑ§Ï°£¬Í¬Ê±ºÍ´ó¼Ò·ÖÏí£¡
£¨ÕâÆª²©¿ÍÖ÷ÒªÊÇÒÔ¼üÅÌÇý¶¯ÎªÀýµÄ£¬²»¹ý½²½âµÄÊÇLinux Input Subsystem£¬¿ÉÒÔ×ÐϸµÄÑо¿Ò»Ï£¡£©
¼üÅÌÇý¶¯½«¼ì ......
Ò»£®Ìî¿ÕÌ⣺
1. ÔÚLinuxϵͳÖУ¬ÒÔÎļþ·½Ê½·ÃÎÊÉ豸 ¡£
2. LinuxÄÚºËÒýµ¼Ê±£¬´ÓÎļþ/etc/fstabÖжÁȡҪ¼ÓÔØµÄÎļþϵͳ¡£
3. LinuxÎļþϵͳÖÐÿ¸öÎļþÓÃi½ÚµãÀ´±êʶ¡£
4. È«²¿´ÅÅÌ¿éÓÉËĸö²¿·Ö×é³É£¬·Ö±ðΪÒýµ¼¿é ¡¢×¨ÓÃ¿é ¡¢ i½Úµã±í¿é ºÍÊý¾Ý´æ´¢¿é¡£
5. Á´½Ó·ÖΪ£ºÓ²Á´½Ó ºÍ ·ûºÅÁ´½Ó¡£
6. ³¬¼¶¿é°üº¬ÁËi½Úµã±í ......
´íÎóÓ¡ÏóºÍÈÏʶÂÞÁÐÈçÏ£¬Ò»Ò»½âÊÍ£º
1¡£linuxϵÄÈí¼þÌ«ÉÙ
»Ø´ð£ºlinux ϵÄÈí¼þÒ»µãÒ²²»ÉÙ¡£windows»¹ÔÚÄï¶Ç×ÓÀïµÄʱºò£¬UnixÒѾÈçÈÕÖÐÌìÁË¡£ÒªÖªµÀ΢Èí¹«Ë¾¿ª·¢µÄµÚÒ»¸ö²Ù×÷ϵͳÊÇʲôÂð£¿ÊÇÒ»¸ö½Ð×öXenixµÄ¶«Î÷£¬ÊÇUnixµÄÒ»¸ö·ÖÖ§£¬ºóÀ´²ÅÈ¥¸ãDOSµÄ¡£ÓÐÈËÓÖÎÊÁË£¬Unix²»ÊÇLinux°¢£¬ÒªÖªµÀ£¬LinuxÍêÈ«ÖØÐµÄʵÏÖÁËUni ......
Ò»£®ºËÐÄÔ´³ÌÐòµÄÎļþ×éÖ¯£º
1£®LinuxºËÐÄÔ´³ÌÐòͨ³£¶¼°²×°ÔÚ/usr/src/linuxÏ£¬¶øÇÒËüÓÐÒ»¸ö·Ç³£¼òµ¥µÄ±àºÅÔ¼¶¨£ºÈκÎżÊýµÄºËÐÄ£¨ÀýÈç2.0.30£©¶¼ÊÇÒ»¸öÎȶ¨µØ·¢ÐеĺËÐÄ£¬¶øÈÎºÎÆæÊýµÄºËÐÄ£¨ÀýÈç2.1.42£©¶¼ÊÇÒ»¸ö¿ª·¢ÖеĺËÐÄ¡£
±¾ÎÄ»ùÓÚÎȶ¨µÄ2.2.5Ô´´úÂ룬µÚ¶þ²¿·ÖµÄʵÏÖÆ½Ì¨Îª Redhat Linux 6.0¡£
2£®ºËÐÄÔ´³ÌÐò ......
#include<unistd.h>
¡¡¡¡¶¨Ò庯Êý£º
¡¡¡¡int execvp(const char *file ,char * const argv []);
¡¡¡¡º¯Êý˵Ã÷£º
¡¡¡¡execvp()»á´ÓPATH »·¾³±äÁ¿ËùÖ¸µÄĿ¼ÖвéÕÒ·ûºÏ²ÎÊýfile µÄÎļþÃû£¬ÕÒµ½ºó±ãÖ´ÐиÃÎļþ£¬È»ºó½«µÚ¶þ¸ö²ÎÊýargv´«¸ø¸ÃÓûÖ´ÐеÄÎļþ¡£
¡¡¡¡·µ»ØÖµ£º
¡¡¡¡Èç¹ûÖ´Ðгɹ¦Ôòº¯Êý²»»á·µ»Ø£¬Ö´ÐÐʧ°ÜÔ ......