|   当前位置: Home > Linux > 设备驱动 >  文章 | 
	        | 
	     
	     
	      | 
	        
		  
		   Linux设备驱动编程之阻塞与非阻塞 
		  
		
               | 
             
             
              |  
                文章来源: 天极软件 
		文章作者: 宋宝华 
		发布时间: 2006-10-22
		  
		字体: 
		  [大
		  中
		  小]
		  
               | 
	     
	     
	      | 
	           
		
		
		  
		     | 
		    
		    
                    
 
 
    
        
            #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <fcntl.h> main() {  int fd, num;
   fd = open("/dev/globalvar", O_RDWR, S_IRUSR | S_IWUSR);  if (fd != - 1)  {   while (1)   {    read(fd, &num, sizeof(int)); //程序将阻塞在此语句,除非有针对globalvar的输入    printf("The globalvar is %d\n", num);
     //如果输入是0,则退出    if (num == 0)    {     close(fd);     break;    }   }  }  else  {   printf("device open failure\n");  } } | 
         
    
 
  写的程序为: 
 
    
        
            #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <fcntl.h> main() {  int fd, num;
   fd = open("/dev/globalvar", O_RDWR, S_IRUSR | S_IWUSR);  if (fd != - 1)  {   while (1)   {    printf("Please input the globalvar:\n");    scanf("%d", &num);    write(fd, &num, sizeof(int));
     //如果输入0,退出    if (num == 0)    {     close(fd);     break;    }   }  }  else  {   printf("device open failure\n");  } } | 
         
    
 
		    
                      
		      
		      
		   | 
		 
               
	     | 
	   
	  
	    | 
	      ↑返回顶部
	       
	      打印本页
	       
	      关闭窗口↓
	       
	     | 
	   
	  
	    
	       
	      
	      
	      
             | 
           
         
        | 
       
        
	  
	  
          
          
         
         
         
         
         
         
         
         
         
         
         
         
	  
        |