2019-2020-1 20175310 "Information security system design basis" the ninth week of learning summary
Multithreading
Universal function
void * func( void * parameter)
typedef void* (*uf)(void * para)
interface
- What do indicate the module
- Identifier, type, function, etc.
- *.h
- Function caller
achieve
- How to indicate the completion of the interface module
- More than one interface (possible)
- *.c
- Function implementer
Function signature (Function Signature)
- Function name, its number of parameters, parameter types
- Note that the return value is not included
- Function signature for identifying different functions, like the signature of the same person used to identify different
Multithreading
Single-threaded
Complicated by
- Program-level concurrency - process
- Function-level concurrent - thread
sayhello1.c
#include <stdio.h>
#define NUM 5
void print_msg(char *m)
{
int i;
for (i=0;i<NUM;i++){
printf("%s",m);
fflush(stdout);
sleep(1);
}
}
int main(){
print_msg("hello");
print_msg("world\n");
}
Multithreading
termination
- Terminate a thread without terminating the entire process
- From the thread function
return
- Call to
pthread_cancel
terminate another thread in the same process - Call
pthread_exit
termination own
Pthread_join obtained final state:
- If the
thread
thread through thereturn
returnvalue_ptr
unit is intended, for the storage of athread
return value of the function thread - If the
thread
thread is invoked by another threadpthread_cancel
terminates abnormally offvalue_ptr
unit is pointing in the store is constantPTHREAD_CANCELED
- If the
thread
thread is their own callpthread_exit
termination,value_ptr
pointed to the storage unit is passed topthread_exit
parameters
Critical Resources
- Allow only one process (thread) use of resources
Printer, variables, data, etc.
Exclusive
- Indirect constraint relations
When a process enters a critical region using critical resources, another process (thread) must wait, when occupation critical resource process exits the critical region, another process is allowed to access this critical resource.
Synchronize
- Directly restrict relations
To accomplish certain tasks and the establishment of two or more processes (threads), these processes (threads) because of the need to coordinate their work order at certain positions and wait for the transfer restriction relationship information generated.
MutexMutex,Mutual Exclusive Lock
- Thread acquires the lock can be done, "read - modify - write" operation to release the lock and then another thread
- Thread does not get the lock can only wait and can not access the shared data
- "Read - modify - write" an atomic operation consisting of three operations, are either executed or not executed, the execution is not interrupted intermediate, this operation is not done in parallel on another processor
Mutex
Bypthread_mutex_t
indicating the type of a variable
Threads and processes
Features | process | Thread |
---|---|---|
create | fork | pthread_create |
wait | wait/waitpid | pthread_join |
termination | exit/_exit | pthread_exit |
ID | getpid/getppid | pthread_self |
Exclusive | semaphore | mutex/semaphore |
Synchronize | semaphore | cond var/semaphore |
Problem of debugging code
- Problems encountered 1: When running multithreaded code error
对‘pthread_create’未定义的引用
对‘pthread_join’未定义的引用
Solution: Add a parameter at compile time
-lpthread
on it
- Problems encountered 2:
Solution:
ls -l test_drv_load
View the system and some group, then the group name in the script was changed, some group system.
summarize
- Because the first two weeks of the experiment too much, has not write a blog, a lot of fall
- Some in class by class time to finish the test, but also spent time after school to study
- Still need to seriously study, once the fall curriculum will need to spend more time to make up
Code hosting
Reference material
Textbook Introduction and weekly examinations focus on
"in-depth understanding of computer systems V2" study guide
ingenious method of learning Linux system calls