The concept of threads and multiple threads of control

Multithreading:
    Multi-process: data can be processed (concurrent / parallel) at the same time
    Multithreading: it may process data (concurrent / parallel) simultaneously
        
    Thread concept:
        Multi-tasking process (using more virtual address spaces): multiple tasks down into multiple programs (broken down into more than one process is completed).
        Multithreaded processing: a plurality of pcb share the same virtual address space, while the completion of a plurality of different modules of code segments.
        
        Understand the process: only one thread of the process
        Thread of understanding:
            1. In a conventional operating system process is a description of the operation of the program - run> pcb, control program
            2.linux and is not designed to control a thread tcb threads running
            3. Thread the process in order to achieve the pcb under linux, that is, under Linux pcb is actually a thread.
                (So ​​linux Thread: Also called lightweight processes)
                (Linux under process: actually a thread group - containing more than one / thread)
            
        The basic unit because the cpu scheduler runs are scheduled pcb, therefore when the cpu scheduling thread
        Because a program up and running will allocate considerable resources to the thread group, so the process is the basic unit of resource allocation
        
        Because the child process created by vfork shared with the parent process through a virtual address space, and therefore can not run at the same time
        And why multithreading will not have this problem?
            Unique between threads in the same process of data:
                1. Stack
                2. Register (hardware on cpu)
                3.errno (error number)
                4. The mask character signal (set signal blocking)
                The thread id (thread identifier)
                
            Shared between threads of the same process data:
                1. The data segment, code segments
                2. file descriptor table
                3. The signal processing mode
                4. Work path
                The user id, group id
                
        Multi-threaded and multi-process can be complicated by the task which one is better? (Analysis of the advantages and disadvantages, depending on the use on the scene)
            The advantages of multi-threading: share the same virtual address space
                1. Communication between threads easier
                2. Thread the lower costs to create destruction
                3. On the same thread scheduling process costs between a lower
                4. enforcement more detailed
                
            Multithreading disadvantages:
                1. Lack of access control, low robustness: Some system calls or abnormal, an effect for the entire process
                
        Common advantage: It's possible concurrent / parallel processing tasks, improve the treatment effect
                
        cpu intensive program: the program is a lot of arithmetic operations
        
        io intensive program: the program is a lot of io operations
        
        A common drawback: the operation of critical resources need to be considered more and more complex coding
 
        Multi-process usage scenarios:
            Security should be the main process is particularly high --shell (using multi-threaded multi-process than safety)
            
 
    Thread Control:
        Thread creation:
            Thread Control Interface: Because the operating system does not provide a direct interface to create a thread, the thread on the package a set of interfaces. It is called
            Thread creation is a user mode thread, there is one lightweight process scheduler in the kernel
        
            int pthread_create(pthread_t *thread, const pthread_sttr_t *attr,
                void *(*start_routine)(void *), void *arg);
                
                thread: output type parameters for obtaining a newly created thread id
                attr: Thread properties are usually blank
                start_routine: thread entry function
                arg: argument passed to the thread
                Return value: the successful return 0; Failure: error number (errno)
            
            Each thread has a pcb-task - struct has a pid, but the user using the ps -ef command to view the process when only one process,
            Only a process pid
            
            View Information: ps -eflL | head -n 1 && ps -eflL | grep create
            
            LWP:task_struct -> pid
            PID: task_struct -> tgid (thread group id) == default thread group in the main thread process id pid ==             
            tid: first address (the first address in the address space of the thread shared area)
            
        Thread termination:
            Thread entry point return, if return exit in the main function the whole process
            
            void pthread_exit (void * retval): Quit calling thread retval generally NULL (who calls who quit)
            Thread exits will form a zombie process, because the thread exits but also save your own exit return value
            Thread exits, the return value will save and exit, and become a zombie process
            The main thread exits, and the process does not exit.
            
            Cancel the specified thread :( passive launch)
            int pthread_cancel(pthread_t thread)
            thread —>
            
            After-school research:
            Can you recall your own?
            If one thread to cancel the return value is how much?
            
        Thread waits: Gets the return value of the thread exits, and allow the operating system thread resource recovery
            Start a thread after a default property is the right thread is joinable state
            * Thread is joinable state, exit, it does not automatically release the resources that need to be waiting for other threads
            
            int pthread_join(pthread_t thread, void **retval);
            Function: Wait thread exits, access to the return value, resource recovery thread
            Premise: The waiting thread must be joinable state
            pthread-> id specified thread
            retval-> get a thread exit return value
            return value
                Success: 0 Failed: Error number (errno)
            
        
        :( thread separation property of the thread becomes detach property from joinable)
            Function: a separate thread, the thread exits the system will automatically recover resources, can not be separated thread to wait, if an error is returned it will have to pthread_join
            
            int pthread_detach (pthread_t thread); -> can be called at any position
            thread: to be separated thread id
            
            Note: The thread is separated from the premise that the user does not care about the return value of the thread exits
            * Automatic resource recovery after detach state in a thread exit, need not be waiting
            
            * The default attribute is joinable thread

Guess you like

Origin www.cnblogs.com/cuckoo-/p/11409967.html