<UNIX advanced programming> note Chapter1

Source: Internet
Author: User

The OS is strictly software and provides resources and app running environment for computers. It is called the kernel.

The kernel interface is called as system call. Then the library function encapsulates the system call. shell is a special app that provides an interface for running other programs.

Shell is a command line interpreter. It reads the input and executes the command.

The UNIX file system is composed of directory and file. The directory starts from root and is named /.

File Attribute refers to the type, size, owner, permission, and modification time. Stat and fstat returned File Attribute structure.

For example:




2. File Name

Only/and null cannot appear in the file name. Because/is used to separate the component path name, null is used to terminate a path name.

If you create a new directory, two file names are automatically created ..

Point refers to the current directory,... refers to the upper-level directory.


3. Path Name

One or more/form the path name.

Starting with "/" indicates the relative path. Otherwise, it is an absolute path. In fact, relative paths are often used.


Practice:

1. Listing all files in the directory is to implement the LS function.

#include <dirent.h>#include "stdio.h"#include "stdlib.h"int main(int argc, char** argv){    if (argc != 2){        printf("usage: ls dir_name\n");        return 1;    }    DIR* dp = opendir(argv[1]);    if (NULL == dp){        printf("can't open %s\n", argv[1]);        return 1;    }    printf("load dir success\n");    struct dirent* dirp = NULL;    while ((dirp = readdir(dp)) != NULL)        printf("%s ", dirp->d_name);    printf("\n");    closedir(dp);    return 0;}

The results show that... And... are indeed valid directories. The printed results are unordered. The ls results are ordered.

Exit (0) indicates that the program Exits normally. 1-255 indicates the error code.


1.5 Input and Output


FD is a non-negative integer. It indicates the file being accessed by a specific process by the kernel.

Each time the shell is opened, three default FD are opened: stdin stdout stderror

In addition, shell can be easily redirected.

3) No buffer Io

Open read close write lseek provides Io without buffering

Code:

#include "stdio.h"#include "unistd.h"int main(){    int n = 0;    const int maxLen = 4096;    char buff[maxLen];    while ((n == read(STDIN_FILENO, buff, maxLen)) > 0){        if (write(STDOUT_FILENO, buff, n) != n){            printf("error\n");            exit(1);        }    }        exit(0);}

This code is not very understandable. Let's see it tomorrow.

Learn this.



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.