On the basis of UNIX

Source: Internet
Author: User

On the basis of UNIX

Time flies, and I have graduated for almost two years. I feel that I have learned a lot but I am not refined. I think deeply about this and feel it is necessary to serialize unix programming articles to encourage myself to learn. I am here to prove that two days ago I wrote a blog post about unix environment programming from scratch.

Reference books third edition of advanced programming in UNIX environment

1.1 describes a UNIX architecture. A newbie to Linux is described as a few keywords. But for a Linux expert, this figure describes too much information.

Kernel: in a strict sense, the operating system can be understood as a software, which controls the hardware resources of the computer and provides the environment for the application to run. In fact, Linux is a kernel, which is the kernel of the GNU operating system. Of course, the familiar mac osx is also the kernel of the GNU operating system.

System Call: A system call is an interface provided by the kernel to provide external services. For example, you need to operate the keyboard. Some people will say that we can use API functions to operate the keyboard. You are right. But remember that API functions, that is, library functions, are built on system calls. The number of system calls provided by the kernel is very limited. Up to 2.6.23, the number of kernels is only 325, but there are so many API functions.

Library Function: a set of functions that meet the POSIX standard.

Shell: a special type of software, which is a command interpreter. Similar to cmd in Windows, it also interacts with the kernel through system calls. We can use the strace command to view the implementation process of the shell command. For example, strace echo.

 

Summary: System calling is the only way to access the kernel. It gives a deeper understanding of system calling during driver development.

Log on to UNIX

What did the system do when we logged on to the unix system? When we enter the user name and password, the system will find the matched user name and password in/etc/passwd.

 

For example, Username: Password (the system has encrypted the password to another file): digital user ID: digital group user ID: Comment code: Start Directory: shell program

There are 6 fields in total: 7 fields composed of numbers. Haha, now you understand the login process. You know why your initial directory is under/root;

 

Note that there are many shell versions. Watch out for what shell your system is.

Currently, the popular shell

1./bin/bash

2./bin/sh

3./bin/ksh

4./bin/csh

5./bin/tcsh

6./bin/dash

We can use the following command to view the version of the shell we are running.

 

Files And Directories

 

Below we use C to implement the ls command in shell

# Include <unistd. h> # include <dirent. h> # include <errno. h> # include <stdlib. h> # include <string. h> int main (int argc, char * argv []) {DIR * dp; // The Directory descriptor struct dirent * dirp; // The structure of the directory content if (argc! = 2) {printf ("please input -- help"); return-1;} if (strcmp (argv [1], "-- help") = 0) {puts ("-a, -- all do not hide any. the project \ n \-A, -- almost-all to list the division. and .. for any project other than \ n \ -- author and-l, the author \ n \-B of each file is listed at the same time, -- escape uses an octal overflow sequence to indicate unprintable characters \ n "); return 0;} if (dp = opendir (argv [1]) = NULL) perror ("can not open % s", argv [1]); // judge the standard error type while (dirp = readdir (dp ))! = NULL) printf ("% s \ n", dirp-> d_name); // printf ("Hello world! \ N "); return 0 ;}

I want to see our program easily based on the running results.

 

Input and Output

1. file descriptor: a non-negative integer. the kernel is used to indicate the files being accessed by a specific process. A descriptor returned when the kernel opens an existing file or creates a new file. This descriptor can be used during reading and writing.

2. Every time a new program is run, all shells open three descriptors for it, namely standard input, standard output, and standard error. By default, all three descriptors point to the terminal. It is like running any c program. If an error occurs, you will see the error prompt somewhere, and we will see the error in the terminal. Similarly, standard input and output are on the terminal.

Are you sure you want to modify the terminal?

Use standard ls

The output result is displayed on the terminal.

How to modify it to ls> 2.txt. okay, nothing is printed on the terminal. The result of the ls command is found in cat 2.txt(open the 2.txt file ).

Programs and processes

1. A program is an executable file stored in the disk directory. The kernel uses the exec function (one of the seven exec functions) to read the program into the memory and execute

2. An instance of program execution is a process. Each process has an id called pid.

3. Thread: A process usually has only one task thread for processing. If you want to handle a single problem, you can use multiple threads to handle it. This will make the process abnormal and easy. The thread of a process shares the same address space, file descriptor, stack, and so on. Of course, how to distinguish different threads, right, Use thread ID, thread ID only plays a role in the process.

The following describes how to use the exec function to execute other programs and deepen the understanding of the kernel running programs.

# Include <stdio. h> # include <unistd. h> # include <sys/wait. h> # include <string. h> int main () {char buf [10]; int I; pid_t pid; int status; printf ("cmd:"); memset (buf, '\ 0 ', 10); while (gets (buf) {if (pid = fork () <0) {perror ("fork creat error \ n ");} else if (pid = 0) {if (execlp (buf, buf, (char *) 0) <0) // run a specified program perror ("execlp error \ n"); exit (127);} if (pid = waitpid (pid, & status, 0 )) <0) perror ("wait pid error \ n"); printf ("cmd:");} return 0 ;}

This article is based on programming in the unix environment and involves many knowledge points. For example, threads and processes will be detailed in later articles. Next blog asked about the unix environment programming and continued to describe the unix system on a macro scale. Programming in combination with the unix environment will cover the majority of unix content. 2017/

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.