前一段时间看apue的时候 对文件状态标志的操作就比较迷茫 因为是刚开始看, 所以就匆匆的略过了 今天看到高级IO部分,又涉及到这部分问题,所以先要将这个解决才能继续看下去。
首先拿open函数举例int open(const char *pathname, int oflag, ... );
返回值:成功则返回文件描述符,否则返回 -1
oflag参数所表示的就是文件状态标志所组成的集合,用来说明以什么样的状态打开pathname文件。而此集合是由多个常量的“或”运算构成的 :
O_RDONLY 只读打开
O_WRONLY 只写打开
O_RDWR 读·写打开
O_APPEND 写时追加
...
create函数可以用来创建一个新文件,open函数通过操作这些状态标志同样也可以达到此目的
create(const char* pathname, mode_t mode);
等价于
open(pathname, O_WRONLY | O_CREATE | O_TRUNC, mode);
该open函数中的oflag参数为三个文件状态标志用或运算组合在一起的
因为没有看过源码起初对这种模式感到很奇怪,谷歌了一下也没有得到满意的答案。
apue中有这样一段代码
//《Unix环境高级编程》程序3-4:打印指定的描述符的文件标志
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int val;
if( argc != 2 )
{
fprintf(stderr, "Usage: a.out <descriptor#>");
exit(1);
}
//改变已打开的文件的性质
val = fcntl( atoi(argv[1]), F_GETFL, 0);
if( val < 0 )
{
fprintf(stderr, "fcntl error for fd %d", 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:
fprintf(stderr, "unknow access mode");
exit(1);
}
if( val & O_APPEND )
printf(", append");
if( val & O_NONBLOCK )
printf(", nonblocking");
#if defined(O_SYNC)
if( val & O_SYNC )
printf(", synchronous writes");
#endif
#if !defined(_POSIX_C_SOURCE) && defined(O_FSYNC)
if( val & O_FSYNC )
printf(", synchronous writes");
#endif
putchar('\n');
return 0;
} 或操作
前面提到过文件状态标志就是由一个或多个常量(标志值)组成的
文件状态标志可以用一个二进制数表示 该二进制数的每一位都表示一个常量,二进制数每一位都可以为0或者为1.
为1就表示该常量被选中,为0表示未被选中
举个例子
假设
我是在安装mysql-server-5.5时,报错Sub-process /usr/bin/dpkg returned an error code (1),
全部卸载之前的所有以来的软件包,重新安装,还是报这个dpkg的错误,
sudo apt-get autoremove mysql-client-5.5 mysql-client-core-5.5 mysql-common mysql-server-5.5 mysql-server-core-5.5 libnet-daemon-perl libplrpc-perl libterm-readkey-perl libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18
sudo apt-get install mysql-client-5.5 mysql-client-core-5.5 mysql-common mysql-server-5.5 mysql-server-core-5.5 libnet-daemon-perl libplrpc-perl libterm-readkey-perl libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18
Sub-process /usr/bin/dpkg returned an error code (1)
$cd /var/lib/dpkg/info
$sudo rm -rf mysql-*
$sudo apt-get autoremove mysql-client-5.5 mysql-client-core-5.5 mysql-common mysql-server-5.5 mysql-server-core-5.5 libnet-daemon-perl libplrpc-perl libterm-readkey-perl libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18
$sudo apt-get install mysql-client-5.5 mysql-client-core-5.5 mysql-common mysql-server-5.5 mysql-server-core-5.5 libnet-daemon-perl libplrpc-perl libterm-readkey-perl libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18
$mysql
mysql> exit
Bye
OK了
也可以执行下面这个方法二:
$cd /var/lib/dpkg/info
$sudo rm -rf mysql*
$sudo apt-get -f install$sudo apt-get update
nginx配置的代码:
user www www;
worker_processes 8;
error_log /data111/logs/nginx/nginx-error.log crit;
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http
{
include mime.types;
default_type application/octet-stream;
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
server
{
listen 80;
server_name 110.1.09.116;
index index.html index.htm index.php;
root /data0/www1/html;
#limit_conn crawler 20;
if ($request_uri ~* "^/(static|data|\.)/.*"){
set $rule '1';
}
if ($rule !~ '1'){
rewrite ^/(.+)$ /index.php?/$1 last;
}
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /data111/logs/nginx/download-access.log access;
}
server
{
listen 80;
server_name status.sina.com;
location / {
stub_status on;
access_log off;
}
}
}
详细含义可以参考《Nginx配置与应用详解》专题http://developer.51cto.com/art/201004/194472.htm