Linux ProgramingLinux Programing
System callsSystem calls
System call thường là một yêu cầu đến hệ điều System call thường là một yêu cầu đến hệ điều
hành để làm một tác vụ phần cứng/chuyên biệt hành để làm một tác vụ phần cứng/chuyên biệt
hệ thống hay tác vụ đặc quyền hệ thống . hệ thống hay tác vụ đặc quyền hệ thống .
Trong LinuxTrong Linux 1.2 có 140 system calls được định 1.2 có 140 system calls được định
nghĩa. nghĩa.
System calls như System calls như close()close() được hiện thực trong được hiện thực trong
Linux libc. Linux libc.
Việc hiện thực này xoay quanh việc gọi một là Việc hiện thực này xoay quanh việc gọi một là
syscall().syscall().
Các tham số truyền vào syscall() là số hiệu Các tham số truyền vào syscall() là số hiệu
system call và được theo sau bởi các tham số system call và được theo sau bởi các tham số
cần thiết cần thiết
System callsSystem calls
Số hiệu của system call có tìm thể thấy ở Số hiệu của system call có tìm thể thấy ở
< linux/unistd.h > < linux/unistd.h >
Trong đó < sys/syscall.h > được cập nhật Trong đó < sys/syscall.h > được cập nhật
với libc mới.với libc mới.
Nếu có các calls xuất hiện mà chúng chưa Nếu có các calls xuất hiện mà chúng chưa
có trong libc, ta có thể gọi syscall().có trong libc, ta có thể gọi syscall().
System callsSystem calls
Ví dụ, ta có thể đóng file dùng syscall() như sau Ví dụ, ta có thể đóng file dùng syscall() như sau
(không khuyến khích):(không khuyến khích):
#include <syscall.h>#include <syscall.h>
extern int syscall(int, );extern int syscall(int, );
int my_close(int filedescriptor)int my_close(int filedescriptor)
{{
return syscall(SYS_close, filedescriptor);return syscall(SYS_close, filedescriptor);
}}
System call ParametersSystem call Parameters
The “swiss army knife” ioctlThe “swiss army knife” ioctl
phillips screwdriver, screwdriver,
screwdriver, screwdriver, corkscrew,
scissors, metal saw, wood saw, can
opener, mini screwdriver, nailfile,
metal file, wire bender, large blade,
small blade, cap lifter, wire stripper,
reamer, punch, key ring, tweezers,
multi-purpose hook, chisel, wire cutters, pin, nail cleaner,
multipurpose pliers, clef 6 pans 5mm pour connecteurs D-
SUB,embout Pozidrive 0, embout Pozidrive 1, embout
tournevis, embout Phillips 2, embout Hex (inbus), embout
Torx 8, embout Torx 10, embout Torx 15, long ballpoint
pen, toothpick
/>The “swiss army knife” ioctlThe “swiss army knife” ioctl
ioctl viết tắt cho input/output control và ioctl viết tắt cho input/output control và
nó được dùng để thao tác đến các nó được dùng để thao tác đến các
character device thông qua filedescriptor. character device thông qua filedescriptor.
Format của ioctl là Format của ioctl là
ioctl(unsigned int fd, unsigned int request, ioctl(unsigned int fd, unsigned int request,
unsigned long argument)unsigned long argument)
. .
Giá trị trả về là Giá trị trả về là 1 nếu có lỗi và =0 nếu 1 nếu có lỗi và =0 nếu
request thành công (tương tự như các system request thành công (tương tự như các system
call khác). call khác).
ioctl (tt)ioctl (tt)
Kernel phân biệt các file đặc biệt và file thông Kernel phân biệt các file đặc biệt và file thông
thường . File đặc biệt là những file nằm trong thường . File đặc biệt là những file nằm trong
/dev và /proc. Chúng khác với các file thông /dev và /proc. Chúng khác với các file thông
standard output standard output
của một của một
tiến trình process vào một tiến trình process vào một
standard input standard input
của của
tiến trình khác. Pipes là một công cụ IPC cổ tiến trình khác. Pipes là một công cụ IPC cổ
nhất, nó có từ giai đoạn phôi thai nhất của HĐH nhất, nó có từ giai đoạn phôi thai nhất của HĐH
UNIX. Chúng cung cấp một của việc giao tiêế 1 UNIX. Chúng cung cấp một của việc giao tiêế 1
chiều (thuật ngữ halfchiều (thuật ngữ half duplex) giữa các process. duplex) giữa các process.
Tính năng này được dùng rộng rãi, thậm chí Tính năng này được dùng rộng rãi, thậm chí
trong shell của Unixtrong shell của Unix
ls | sort | lp
HalfHalf duplex UNIX Pipesduplex UNIX Pipes
Tạo pipes trong CTạo pipes trong C
Để tạo một pipe đơn giản trong C, ta cần Để tạo một pipe đơn giản trong C, ta cần
dùng pipe() system call. Nó cần 1 tham số, đó dùng pipe() system call. Nó cần 1 tham số, đó
là một array có 2 số integer và nếu thành là một array có 2 số integer và nếu thành
công, array sẽ chứa 2 file descriptors mới để công, array sẽ chứa 2 file descriptors mới để
được dùng cho pipeline. Sau khi tạo pipe, được dùng cho pipeline. Sau khi tạo pipe,
process thường sinh ra 1 tiến trình mới (lưu ý process thường sinh ra 1 tiến trình mới (lưu ý
rằng, tiến trình con thừa kế mô tả file đã mở).rằng, tiến trình con thừa kế mô tả file đã mở).
TTạo ạo pipes trong Cpipes trong C
SYSTEM CALL: pipe();SYSTEM CALL: pipe();
PROTOTYPE: int pipe( int fd[2] );PROTOTYPE: int pipe( int fd[2] );
RETURNS: 0 on successRETURNS: 0 on success
1 on error: errno = EMFILE (no free 1 on error: errno = EMFILE (no free
descriptors)descriptors)
EMFILE (system file table is full)EMFILE (system file table is full)
EFAULT (fd array is not valid)EFAULT (fd array is not valid)
NOTES: fd[0] is set up for reading, fd[1] is set NOTES: fd[0] is set up for reading, fd[1] is set
perror("fork");perror("fork");
exit(1);exit(1);
}}
if(childpid == 0) {if(childpid == 0) {
/* Child process closes up input side /* Child process closes up input side
of pipe */of pipe */
close(fd[0]);close(fd[0]);
/* Send "string" through the output /* Send "string" through the output
side of pipe */side of pipe */
write(fd[1], string, strlen(string));write(fd[1], string, strlen(string));
exit(0);exit(0);
}}
else {else {
/* Parent process closes up output /* Parent process closes up output
side of pipe */side of pipe */
close(fd[1]);close(fd[1]);
/* Read in a string from the pipe *//* Read in a string from the pipe */
nbytes = read(fd[0], readbuffer, nbytes = read(fd[0], readbuffer,
sizeof(readbuffer));sizeof(readbuffer));
printf("Received string: %s", printf("Received string: %s",
readbuffer); }readbuffer); }
return(0);return(0);
}}
Các lưu ý trong
half-duplex pipes
pipes hai chiều có thể được tạo ra bằng cách mở pipes hai chiều có thể được tạo ra bằng cách mở
hai pipes, và được gán lại file descriptors một hai pipes, và được gán lại file descriptors một
cách thích hợp trong tiến trình con .cách thích hợp trong tiến trình con .
Lời gọi pipe() phải được thực hiện trước khi gọi Lời gọi pipe() phải được thực hiện trước khi gọi
fork(), nếu không thì descriptors sẽ không được fork(), nếu không thì descriptors sẽ không được
LIBRARY FUNCTION: mknod();LIBRARY FUNCTION: mknod();
PROTOTYPE: int mknod( char *pathname, PROTOTYPE: int mknod( char *pathname,
mode_t mode, dev_t dev);mode_t mode, dev_t dev);
RETURNS: 0 on success,RETURNS: 0 on success,
1 on error:errno=EFAULT (pathname invalid)1 on error:errno=EFAULT (pathname invalid)
NOTES: Tạo filesystem node (file, device file, or FIFO) NOTES: Tạo filesystem node (file, device file, or FIFO)
EACCES (permission denied)EACCES (permission denied)
ENAMETOOLONG (pathname ENAMETOOLONG (pathname
too long)too long)
ENOENT (invalid pathname)ENOENT (invalid pathname)
ENOTDIR (invalid pathname)ENOTDIR (invalid pathname)
mknodmknod
Muốn biến rõ hơn về mknod thì hãy dùng lệnh Muốn biến rõ hơn về mknod thì hãy dùng lệnh
manman,nhưng xét ví dụ đơn giản sau trong C:,nhưng xét ví dụ đơn giản sau trong C:
mknod("/tmp/MYFIFO", S_IFIFO|0666, 0);mknod("/tmp/MYFIFO", S_IFIFO|0666, 0);
Ở đây, file “/tmp/MYFIFO” được tạo ra như là Ở đây, file “/tmp/MYFIFO” được tạo ra như là
một FIFO file. Quyền của file là “0666”, mặc dù một FIFO file. Quyền của file là “0666”, mặc dù
chúng bị ảnh hưởng bởi umask setting như sau:chúng bị ảnh hưởng bởi umask setting như sau:
final_umask = requested_permissions & final_umask = requested_permissions &
˜original_umask˜original_umask
Một mẹo nhỏ dùng umask() system call để tạm Một mẹo nhỏ dùng umask() system call để tạm
thời vô hiệu giá trị umask :thời vô hiệu giá trị umask :
umask(0);umask(0);
mknod("/tmp/MYFIFO", S_IFIFO|0666, 0);mknod("/tmp/MYFIFO", S_IFIFO|0666, 0);
Thao tác FIFO Thao tác FIFO
Các thao tác I/O trên FIFO là cần thiết như đối Các thao tác I/O trên FIFO là cần thiết như đối
với các pipe thông thường, với một ngoại lệ với các pipe thông thường, với một ngoại lệ
chính yếu. Một “open” system call hay hàm thư chính yếu. Một “open” system call hay hàm thư
viện cần phải được dùng để physically open up viện cần phải được dùng để physically open up
một kênh tới pipe. Với halfmột kênh tới pipe. Với half duplex pipes, điều duplex pipes, điều
#include <stdio.h>#include <stdio.h>
#include <stdlib.h>#include <stdlib.h>
#define FIFO_FILE "MYFIFO"#define FIFO_FILE "MYFIFO"
int main(int argc, char *argv[])int main(int argc, char *argv[])
{{
FILE *fp;FILE *fp;
if ( argc != 2 ) {if ( argc != 2 ) {
printf("USAGE: fifoclient [string]printf("USAGE: fifoclient [string]\\n");n");
exit(1);exit(1);
}}