UNIX Environment Advanced Programming--Chapter 1 UNIX Basics

Chapter 1 UNIX Basics

1.2 UNIX Architecture
 
Strictly speaking, an operating system can be defined as a kind of software that controls computer hardware resources and provides an environment for program execution. We call this software the kernel because
It's relatively small and at the heart of the environment. The kernel's interface is called a system call. The public function library is built on the system call interface, and the application can use the public function library or the system call. A shell is a special application that provides an interface for running other applications.
 
1.5 Input and output
1. File descriptor
A file descriptor is usually a small non-negative integer used by the kernel to identify the file a particular process is accessing.
When the kernel opens an existing file or creates a new file, it returns a file descriptor. This file descriptor can be used when reading and writing files.
2. Standard input, standard output and standard error
By convention, every time a new program is run, all shells open three file descriptors for it, standard input, standard output, and standard error.
3. Unbuffered I/O
The functions open, read, write, lseek, and close provide unbuffered I/O. These functions use file descriptors.
4 Standard I/O
Standard I/O provides a buffered interface to unbuffered I/O functions. Use standard I/O functions without worrying about choosing the best buffer size.
 
1.6 Programs and Processes
1. Procedure
A program is an executable file stored in a directory on disk. The kernel uses the exec function to read the program into memory and execute it.
2. Process and Process ID
An instance of execution of a program is called a process. Some operating systems use tasks to represent programs that are executing.
The UNIX system ensures that each process has a unique numeric descriptor called a process ID. Process ID is always a non-negative integer.
3. Process control
3 main functions for process control: fork, exec, waitpid.
4. Thread and thread ID
Typically, a process has only one thread of control—a set of machine instructions that execute at a time.            
All threads within a process share the same address space, file descriptors, stacks, and process-related attributes. Because they can access the same memory area,
Therefore, each thread needs to take synchronization measures to avoid inconsistency when accessing shared data.
 
1.7 Error handling
The latter negative value is usually returned when UNIX system functions fail, and the integer variable errno is usually set to a value with specific information.   
 
Two functions:
1.
#include <string.h>
/* According to the input error code, return the corresponding string information */
char *strerror(int errnum);
2.
#include <stdio.h>
/* Input and input parameters, a colon, a space, and then the error message string corresponding to error*/
void perror(const char *msg);
 
1.9 Signals
Signals are used to notify a process that something has been sent.
There are three ways for a process to handle signals.
  • Signal is ignored.
  • Process according to the system default.
  • Provide a function, the signal occurrence is to call the function, which is called catching the signal.
 
1.10 Time value
UNIX systems use two time values.
1. Calendar time. Cumulative value in seconds since January 1, 1970 00:00:00.
2. Process time. Also known as CPU time, it measures the CPU resources used by a process. Process time is calculated in time ticks.
 
When measuring the execution time of a process, the UNIX system maintains three times for the process
  • Clock Time: The total amount of time a process has been running, related to the number of processes running simultaneously in the system.
  • User CPU Time: Time spent executing user instructions.
  • System CPU time: The time elapsed to execute the memory program for this process.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325314805&siteId=291194637