linux 系统编程学习笔记二

来源:互联网 发布:淘宝的一分兑换在哪里 编辑:程序博客网 时间:2024/05/19 14:38


lseek
文件在打开时读写位置是0,如果是以O_APPEND方式打开,位置会移致动到文件末尾
#include <sys/types.h>
#include <unistd.h>
/*移动当前读写位置
  参数同fseek
  offset 偏移量
  whence 起始位置
 */
off_t lseek(int fd,off_t offset,int whence);


打开文件的当前偏移量
off_t currpos;
currpos=lseek(fd,0,SEEK_CUR);




fcntl
改变一个已打开的文件的属性,可以重新设置读、写、追加、非阻塞标志(file status 


flag)
#include <unistd.h>
#include <fcntl.h>


int fcntl(int fd,int cmd);
int fcntl(int fd,int cmd,long arg);
int fcntl(int fd,int cmd,struct flock *lock);


实现非阻塞读终端
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>


#define MSG_TRY "try again\n"


/* 使用F_GETFL 和F_SETFL两种fcntl命令改变STDIN_FILENO的属性*/
int main(void){
char buf[10];
int n;
int flags;

flags=fcntl(STDIN_FILENO,F_GETFL);
flags|=O_NONBLOCK;
if(fcntl(STDIN_FILENO,F_SETFL,flags)==-1){
perror("fcntl");
exit(1);
}
tryagain:
n=read();
if(n<0){
if(errno==EAGAIN){
sleep(1);
write(STDOUT_FILENO,MSG_TRY,strlen(MSG_TRY));
goto tryagain;
}
perror("read stdin");
exit(1);
}
write(STDOUT_FILENO,buf,n);
return 0;
}




#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>


/*通过命令行的第一个参数指定一个文件描述符,利用shell的重定向功能在
  该描述符上打开文件,然后用fcntl的F_GETFL命令取出file status flagv
 */
int main(int argc,char *argv[]){
int val;
if(argc!=2){
fputs("usage:a.out<descriptor#>\n",stderr);
exit(1);
}
if((val=fcntl(atoi(argv[1]),F_GETFL))<0){
printf("fcntl error for fd %d\n",atoi(argv[1]));
exit(1);
}
switch(val & O_ACCMODE){
case O_RDONLY:
printf("read only");
break;
case O_WRONLY:
printf("write only");
break;
case O_RDWR:
printf("read write");
break;
default:
fputs("invalid access mode\n",stderr);
exit(1);
}
if(val & O_APPEND)
printf(",append");
if(val & O_NONBLOCK)
printf(",nonblocking");
putchar('\n');
return 0;
}
运行结果:
yuezhenhua@ubuntu:/opt/sdk/tc$ ./a.out 0 </dev/tty
read only


ioctl
用于向设备发控制和配置命令,这些数据是不能通过read/write读写的(out-of-band)数据
ioctl发送的是控制信息,其中的数据是辅助数据
例如在串口线上收发数据通过read/wirte操作,串口的波特率、校验位、停止位通过ioctl设


置,A/D转换的结果通过read读取,而A/D转换的精度和工作频率通过ioctl设置


#include <sys/ioctl.h>
/*
  d文件描述符
  request是ioctl的命令
  可变参数是一个指向变量或结构体的指针
 */
int ioctl(int d,int request,...);




#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>


/*取得终端设备的窗口大小*/
int main(void){
struct winsize size;
if(isatty(STDOUT_FILENO)==0){
exit(1);
}
if(ioctl(STDOUT_FILENO,TIOCGWINSZ,&size)<0){
perror("ioctl TIOCGWINSZ error");
exit(1);
}
printf("%d rows,%d columns\n",size.ws_row,size.c4
运行结果:
yuezhenhua@ubuntu:/opt/sdk/tc$ gcc test44.c -o test44
yuezhenhua@ubuntu:/opt/sdk/tc$ ./test44
24 rows,80 columns
原创粉丝点击