2019-2020-1 20175310 "Information security system design basis" the ninth week of learning summary

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_cancelterminate another thread in the same process
  • Call pthread_exittermination own

Pthread_join obtained final state:

  • If the threadthread through the returnreturn value_ptrunit is intended, for the storage of a threadreturn value of the function thread
  • If the threadthread is invoked by another thread pthread_cancelterminates abnormally off value_ptrunit is pointing in the store is constantPTHREAD_CANCELED
  • If the threadthread is their own call pthread_exittermination, value_ptrpointed to the storage unit is passed to pthread_exitparameters

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
  • MutexBy pthread_mutex_tindicating 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 -lpthreadon it

  • Problems encountered 2:
  • Solution:
    ls -l test_drv_loadView 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

Guess you like

Origin www.cnblogs.com/xicyannn/p/11924406.html