Basic UNIX knowledge programs and processes

Source: Internet
Author: User

I. Procedures

A program (program) is an executable file stored on a disk and in a directory. Use one of the Six exec functions to read the program into the memory and execute the program.

2. Process and process ID

The execution instance of a program is called a process ). Some operating systems use tasks to indicate programs being executed.

UNIX ensures that each process has a unique digital identifier called process ID ). The process ID is always a non-negative integer.

Program list 1-4 print process ID

[root@localhost unix_env_advance_prog]# cat prog1-

Compile and run:

[root@localhost unix_env_advance_prog]# ./prog1- process ID /prog1- process ID 

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

3. Process Control

There are three main functions used for process control: fork, exec, and waitpid. (Exec has six variants, but they are often referred to as exec functions .)

Program list 1-5 read and execute commands from standard input (Simplified Implementation of shell programs ):

[root@localhost unix_env_advance_prog]# cat prog1-<sys/wait.h>    buf[MAXLINE];   );         (fgets(buf, MAXLINE, stdin) !=(buf[strlen(buf) - ] == - ] = ;                 ((pid = fork()) <  (pid == )   *)                ((pid = waitpid(pid, &status, )) < 

Compile and run:

[root@localhost unix_env_advance_prog]# ./prog1-%-.c  prog1-.c  prog1-.c  prog1-.c  prog1--   prog1-    prog1-    prog1-    prog1-%

Use the standard I/O function fgets to read a row from the standard input. When you type the file terminator (usually Ctrl + D) as the row's 1st characters, fgets returns a null pointer, so the process terminates the loop.

Because each row returned by fgets is terminated with a line break, followed by a null byte, use the standard C function strlen to calculate the length of this string, and then replace the line break with a null byte. This is because the execlp function requires that the parameter end with a null character instead of a linefeed.

Call fork to create a new process. The new process is a copy of the calling process. We call the calling process as the parent process and the newly created process as the child process. Fork returns the ID (non-negative) of the new Child process to the parent process, and returns 0 to the child process. Because fork creates a new process, it is called once (by the parent process), but returns twice (in the parent process and child process respectively ).

In a child process, execlp is called to execute the command read from standard input. This replaces the program file previously executed by the sub-process with the new program file. The combination of fork and exec following it is a new process that some operating systems call. In UNIX systems, these two parts are separated to form two functions.

The child process calls execlp to execute the new program file, and the parent process wants to wait for the child process to terminate. This requirement is implemented by calling waitpid. Its parameter specifies the process to wait (here, pid parameter is the sub-process ID ). The waitpid function returns the subprocess termination status (status variable ). If necessary, you can use the value of the status variable to accurately determine the cause of the termination of the sub-process.

Note: ^ D indicates a control character. The Control character is a special character, which is formed by pressing the Control key on the keyboard-usually marked as Control or Ctrl, while pressing another key. Control-D or ^ D is the default file Terminator.

4. Thread and thread ID

Generally, a process has only one thread and only executes one group of machine commands at a time. For some problems, if different parts use a control thread, it is much easier to solve the problem. In addition, multiple control threads can make full use of the parallelism of the multi-processor system.

. Because they can access the same bucket

Same as the process, the thread is also identified by ID. However ,. When multiple threads are operated in a process, we use the thread ID to reference the corresponding thread.

The function that controls the thread is similar to the function that controls the process, but there is another set. After the process model was established for a long time, the thread model was introduced into the UNIX system. The two models interacted with each other in a complex manner.

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.