Chinaunix首页 | 论坛 | 博客
  • 博客访问: 346307
  • 博文数量: 92
  • 博客积分: 2500
  • 博客等级: 少校
  • 技术积分: 960
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-21 19:38
文章分类

全部博文(92)

文章存档

2010年(71)

2009年(21)

我的朋友

分类: 嵌入式

2009-08-22 21:12:46

Reference: "Linux application development technology "

Reference Site: http://www.ibm.com/developerworks/cn/linux/kernel/syscall/part1/index.html

   In general, the process can not access the kernel, it can not access the kernel memory space and the kernel function can not be deployed, however the system call is an exception to these rules, which is determined by the CPU hardware. The principle is that the process of kernel Jump to a location. Check this location in the system call number, and report on core processes of the service request is. Then, it re-find the system call table sys_call_table, want to call to find the address of the kernel function and the function call.

#include
#include    
_syscall1(time_t,time,time_t *,tloc)   
main()
{
        time_t the_time;
        the_time=time((time_t *)0);
        printf("The time is %ld\n",the_time);
}
System call time() function to calculate the number of seconds from GMT at 0:00 on January 1, 1970 to the present.
This is the most standard form of system calls, macro _syscall1 () function to start to get a prototype, but a matter of fact, if the program changed to the following example, the program can also run the same result.
#include
main()
{
        time_t the_time;
        the_time=time((time_t *)0);
        printf("The time is %ld\n",the_time);
}

Defined in the unistd.h in seven macros are

_syscall0(type,name)
_syscall1(type,name,type1,arg1)
_syscall2(type,name,type1,arg1,type2,arg2)
_syscall3(type,name,type1,arg1,type2,arg2,type3,arg3)
_syscall4(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4)
_syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,type5,arg5)
_syscall6(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,type5,arg5,type6,arg6)

They don't look like macro, but its essence is the same with the format like that:
  # define MAXSIZE 100 
which there is no difference between the MAXSIZE.
Their role is that forming the corresponding function prototype for system calls   in the procedure. We are easily found in the law, _syscall behind the numbers and typeN, argN number as many. In fact, _syscall followed by the number specified after the start of the formation function of the number of parameters, let us look at an example of the time which is just used system call: 
_syscall1 (time_t, time, time_t *, tloc)
 

 
time_t   time(time_t *   tloc)
{
    long __res;
    __asm__ volatile("int $0x80" : "=a" (__res) : "0" (13),"b" ((long)(tloc)));
    do {
        if ((unsigned long)(__res) >= (unsigned long)(-125)) {
            errno = -(__res);
            __res  = -1;
        }
        return (time_t) (__res);
    } while (0) ;
}

As can be seen, _syscall1 (time_t, time, time_t *, tloc) started as a function of time, time_t is a function of the original parameters of the return type, the original and time_t * parameters, respectively, constitute a new tloc function parameters. In fact, the procedure used in the prototype function of time is that.
 
Commonly used system call *fcntl() int fcntl(int fd, int cmd); int fcntl(int fd,int cmd,long arg); int fcntl(int fd,int cmd,struct flock * lock);
fcntl () used to operate some features in file descriptor, for example calling the fcntl to set O_NONBLOCK signs, so that the file descriptor into non-blocking mode.
 
*open()
 int open(const char * pathname, int flages);
 int open(const char * pathname,int flages, mode_t mode);
open() used to open the assigned path file
 
*creat()
 int creat(const char * pathname, mode_tmode);
creat()used to create a file and return file descriptor
*int close(int fd);

*ssize_t read(int fd, void * buf,size_t count);


*ssize_t write(int fd,void * buf,size_t count);


*ssize_t writev(int fildes, const struct iovec *iov, int iovcnt);  write the data in the buffer array into document

*ssize_t readv(int fildes,const struct iovec * iov,int iovcnt);     read the data from document to buffer array

*off_t lseek(int fildes,off_t offset ,int whence);
 used to local the read or write position.


 
阅读(1114) | 评论(0) | 转发(0) |
0

上一篇:Loadable Modules

下一篇:Linux standard I/O flow

给主人留下些什么吧!~~