Linux c Note Process Control (i)

Source: Internet
Author: User
Tags unique id

1. Process Brief
A process is a dynamic entity, the basic unit of the operating system resource allocation, and each process has a unique process ID for a nonnegative integer. Because the process ID identifier is always unique, it is often used as part of another identifier to ensure its uniqueness.
1) Process ID:
Each process has a unique process ID of a nonnegative integer. Because the process ID identifier is always unique, it is often used as part of another identifier to ensure its uniqueness. The Tmpnam function creates a unique path name for the process ID as part of the name.
There are some dedicated processes: Process ID 0 is the dispatch process, often referred to as the interchange process (swapper). The process does not execute any program on disk-it is part of the kernel and is therefore referred to as a system process. Process ID 1 is typically the INIT process, which is called by the kernel at the end of the bootstrap process. The program file for this process is/Etc/init in the earlier version of UXIX, which is/Sbin/init in the newer version. This process is responsible for starting a UNIX system after the kernel bootstrap. Init typically reads system-related initialization files (/ETC/RC * files) and directs the system to a state (for example, multiple users). The init process never terminates. It is a normal user process (unlike the swap process, which is not a system process in the kernel), but it runs with superuser privileges. In some of the virtual memory implementations of U N I x, Process ID 2 is the page sprite process (Pagedaemon). This process is responsible for paging operations that support the virtual storage system. As with the swap process, the page daemon process is also a kernel process.

In addition to process I D, there are some other identifiers for each process. The following functions return these identifiers.

#include <sys/types.h>
#include <unistd.h>
pid_t getpid (void); Return: Process ID of the calling process
pid_t getppid (void); return: The parent process ID of the calling process
uid_t getuid (void); Return: The actual user ID of the calling process
uid_t geteuid (void); Return: The valid user ID of the calling process
gid_t getgid (void); Return: The actual group ID of the calling process
gid_t getegid (void); Return: The valid group ID of the calling process


Process Status of 2.linux:
1). Operational status 2). interruptible wait Status 3). Non-interruptible wait status 4). Zombie Status 5). Stop State

3. Process Control:
Fork: Using and creating a new process
exit: Used to terminate a process
exec: Used to execute a process
wait: Suspends the parent program and waits for the subroutine to terminate
Getpid: Gets the process ID of the current process
Nice: Changing the priority of a process

• Code snippet:
Part of the machine instruction executed by the CPU. In general, code snippets are shareable, there is only one copy in memory, and the code snippet is often read-only to prevent the program from modifying its own instructions due to an accident, the parent-child program shares the code snippet, and the child process also obtains a copy of the parent process data heap, Zhi.
Initialize Data segment:
This segment is often referred to as a data segment, which contains variables that need to be assigned an initial value in the program.   For example, a description outside any function in the C program: int maxcount = 99; Causes this variable to be stored in the initialization data segment with an initial value.
• Non-initialized data segments:
This segment is often referred to as a BSS segment, which is derived from an operator of an earlier assembler, meaning "block started by symbol (blocks starting with symbols)", which the kernel initializes to 0 before the program begins execution.  Description outside the function: Long sum[1000]; Causes this variable to be stored in a non-initialized data segment.
Stack
The automatic variables and the information you need to save each time the function is called are stored in this section. Each time the function is called, its return address, and the caller's environment information (for example, some machine registers) are stored in the stack. The newly called function then allocates storage space for its automatic and temporary variables on the stack. By using the stack in this way, the C function can be called recursively. For function calls, save the return address of the function, the parameters of the function, and the local quantification of the function's internal definition. ,
Heap
Dynamic storage allocations are typically performed in the heap.



4. Create a process
1). Fork function
An existing process call fork function is the only way for the Unix kernel to create a new process (this does not apply to the previous section
Process, the Init process, and the page daemon. These processes are made by the kernel as part of the bootstrap process in a special way
Created).
#include <sys/types.h>
#include <unistd.h>
pid_t fork (void);
Return: 0 in child process, parent process is child process ID, error is-1 (the reason why the creation failed is that the parent program has more than the specified number of subroutines, Error=eagain, if the available memory is insufficient also causes the creation to fail, at this time error = Enomem)
A new process created by Fork is called a subprocess (child process). The function is called once, but returns two times. The difference of two returns is that the return value of the child process is 0, and the return value of the parent process is the process ID of the new child process. The reason for returning the child process ID to the parent process is that because there can be more than one child process for a process, there is no function that enables a process to obtain the process ID of all its child processes. Fork causes the child process to get a return value of 0 because a process will have only one parent process, so the child process can always call Getppid to get the process ID of its parent process (process ID 0 is always used by the interchange process, so the process ID of a child process cannot be 0).
The child process and the parent process continue to execute the instruction after the fork. A child process is a replica of the parent process. For example, a child process obtains a copy of the parent process data space, heap, and stack. Note that this is the copy owned by the child process. The parent, child processes do not share these storage space portions. If the body segment is read-only, the parent and child processes share the body segment.
Now a lot of implementations do not do a parent process data segment and the heap is completely
1. The subroutine has its own unique ID
The return value of 2.fork is different, the parent process returns the ID of the child process, and the child process is 0
3. Different parent process ID. The parent process ID of the child process to create his parent process ID.
4. The child process shares the file descriptor that the parent process opens, but the parent process changes the literal descriptor without affecting the file descriptor in the child process.
5. Child processes do not inherit file locks set by parent process
6. The child process does not inherit the settings of the parent process warning
7. The pending signal set of the child process is emptied

There are two ways to use fork:
A parent process wants to replicate itself so that the parent and child processes execute different pieces of code at the same time. This is common in the Network service process-the parent process waits for the client's service request. When such a request arrives, the parent process calls fork to cause the child process to process the request. The parent process continues to wait for the next service request.
A process is going to execute a different program. This is a common scenario for the shell. In this case, the child process
Call exec immediately after returning from fork.
Some operating systems combine two (2) operations (exec after fork) into one and call it spawn. UNIX separates the two operations, because in many cases it is necessary to use the fork separately, followed by exec. Also, separate the two operations so that the child process can change its properties between fork and exec. such as I/O redirection, User ID, signal arrangement, and so on.

vfork function
The call sequence and return value of the Vfork function is the same as fork, but the semantics of the two are different.
Vfork is used to create a new process, and the purpose of the new process is to E x E c a new program. Vfork creates a child process just like fork, but it does not completely copy the address space of the parent process into the child process, because the child process immediately calls exec (or exit) and therefore does not save the address space. However, before the child process calls exec or exit, it runs in the space of the parent process. This mode of operation improves efficiency in some of the page-type virtual memory implementations of U N I x
Another difference between vfork and fork is that vfork guarantees that the child process runs first, and the parent process may be scheduled to run after it calls exec or exit. (If the child process relies on further actions of the parent process before invoking these functions, it causes a deadlock.) )

The so-called deadlock: refers to two or more than two processes in the course of execution, because of competing resources or due to the communication between each other caused by a blocking phenomenon, if there is no external force, they will not be able to proceed. At this point the system is in a deadlock state or the system generates a deadlock, and these processes, which are always waiting on each other, are called deadlock processes.

Linux c Note Process Control (i)

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.