difference between thread and process

what is a process

The classic definition of a process is an instance of an executing program . Every program in the system runs in the context of a process.

A context is made up of the states required for a program to function correctly .

This state includes the program's code and data in memory, its stack, the contents of general-purpose registers, the program counter, environment variables, and a set of open file descriptors.

Each process is a logical control flow, and multiple flows run concurrently in time slices.

Each process has its own virtual address space.

We use the fork system call to create parent and child processes.

#include<sys/types.h>
pid_t fork(void);
Return value: If it is called successfully once, it returns two values, the child process returns 0, and the parent process returns the child process ID; otherwise, an error returns -1

After fork and before exec, the two processes use the same physical space. The code segment, BSS segment, data segment, and stack of the child process all point to the physical space of the parent process , but the virtual addresses are different .

New physical memory is not allocated to the child process until the parent and child processes behave differently.

Use waitpid to recycle child processes

#include<sys/types.h>
#include<sys/wait.h>
pid_t waitpid(pid_t pid,int * status,int options);

If there is no wait or waitpid to recycle the child process, a zombie process will appear.

Orphaned process: A parent process exits while one or more of its child processes are still running, then those child processes will become orphaned. The orphan process will be adopted by the init process (process number is 1), and the init process will complete the state collection work for them.

Zombie process: A process uses fork to create a child process. If the child process exits and the parent process does not call wait or waitpid to obtain the status information of the child process, the process descriptor of the child process is still stored in the system. Such a process is called a zombie process.


what is thread

A thread is a logical flow that runs in the context of a process . A process can run multiple threads at the same time, and the threads are automatically scheduled by the kernel.

Each thread has its own thread context , including a unique integer thread ID, stack, stack pointer, program counter, general purpose registers, and condition codes .

All threads share the entire virtual address space of the process.

Threads created by the main thread are called peer threads . A thread context switch occurs when the main thread executes a slow system call (read, sleep) or is interrupted by a system timer.

Threads create peer threads via pthread_create.
#include<pthread.h>
int pthread_create(pthread_t *tidp, const pthread_attr_t *attr, (void*)(*start_rtn)(void*), void *arg);
The first parameter is a pointer to the thread descriptor tid, the second parameter is usually set to NULL, the third parameter is the thread function, and the last parameter is the passed parameter.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324693431&siteId=291194637