1 (Unix Foundation)

Source: Internet
Author: User

1 UNIX Architecture


The operating system can be defined as a software that controls the hardware resources of the computer, also known as the kernel (kernel).
The interface of the kernel is called system call. Public function libraries on top of system calls, the application software can either use a common function library or use system calls. (Comparison of system calls and library functions at the end)

2 lists the names of all the files in a directory, that is, the implementation of the command LS

Figure 1.3. List all the files in a directory

#include "apue.h"#include <dirent.h>intMainintARGC, Char*ARGV[]) {DIR*DP; struct dirent*dirp;if(ARGC! =2) Err_quit ("Usage:ls directory_name");if(DP =Opendir(argv[1]) = = NULL) Err_sys ("can ' t open %s", argv[1]); while((Dirp =Readdir(DP)) ! = NULL)printf('%s\ n ', dirp->d_name); Closedir (DP);Exit(0);}

3 IO without buffering (open, read, write, Lseek, close)

Figure 1.4. List all the files in a directory

#include "apue.h"#define BUFFSIZE    4096int main(void){    int     n;    char    buf[BUFFSIZE];    while0)        if (write(STDOUT_FILENO, buf, n) != n)            err_sys("write error");    if0)        err_sys("read error");    exit(0);}

The constants Stdin_fileno and Stdout_fileno is defined in

4 Standard IO

The standard IO function provides a buffered interface to the IO without buffering. It has two advantages:
1) No need to worry about choosing the best buffer size
2) simplifies the processing of input lines

The function getc reads one character at a time, and this character are written by PUTC. After the last byte of input have been read, GETC returns the constant EOF (defined in

#include "apue.h"int main(void){    int     c;    while ((c = getc(stdin)) != EOF)        stdout) == EOF)            err_sys("output error");    if (ferror(stdin))        err_sys("input error");    exit(0);}
5 Printing Process ID

Figure 1.6. Print the process ID

#include "apue.h"int main(void){    printf("hello world from process ID %d\n", getpid());    exit(0);}

When this program runs, it calls the function Getpid to obtain its process ID.

6 read commands from standard input and execute

Figure 1.7. Read commands from standard input and execute them

#include "apue.h"#include <sys/wait.h>Int Main (void){CharBuf[maxline];/ * FROM Apue.h * /pid_t pid;intStatusprintf("%% ");/* Print prompt (printf requires% to print%) */     while(Fgets (buf, MAXLINE, stdin)! = NULL) {if(buf[strlen(BUF)-1] =="\ n") buf[strlen(BUF)-1] =0;/ * Replace newline with null * /        if(PID = fork ()) <0) {Err_sys ("Fork Error"); }Else if(PID = =0) {/ * Child * /EXECLP (buf, buf, (Char*)0); Err_ret ("couldn ' t execute:%s", buf);Exit(127); }/ * Parent * /        //waitpid () will temporarily stop the execution of the current process until a signal arrives or the child process ends.         if(PID = Waitpid (PID, &status,0)) <0) Err_sys ("Waitpid Error");printf("%% "); }Exit(0);}

We Call Fork to create a new process, which is a copy of the caller. We say the caller is the parent and the newly created process are the child. Then fork returns the non-negative process ID of the new child process to the parent, and returns 0 to the child. Because Fork creates a new process, we say that it was called once by the parent but returns twice in the parent and in the Child.
The Fork () function call returns two times at a time, is its own process ID, and the other is a child process, which is 0

7 Demonstrate strerror and perror
#include "apue.h"#include <errno.h>Int main(intchar *argv[]){    fprintf(stderr"EACCES: %s\n", strerror(EACCES));    errno = ENOENT;    perror(argv[0]);    exit(0);}

The C standard defines two functions that help print error messages

#include<string.h>char *strerror(int errnum);//此函数将errnum(它通常就是errno值)映射为一个出错信息字符串,并且返回此字符串的指针。//perror函数基于errno当前值,在标准出错上产生一条出错信息,然后返回#include<stdio.h>void perror(constchar* msg);//它首先输出由msg指向的字符串,然后是一个冒号,一个空格,接着是对应于errno值的出错信息,最后是一个换行符
8 Print User ID and group ID
#include "apue.h"Int main(void){    printf("uid = %d, gid = %d\n", getuid(), getgid());    exit(0);}


Because it is the root user, the UID is 0. I don't have a distribution group, so GID is also 0

9 Signal

Figure 1.10. Read commands from standard input and execute them

  #include "apue.h"  #include <sys/wait.h>  Static voidSig_int (int);/ * Our signal-catching function * /Int Main (void)  {CharBuf[maxline];/ * FROM Apue.h * /pid_t pid;intStatusif(Signal (SIGINT, sig_int) = = Sig_err) Err_sys ("Signal Error");printf("%% ");/* Print prompt (printf requires% to print%) */       while(Fgets (buf, MAXLINE, stdin)! = NULL) {if(buf[strlen(BUF)-1] =="\ n") buf[strlen(BUF)-1] =0;/ * Replace newline with null * /          if(PID = fork ()) <0) {Err_sys ("Fork Error"); }Else if(PID = =0) {/ * Child * /EXECLP (buf, buf, (Char*)0); Err_ret ("couldn ' t execute:%s", buf);Exit(127); }/ * Parent * /          if(PID = Waitpid (PID, &status,0)) <0) Err_sys ("Waitpid Error");printf("%% "); }Exit(0); }voidSig_int (intSigno) {printf("interrupt\n%%"); }


A signal is a technique that notifies a process that a situation has occurred. For example, if a process performs a division operation with a divisor of 0, the named SIDFPE (floating-point exception) is sent to the process. There are three options for how processes are handled:
1. Ignore the signal. Some signals indicate hardware exceptions, such as dividing by 0 or accessing cells outside of the address space, where the consequences of these exceptions are uncertain, so it is not recommended
2. The system is handled by default. For dividing by 0, the system terminates the process by default
3. Provide a signal that the signal occurs when the function is called, which is called capturing the signal. We need to provide a self-programmed function to handle it.

10 system calls and library functions

The system call actually refers to the bottom-level invocation, which is the meaning of the underlying call in the Linux programming. The hardware is oriented. The library function call is for application development, which is equivalent to the application's API
1. System calls
System calls provide functions such as open, close, read, write, IOCTL, etc., which need to include the header file Unistd.h. Write for example: Its function prototype is size_t write (int fd, const void *buf, size_t Nby TES), whose action object is a file descriptor or file handle FD (descriptor), to write a file, you must first open a file with writable permission to obtain the FD of the open file, such as Fd=open (\ "/dev/video\", O_ RDWR). FD is an integer value, with each new file opened, the FD obtained is the current maximum FD plus 1 (the Linux system is assigned 3 file descriptor values By default: 0-standard input,1-standard output,2-standard error)
System calls are typically used for the underlying file access (low-level files access), such as direct access to the device files in the driver.
System calls are operating system-related, so there is generally no portability across operating systems.
2. Library functions
The standard C library functions provide file operation functions such as fopen, Fread, Fwrite, Fclose, Fflush, fseek, etc., including header file stdio.h. In fwrite, for example, the function prototype is size_t fwrite (const void * Buffer, size_t size, size_t item_num, File *PF), the object of the action is the document pointer to the filename *PF, to write a file, you must first open a file with a writable permission with the fopen function to obtain the file structure pointer pf of the open files, For example pf=fopen (\ "~/proj/filename\", \ "w\"). In fact, since the operation of the library function on the file is ultimately implemented through a system call, the file structure pointer that is obtained for each file opened has one kernel space corresponding to the document descriptor FD. There is also a corresponding predefined file pointer: Stdin-standard input,stdout-standard output,stderr-standard error.
Library function calls are typically used for access to generic files in an application.
Library function calls are system-independent, so portability is good.
Because library function calls are based on the C library, it is not possible to use the device in the kernel space driver.
using library functions also has the overhead of system calls, so why not use system calls directly?
This is because the use of library functions can greatly reduce the number of system calls, since the read-write file is usually a large amount of data (which is a large number of data manipulation units implemented relative to the underlying driver's system call). This result is also due to the buffer technology. In the user space and kernel space, the use of the file operation of the buffer, for example, write a file with fwrite, the content is written to the user space buffer, when the user space buffer full or the end of the write operation, the contents of the user buffer is written to the kernel buffer, the same reason, The kernel buffer content is written to the hardware medium that corresponds to the file when the kernel buffer is full or the write is complete.

1 (Unix Foundation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.