LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1123|回复: 5

关于ioctl的问题

[复制链接]
发表于 2006-8-10 11:02:34 | 显示全部楼层 |阅读模式
大家好,我刚学习linux下的编程,用书上的例子实现了一个简单的ioctl的使用——
弹出CD,可是程序运行在ioctl的时候错误,错误信息如下:
[jackk@killua ch05]$ ./cdrom_eject /dev/hda
The file descriptor is 3:
The return value of the ioctl function is -1:
the error message is :Input/output error

程序源码:
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/ioctl.h>
#include<linux/cdrom.h>
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<errno.h>

int main( int argc, char* argv[] ){
  /*Open a file descriptor to the device specified on the command line.*/
  int tmp;
  int fd = open ( argv[1], O_RDONLY );
  printf( "The file descriptor is %d:\n", fd );
  /*Eject the CD-ROM.*/
  tmp = ioctl( fd, CDROMEJECT );
  printf( "The return value of the ioctl function is %d:\n", tmp );
  if( tmp == -1 ){
        fprintf( stderr, "the error message is :%s\n", strerror ( errno ) );
  }
  /*Close the file descriptor. */
  close ( fd );
  return 0;
}
 楼主| 发表于 2006-8-10 11:03:28 | 显示全部楼层
请教一下这个ioctl的使用是否正确,为什么会出现这样的错误,谢谢
回复 支持 反对

使用道具 举报

发表于 2006-8-11 11:52:33 | 显示全部楼层
        大家好,我刚学习linux下的编程,用书上的例子实现了一个简单的ioctl的使用——
弹出CD,可是程序运行在ioctl的时候错误,错误信息如下:
引用:
[jackk@killua ch05]$ ./cdrom_eject /dev/hda
The file descriptor is 3:
The return value of the ioctl function is -1:
the error message is :Input/output error


光驱一般是 /dev/hdc 吧。
你不是ioctl错误,你是设备没打开。

fd= open(argv[1], O_RDONLY|O_NONBLOCK);

用上面这句打开就OK了,因为光驱是块设备。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-8-11 15:39:32 | 显示全部楼层
Post by leros_H
光驱一般是 /dev/hdc 吧。
你不是ioctl错误,你是设备没打开。

fd= open(argv[1], O_RDONLY|O_NONBLOCK);

用上面这句打开就OK了,因为光驱是块设备。


恩,照你说的就可以了
不过我的光驱就是/dev/hda,,因为我的光驱在第一个IDE控制器上
我的硬盘是/dev/hdc,在第二个IDE控制器上
回复 支持 反对

使用道具 举报

发表于 2006-8-11 15:41:36 | 显示全部楼层
楼主还是加点判断配合 perror 着用吧.
回复 支持 反对

使用道具 举报

发表于 2006-8-11 23:53:15 | 显示全部楼层
O_NONBLOCK 为非阻塞标志。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表