A process is an entity. Every process has his own memory address segment (Heap,stack, etc.) the process is the executing program. A program is an inanimate entity that can become an active entity only if the processor gives the program life. Process is the most basic and important concept in the operating system. threads, which are also known as lightweight processes (lightweight process LWP), are the smallest units of program execution. Each program has at least one thread, if the program has only one thread, then it is the program itself. A single-threaded process can simply think of a process with only one thread. A process that only does one thing at a time, has multiple threads after a process can do many things at the same time. Each thread can handle a different transaction. regardless of the system has several CPUs, even if the process is running on a single CPU, multithreading can also be the process of processing multiple transactions concurrently. One thread blocking does not affect another thread. multi-threaded processes can take advantage of system CPU resources as much as possible.
But not as many threads as possible, the better the threads, the less time slices the CPU allocates to each thread.
Threads contain the information necessary to represent the in-Process execution environment, including the thread ID that identifies the thread, a set of register values, stacks, scheduling priorities and policies, signal-masking words, errno variables, and thread-private data.
For memory, heap memory and code areas are generally part of a process, but the stack belongs to one thread, and each thread has a separate stack . errno are also part of a single thread, and the errno in each thread is independent. all the information in the process is shared with threads, including execution code, global variables, and heap memory, stacks, and file descriptors. Thread Identity - just as each process has a process ID, the thread also has its own ID. int. -- The thread can be represented by pthread_t, and pthread_t cannot treat it as an integer. --thread can get its own thread ID through the pthread_self () function
Thread Creation--There is only one control thread in the process--when the program starts running, each process has only one thread, which is started in a single-threaded manner, and the behavior of the process is no different from the traditional process until multiple threads are created--GCC needs to be added at the time of the link-the Lpthread option (pthread is a shared library file ). --create a thread to invoke the Pthread_create function. intPthread_create (pthread_t *thread,Constpthread_attr_t *attr,void* (*start_routine) (void*),void*arg); If Pthread_create returns successfully, the memory unit that the thread points to is set to the thread ID of the newly created thread. The attr parameter is used to customize various thread properties. The newly created thread executes from the Start_routine function address, which has only one void*parameters,
If you need to pass multiple arguments to the Start_routine function, you need to put these parameters into a struct and then use the address of the struct as void*incoming. There is no guarantee that the thread will run first when it is created . The Pthread function returns 0 successfully, and the failure returns non 0. --NOTE: Each thread has a copy of errno, and different threads have different errno
Linux Processes and Threads