A few questions about the operating system

Several problems with the operating system

1. What is the difference between a process and a thread?
Parse:

(1) A process is an independent unit of resource allocation and scheduling, and a thread is the basic unit of CPU scheduling;
(2) The same process can include multiple threads, and threads share the resources of the entire process (registers, stacks, contexts) , a process includes at least one thread;
(3) The creation of the process calls fork or vfork, and the creation of the thread calls pthread_create. After the process ends, all threads it owns will be destroyed, and the end of the thread will not affect the same process. The end of other threads;
(4) A thread is a lightweight process, and its creation and destruction takes much less time than a process, and all execution functions in the operating system are completed by creating threads;
(5) Threads are Synchronization and mutual exclusion are generally required during execution, because they share all resources of the same process;
(6) Threads have their own private attributes TCB, thread id, registers, hardware context, and processes also have their own private attributes Process Control Block PCB , these private attributes are not shared and are used to identify a process or a thread.
2. Deadlock? Cause of deadlock? Necessary for deadlock? How to deal with deadlock?
Parse:

Deadlock: Deadlock is a stalemate state caused by mutual waiting for resources. If there is no external intervention, this state will continue
.
Necessary conditions: mutual exclusion, non-preemption, circular waiting, request and hold;
dealing with deadlocks: because mutual exclusion is immutable, only one of the other three conditions can be destroyed to release deadlocks, methods: deprive resources, kill Die one of the threads.
3. What are the Windows memory management methods?
Analysis: segment storage, page storage, segment page storage.
4. What are the states of the process?
Parse:

(1) run (running state): a running process or a process waiting in a waiting queue, the waiting process can run as long as it gets the cpu;
(2) Sleep (interruptible sleep state): equivalent to blocking or waiting state;
(3) D (uninterruptible sleep state): the process on the disk;
(4) T (stop state): this state cannot be seen intuitively, because resources are released after the process stops, so it will not stay In linux;
(5) Z (zombie state): the child process ends with the parent process first, but the parent process does not call wait or waitpid to recycle the resources of the child process, so the child process becomes a zombie process, if the parent process ends If the resource of the child process is still not reclaimed, then process No. 1 will reclaim it.
5. What is the IPC communication method?
Parse:

(1) Pipes (anonymous pipes (process communication of pipe affinity), named pipes (mkfifo/mknod));
(2) Message queues: message-based inter-process communication with no affinity, main functions: msgget, msgsend, msgrecv, msgctl;
(3) Semaphore: equivalent to a mutex, operated by p, v, main functions: semget, semop, semctl;
(4) Shared memory: the fastest inter-process communication, So synchronization is often achieved with set semaphores or mutexes, shmget, shmat, shmdt, shmctl.
6. What is virtual memory?
Analysis: It is to load the process part into the memory, so that a large program can run in a memory smaller than it. Its main implementation is realized by swapping in and out of the program, because 0~ 3G is used by users, and 3~4G is used by memory, which is implemented by mapping to map logical addresses to physical addresses.
7. What is the difference between virtual address, logical address, linear address and physical address?
Resolution: The segmentation mechanism converts a logical address to a linear address; then, the paging mechanism converts a linear address to a physical address.
(1) Virtual address: the address mapped from virtual memory;
(2) Logical address: formed by the segment of the program plus the offset, the address obtained by taking the address in the C/C++ program is the logical address;
(3) Linear address: It is the middle layer from logical address to physical address. Linear address is only available when the paging mechanism is enabled. If there is no paging mechanism, then the linear address is the physical address;
(4) Physical address: It is the actual hardware address in memory.
Logical address (start segment) --> linear address (start paging) --> physical address

8. What are the similarities and differences between synchronous and asynchronous, and under what circumstances are they used?
Resolution:
If the data will be shared between threads. For example, the data being written may be read by another thread later, or the data being read may have already been written by another thread, then these data are shared data and must be synchronized to ensure the correctness of access. Use synchronous programming; use asynchronous programming when the application calls a method on an object that takes a long time to execute and does not want the program to wait for the method to return Often more efficient.

Several questions about threads in Java

1. How many ways are there to implement multithreading? How many ways are there to achieve synchronization?
Analysis:
There are two implementation methods for multi-threading, namely inheriting the Thread class and implementing the Runnable interface.
There are two ways to achieve synchronization, namely synchronized, wait and notify.
wait(): Puts a thread in a waiting state and releases the lock on the object it holds.
sleep(): Makes a running thread sleep, it is a static method, call this method to catch InterruptedException (interrupted exception) exception.
notify(): Wake up a thread in a waiting state. Note that when this method is called, it cannot exactly wake up a thread in a waiting state, but the JVM determines which thread to wake up, and not by priority.
Allnotity(): Wake up all threads in the waiting state. Note that it is not to give all awakened threads an object lock, but to let them compete.

2. Do you use run() or start() to start a thread?
Analysis: To
start a thread is to call the start() method, so that the thread is in a ready state and can be scheduled to run in the future. A thread must be associated with some specific execution code to make sense. The run() method is the execution associated with the thread. code.

3. When a thread enters a synchronized method of an object, can other threads enter other methods of the object?

Analysis: It is divided into the following situations:
(1) Whether the synchronized keyword is added before other methods, if not, it can;
(2) If wait is called inside this method, you can enter other synchronized methods;
(3) If The synchronized keyword is added to the other methods, and wait is not called internally, so it cannot;
(4) If other methods are static, the synchronization lock it uses is the bytecode of the current class, which cannot be synchronized with non-static methods, because Non-static methods use this.

4. The basic concept of thread, the basic state of thread and the relationship between states?
Analysis: A program can execute multiple execution threads at the same time. A thread is an execution thread in the program. Each thread is associated with the code to be executed. There is one thread, the one where the main method executes. If it's just a cpu, how can it execute multiple programs at the same time? This is from a macro perspective. The CPU executes the a thread for a while, and executes the b thread for a while, and the switching time is very fast. It gives the impression that a and b are executing at the same time. It is like everyone surfing the Internet in the same office, and there is only one link to the outside. In fact, this network cable will transmit data for a for a while, and data for b for a while. Since the switching time is very short, everyone feels that they are online at the same time.
Status: ready, running, synchronize blocking, wait and sleep pending, end. wait must be called inside synchronized.
After calling the start method of the thread, the thread enters the ready state, and the thread scheduling system turns the thread in the ready state into the running state. When the synchronized statement is encountered, the running state changes to the blocking state. When the synchronized acquires the lock, the blocking state changes to the running state. In this case, you can call the wait method to change to the suspended state. When the code associated with the thread is executed, the thread becomes the end state.

5. Briefly describe the similarities and differences between synchronized and java.util.concurrent.locks.Lock?
Analysis:
The main point of the same: Lock can complete all the functions achieved by synchronized.
Main difference: Lock has more precise thread semantics and better performance than synchronized. synchronized will automatically release the lock, while Lock must require the programmer to release it manually, and it must be released in the finally clause. Lock has more powerful functions, for example, its tryLock method can acquire the lock in a non-blocking manner.

Guess you like

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