Linux processes, threads, kernel action functions __oracle

Source: Internet
Author: User
Tags terminates
Process

1. Replace process mirroring

#include <unistd.h>

extern char **environ;

int execl (const char *path, const Char*arg, ..., (char*) 0);

int EXECLP (const char *file, const Char*arg, ..., (char*) 0);

int execle (const char *path, const Char*arg, ..., char * const envp[]);

int execv (const char *path, char *constargv[]);

int EXECVP (const char *file, char *constargv[]);

int Execve (const char *file, char *const argv[], char *const envp[]);

1) execl, EXECLP, execle parameters are variable, end with null pointer; Execv, EXECVP The second argument is an array of strings.

2 function passes the ARGV parameter to the main function.

3 The function ending with P searches the PATH environment variable to find the new program, and does not exist by using an absolute path to pass to the function.

4 The EXEC function does not return the function that called it.

2. Copying Process objects

#include <unistd.h>

#include <sys/types.h>

pid_t fork (void);

The new process is exactly the same as the original process, executing the code exactly the same, just with its own data space, environment, and file descriptor.

return value:

-1: Creation failed;

0: Current process for subprocess;

Non 0: The current process is the parent process.

3. Process Waiting

#include <sys/types.h>

#include <sys/wait.h>

pid_t Wait (int*stat_loc);

pid_t waitpid (pid_t pid, int* stat_loc, int options);

int Waitid (idtype_tidtype, id_t ID, siginfo_t *infop, int options);

Pauses the parent process until the child process is finished.

Stat_loc:

wifexited

Child process ends Normally, WIFEXITD (Stat_loc) takes a value other than 0

Wexitstatus

Wexitstatus (Stat_loc) is not zero, for process exit code

Wifsignaled

Wifsignaled (Stat_loc) Non-zero If the child process terminates because of a captured signal

Wtermsig

Wtermsig (Stat_loc) Non-zero, then signal code

Wcoredump

Child process terminates unexpectedly, Wcoredump (Stat_loc) takes Non-zero

wifstopped

wifstopped (Stat_loc) Non-zero, then a signal code

4. exit

Exits the process. Process Communication , signaling

Most of the signal types that Linux provides are for the kernel to use, and only a handful of signals can be used to transfer between processes. Here are the common signals and their meanings:

sighup

When terminating a terminal, the kernel sends this signal to all processes controlled by the terminal. Usually the case

, the control terminal of a process group is the terminal that the user owns, but not exactly. When a process group's first process knot

Bundle, this signal is sent to all processes in the process group. This ensures that when a user exits from use,

The subsequent stage of the process is terminated unless there are other arrangements.

sigint

When a user presses the interrupt key (typically CTRL + C), the kernel sends all processes associated with the terminal

Send this signal. It provides an easy way to abort running a program.

sigquit

This signal is very similar to SIGINT, when the user presses the Exit key (for ASCII FS, usually ctrl+\),

The kernel sends out this signal. Sigquit will form the abnormal termination described by the POSIX standard. We call this

The actual operation implemented by UNIX is the core dump, with the information "Quit (Coredump)" points out that this exercise

happen. At this point, the image of the process is transferred to a disk file for debugging purposes.

sigill

The kernel emits this signal when a process attempts to execute an illegal instruction. For example, in the absence of appropriate hardware

Supported conditions, an attempt to execute a floating-point instruction can cause this signal to occur. Sigill and Sigquit

, it also forms an abnormal termination.

sigtrap

This is a proprietary signal that is used by the debugger. Because of his special line and particularity, we no longer discuss it further. Sigtrap also forms abnormal termination.

sigfpe

When a floating-point error is generated (such as an overflow), the kernel emits this signal, which causes an abnormal termination.

sigkill

This is a fairly special signal that is sent from one process to another so that the process that receives the signal

Terminate. The kernel occasionally emits such signals. SIGKILL is characterized by its inability to be ignored and captured, only through

This signal is processed by a user-defined corresponding interrupt handler. Because all the other signals can be ignored and captured, the

Only this signal can absolutely guarantee the termination of a process.

sigalrm

When a timer comes in, the kernel sends the signal to the process. The timer is made by the change process itself with the department

The alarm () set by the EC call.

sigterm

This signal is provided by the system to the ordinary program, and it is used to terminate a process according to the rules.

sigstop

This signal causes the process to temporarily abort, and the system reverses control back to the next process that is waiting to be run.

SIGUSR1 and SIGUSR2

Like Sigterm, these two signals are not sent by the kernel and can be used for whatever purpose the user wants.

sigchld

child process end signal. It is used in UNIX to implement system call exit () and wait (). When the exit () is executed, the SIGCHLD signal is sent to the parent process of the child process, and if the parent process is executing wait (), it is awakened; if the parent process is not executing wait (), then the parent process does not capture the SIGCHLD signal, so the signal does not work. The child process enters the transition state (the child process ends without entering the transition state if the parent process ignores sigchld). This mechanism is very important for most UNIX programmers. signal and processing

1. Send Signal

#include <unistd.h>

unsigned int alarm (unisigned int seconds); send a seconds signal after SIGALRM

Intkill (pid_t pid, int sig); Send signal to specified process

2. Signal Processing

int signal (int sig, __sighandler_t handler);

SIG: Indicates the type of signal to be processed, which can take away any signal except Sigkill and sigstop.

Handler: You can take the following three values:

1 A function address that returns an integer value.

This function must be declared before the signal () is invoked, and handler is the name of the function. When a signal of type sig is received, the function specified by handler is executed. This function should have the following form of definition:

int func (int sig);

The sig is the only parameter passed to it. Once the signal () call is executed, the process simply receives a signal of type sig.

Executes the Func () function immediately, regardless of which part of the program is executing. When the Func () function finishes, control

Right back to the point where the process was interrupted.

2) Sig_ign

This symbol indicates that the signal is ignored. The corresponding signal () call is performed, and the process ignores the signal of type sig.

3) SIG_DFL

This symbol indicates that the recovery system defaults to processing the signal. Inter-process communication (pipeline)

1. #include <unistd.h>

Int pipe (intfd[2]);

Function: Get two pipeline descriptors;

return value:-1 indicates failure, 0 indicates success.

2. Pipes in standard I/O functions

#include <stdio.h>

FILE *popen (Constchar *cmdstring, const char *type);

int Pclose (FILE*PF);

Popen function: Create a pipe, then call fork to produce a subprocess, call exec execute the cmdstring command, close the pipe without using a short, execute the shell to run the command, and wait for the command to terminate.

Parameters:

Cmdstring: Shell command that needs to be executed.

Type: If "r", the command is connected to the standard output, and if "w", the command is connected to the standard input.

3. Multithreading

1. Create a function of a thread:

#include <pthread.h>

Intpthread_create (pthread_t *restrict tidp,const pthread_attr_t *restrict attr,void * (*START_RTN) (void), void * Restrict Arg);

Return value: If the thread is successfully established to return 0, the wrong number is returned.

Parameters:

Pthread_t*restrict TIDP The thread ID pointer of the thread to be created

constpthread_attr_t *restrict attr Thread properties when creating a thread

void* (START_RTN) (void) return value is a pointer function of type void

The formal parameters of void *restrictarg Start_rtn

You can generally use this:

Pthread_create (&id2,null, (void*) myThread1, NULL);

2. wait for the end of a thread: the Pthread_join () function, to wait for thread-specified threads to end in a blocked way

#include <pthread.h>

int Pthread_join (pthread_t thread, void**retval);

return value:

0 represent success. Failure, the error number is returned

Parameters:

Thread: Threads identifier, which is the thread ID, identifies the unique thread.

retval: A user-defined pointer that is used to store the return value of the waiting thread.

3. Exit Thread

void Pthread_exit (void *retval);

4. Cancel the thread

int Pthread_cancel (pthread_t thread);

5. System

System () invokes the fork () to produce the child process, which invokes the/BIN/SH-C string to execute the command represented by the parameter string string, which then returns the process of the original call. The SIGCHLD signal is temporarily shelved during the call to System () and the SIGINT and sigquit signals are ignored.

#i nclude<stdlib.h>

int system (const char *string);

return value:

-1: Error occurred

0: The call was successful but no child processes were present

>0: ID of the child process that successfully exited

Parameters:

String: Command line Kernel driver

1. Kernel Driver main framework

The kernel-driven start function must have the following:

Module_init (function) loading, and module_exit (function) unloading the kernel.

Kernel-driven function headers are defined in linux/or asm/.

2. module_init(x);

Driver initialization entry point.

Parameters:

x function tobe run at kernel boot time or module insertion

3. module_exit(x);

Driver exit entry point.

Parameters:

X:function to was Runwhen driver is removed

4. Write 8-bit data (1 bytes) on OUTB () I/O;

INB () reads one byte from the I/O port (eight-bit)

INW reads one word from the I/O port (that is, two bytes, 16 bits)

Write 16-bit data (2 bytes) on OUTW () I/O;

Write 32-bit data (4 bytes) on outl () I/O.

void Outb (unsigned char data, unsigned short port);

byte inb (Word port);

void outw (unsigned short data, unsigned port);

word inw (word port); returns two bytes;

void outl (unsigned long data, unsigned short port);

5. Each device corresponds to a structural body:

struct File_operations {

The struct module *owner;//points to a pointer to the owning module that is not allowed to unload when its operation is still in use.

///usually simple initialization to this_module.

loff_t (*llseek) (struct file *, loff_t,int);//This action is used to change the read-write location of the current file and to use the new location as the return value.

ssize_t (*read) (struct file *, char __user*, size_t, loff_t *);//This operation is used to get data from the device.

ssize_t (*write) (struct file *, const char__user *, size_t, loff_t *);//This operation is used to send data to the device.

ssize_t (*aio_read) (struct KIOCB *, conststruct Iovec *, unsigned long, loff_t);//This action is used to initialize an asynchronous read operation.

ssize_t (*aio_write) (struct KIOCB *, conststruct Iovec *, unsigned long, loff_t);//This action is used to initialize an asynchronous write operation.

Int (*readdir) (struct file *, void *,filldir_t);//This operation is used to read the directory.

unsigned int (*poll) (struct file *, structpoll_table_struct *)//This action is used to query whether the read or write of one or more file descriptors will be blocked.

Int (*ioctl) (struct inode *, struct file*, unsigned int, unsigned long);//This operation is used to provide methods for issuing device-specific commands.

Long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);

Long (*compat_ioctl) (struct file *,unsigned int, unsigned long);

Int (*mmap) (struct file *, structvm_area_struct *);//This operation is used to request that the device memory be mapped to the address space of the process.

Int (*open) (struct inode *, struct file*)//This operation is used to open the device file and is the first operation on the device.

Int (*flush) (struct file *, fl_owner_tid);

Int (*release) (struct inode *, struct file*);//This action is used to release the file structure. can be empty.

Int (*fsync) (struct file *, struct dentry*, int datasync);

Int (*aio_fsync) (struct KIOCB *, intdatasync);

Int (*fasync) (int, struct file *, int);

Int (*lock) (struct file *, int, structfile_lock *);

ssize_t (*sendpage) (struct file *, structpage *, int, size_t, loff_t *, int);

unsigned Long (*get_unmapped_area) (Structfile *, unsigned long, unsigned long, unsigned long, unsigned long);

Int (*check_flags) (int);

Int (*dir_notify) (struct file *filp,unsigned long arg);

Int (*flock) (struct file *, int, structfile_lock *);

ssize_t (*splice_write) (Structpipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);

ssize_t (*splice_read) (struct file *,loff_t *, struct pipe_inode_info *, size_t, unsigned int);

Int (*setlease) (struct file *, long, structfile_lock * *);

Int (*fsetattr) (struct file *, struct iattr*);

};

6. Copy kernel data into user space

unsigned long copy_to_user (void __user *to, const void *from, unsigned long N)

{

if (ACCESS_OK (Verify_write, to, N))

n = __copy_to_user (to, from, N);

return n;

}

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.