Embedded technology study notes (seven)

fprintf function (format output data to file)
header file: #include <stdio.h>
Define function: int fprintf (FILE * stream, constchar * format, ...);
Function description: fprintf () will format string according to parameters To convert and format the data, and then output the result to the file specified by the parameter stream until the string ('\ 0') appears.
Return value: the number of characters actually output is returned when successful, and -1 is returned when failure occurs. The cause of the error is stored in errno.
Linux process control programming A
process is a running activity of a program with certain independent functions, and it is also the smallest unit
process for resource allocation. And program: the
process is dynamic, the program is static: the program is an ordered collection of code; the process is the execution of the program. Usually processes cannot be migrated between computers; programs usually correspond to files, static and can be copied.
The process is temporary, and the program is long-lasting: the process is a state change process, and the program can be saved for a long time.
The composition of processes and programs is different: the composition of processes includes programs, data, and process control blocks (that is, process status information).
Correspondence between processes and programs: through multiple executions, a program can correspond to multiple processes; through the call relationship, a process can include multiple programs

Process life cycle:
creation: each process is created by its parent process, the process can create a child process, and the child process can create a child process of the child process
Run: multiple processes can exist at the same time, communication between processes can be
canceled: process Can be revoked, thus ending the running of a process

The Linux system is a multi-process system, and its processes have the characteristics of parallelism and non-interference. In other words, each process is an independent operating unit, with its own rights and responsibilities. Among them, each process runs in an independent virtual address space, so even if one process is abnormal, it will not affect other processes in the system.

The process in Linux contains three segments, namely "data segment", "code segment" and "stack segment". The
data segment stores global variables, constants and data space allocated by dynamic data. The
code segment stores program code data The
stack segment stores the return address of the subprogram, the parameters of the subprogram, and the local variables of the program.

Process ID (PID): a unique number that identifies a process.
The ID of the parent process (PPID)
. The user ID (UID) of the
process that started the process. Mutual exclusion means that when several processes have to use a shared resource, at most one process is allowed at any time Use, other processes that want to use the resource must wait until the resource is released, which occupies the resource.
In the operating system, resources that are only allowed to be accessed by one process at a time are called critical resource
process synchronization: a group of concurrent processes executed in a certain order is called inter-process synchronization, and a group of concurrent processes with a synchronization relationship is called a cooperative process. The information sent between cooperative processes is called a message or event.
Deadlock: multiple processes form a deadlock due to competition for resources. Without external force, these processes will never be able to move forward.

Get ID
#include <sys / types.h>
#include <unistd.h>
pid_t getpid (void) Get the process ID
pid_t getppid (void) Get the parent process ID

Process creation
#include <unistd.h>
pid_t fork (void)
function: create a child process, the wonderful thing about fork is that it is called once, but returns twice, it may have three different return values.
The return value is 0 is the child process, the return value is greater than 0 is the parent process, the return value is -1 is an error

pit_t vfork (void)
vfork () will create a new child process, its child process will copy the parent process data and stack space, and inherit the parent process user code, group code, environment variables, open file code, working directory And resource constraints.
The child process will not inherit the file lock and unprocessed signals of the parent process.
Note that Linux does not guarantee that the child process will execute before or after the parent process, so pay attention to the occurrence of deadlocks or race conditions when writing programs

exec function family
#include <unistd.h>
int execl (const char * path, const char * arg1…)
Parameters: path: the name of the executed program (including the full path)
arg1 ~ argn: command line parameters required by the executed program

Published 14 original articles · Like1 · Visits 477

Guess you like

Origin blog.csdn.net/a1152946932/article/details/105362818