The difference between sizeof and strlen, the difference between thread and process

The difference between sizeof and strlen:

1. The essential difference: sizeof is an operator, strlen is a function;

2. The parameter of sizeof can be a type, a variable, or a function. When it is a function, it returns the size of the return value of the function. The parameter of strlen can only be a string, and it is a string ending with'\0'. Return the length of the string when it reaches'\0';

3. When the array name is used as the sizeof parameter, it does not degenerate from the pointer. sizeof is used to calculate the size of the type in memory. When the parameter is a string, the memory size of the pointer is calculated, that is, the parameter of the four-byte array name as strlen degenerates into a pointer. The function starts counting from the address of the first element of the array until it encounters'\0'.

4. Sizeof can only calculate the dimensions of static arrays, but cannot calculate dynamic arrays. When an array is passed as a function parameter, a pointer is passed instead of an array.

The difference between threads and processes:

Thread : A thread is the smallest unit of CPU scheduling and an entity in a process. The thread does not own system resources, but only occupies the resources necessary at runtime. It can share all the resources owned by the process with other threads belonging to the same process. Each thread has its own independent running stack and program counter. A thread can create and cancel another thread, and threads in the same process can execute concurrently.

Advantages of threads:

(1) Easy to dispatch

(2) Concurrency, the process can execute different parts of the program through multiple threads;

(3) Resource utilization is low (low overhead), and thread creation is faster than process creation;

(4) It can give full play to the role of multi-processors, and each processor can be used when executing programs concurrently;

Process : The smallest unit of resource allocation during a process. Each process has its own independent code and data space. A process can contain multiple threads.

The five stages of threads and processes : create, ready, run, block, and terminate.

There is a main thread in a process, and the termination of the main thread means the end of the process. The main thread can create other child threads.

The relationship between threads and processes:

(1) A thread can only belong to one process, and a process can have multiple threads;

(2) The resources owned by the process are shared by all threads in the same process;

(3) What is actually running on the processor is a thread;

(4) Threads need to have synchronization and mutual exclusion operations during execution. Use message communication.

The difference between threads and processes:

(1) Scheduling; thread is the basic unit of scheduling, and process is the basic unit of resources

(2) Concurrency; processes can be executed concurrently, and different threads of the same process can also be executed concurrently;

(3) Resource issues: Processes can own resources, threads do not occupy resources, but can access all resources of the process.

(4) System overhead: The system overhead is obviously greater than the thread overhead when creating and revoking processes.

Guess you like

Origin blog.csdn.net/Gunanhuai/article/details/114649546