C pointer principle (47)-C application skills

The delegation model, that is, there is a boss thread, that is, the main thread, which generates the woker thread, and the boss thread and the worker thread execute concurrently.
The main task of BOSS thread is to create worker thread, put the worker thread into the queue, and wake up the worker thread when there is work to be processed.
/ Create a new thread, starting with execution of START-ROUTINE getting passed ARG. Creation attributed come from ATTR. The new handle is stored in NEWTHREAD. /
extern int pthread_create (pthread_t restrict newthread,
const pthread_attr_t *restrict attr,
void (start_routine) (void ),
void restrict arg) THROW nonnull ((1, 3));
/ Obtain the identifier of the current thread. /
extern pthread_t pthread_self (void) THROW attribute ((const));
//Returns the pthread? T structure pointer of the current thread calling the function
/ Make calling thread wait for termination of the thread TH. The
exit status of the thread is stored in THREAD_RETURN, if THREAD_RETURN
is not NULL.
This function is a cancellation point and therefore not marked with
THROW. */
Extern int pthread \ u join (pthread \ t th, void * * thread \ u return); / / thread \ u return exit status
//Pthread Ou join causes the calling thread to suspend its execution until the end of the target thread.
main.c

#include <pthread.h>
#include <stdio.h>
 //Two worker threads, accumulation and multiplication
void *mycompadd(void *xx){//Argument must be void * and then cast
  int sum=0; 
  int *x=(int *)(xx);
  for (int i=0;i<*x;i++){
    sum+=i;
  }
  printf("add%d\n",sum);    
}
void  *mycompchen(void *xx){//Argument must be void * and then cast
  int sum=1; 
  int *x=(int *)(xx);
  for (int i=1;i<=*x;i++){
    sum*=i;  
  }
  printf("chen%d\n",sum);   
}
 
 
int main(){
  //main is the boss thread,
  pthread_t threada,threadb;
  //Create worker thread and execute thread
  int n=3;
  pthread_create(&threada,NULL,mycompadd,&n);//Thread, thread property, function, parameter. If there are more than one parameter, the structure pointer must be passed
  pthread_create(&threadb,NULL,mycompchen,&n);//Thread, thread property, function, parameter
  //wait worker thread and merge it into BOSS thread
  pthread_join(threada,NULL);
  pthread_join(threadb,NULL);
  return(0);
}

Execution effect:
deepfuture@deepfuture-laptop:~/mytest$ gcc -lpthread -std=c99 -o main main.c
deepfuture@deepfuture-laptop:~/mytest$ ./main
add3
chen6

deepfuture@deepfuture-laptop:~/mytest$ 

C - multithreading - cancel and cancel point

 
Thread canceling
Compile:
gcc -std=c99 -lpthread -o main main.c
 
deepfuture@deepfuture-laptop:~/mytest$ ./main
10000print:250
10000print:500
10000print:750
1add1
1chen1
thread0 has been cancelled!
thread1 has been cancelled!
2chen2
3chen6
4chen24
5chen120
6chen720
7chen5040
8chen40320
9chen362880
10chen3628800
11chen39916800
12chen479001600
13chen1932053504
14chen1278945280
15chen2004310016
16chen2004189184
17chen-288522240
18chen-898433024
19chen109641728
20chen-2102132736
21chen-1195114496
22chen-522715136
23chen862453760
24chen-775946240
25chen2076180480
thread2 cannot be cancelled! br/>deepfuture@deepfuture-laptop:~/mytest$ 
 
C code

#include <pthread.h>  
#include <stdio.h>  
  
#define MAXTHREADS 3   
  
void *mycompprint(void *xx){//Argument must be void * and then cast
  int oldstate,oldtype;    
  pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,&oldstate);//Setting up a thread can be aborted.  
  pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,&oldtype);//Set thread delay to abort, and pthread? Cancel? Deferred is the default value.  
  int *x=(int *)(xx);    
  for (int i=1;i<*x;i++){  
    if ((i%250)==0) {//Cancel if i is a multiple of 250
     printf("%dprint:%d\n",*x,i);       
     pthread_testcancel();//pthread_testcancel() detects whether cancellation is needed. Set the cancellation point. If there is a pending cancellation request, terminate this thread here
    }      
  }  
}  
  
  
void *mycompadd(void *xx){//Argument must be void * and then cast
  int oldstate,oldtype;    
  pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,&oldstate);//Setting up a thread can be aborted.  
  pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,&oldtype);//Set thread thread to terminate immediately. Pthread? Cancel? Asynchronous means thread to terminate immediately.  
  int sum=0;   
  int *x=(int *)(xx);  
  int y;  
  for (int i=1;i<=*x;i++){  
    sum+=i;   
    printf("%dadd%d\n",i,sum);      
  }  
  
}  
  
  
  
  
void  *mycompchen(void *xx){//Argument must be void * and then cast
  int oldstate,oldtype;    
  pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,&oldstate);//The setting thread cannot be aborted.    
  int sum=1;   
  int *x=(int *)(xx);  
  for (int i=1;i<=*x;i++){  
    sum*=i;      
    printf("%dchen%d\n",i,sum);        
  }  
  
}  
  
  
int main(){  
  //Cannot merge after thread separation
  //main is the boss thread
   pthread_t threads[MAXTHREADS];//Create thread pool
  void *status;  
  //Create worker thread and execute thread
  int n1=25;  
  int n2=10000;  

    
  
  pthread_create(&(threads[0]),NULL,mycompprint,&n2);     
  pthread_create(&(threads[1]),NULL,mycompadd,&n1);   
  pthread_create(&(threads[2]),NULL,mycompchen,&n1);   
    
  for (int i=0;i<MAXTHREADS;i++){  
       pthread_cancel(threads[i]);    
  }    
  
  for (int i=0;i<MAXTHREADS;i++){  
       pthread_join(threads[i],&status);  //wait worker thread, and merge it into BOSS thread
       if (status==PTHREAD_CANCELED){  
        printf("thread%d Cancelled!\n",i);  
       }  
       else{  
        printf("thread%d Can't be cancelled!\n",i);  
       }      
         
  }   
  return(0);  
}  
 

c - multithreading - clean up before abort
gcc -lpthread -std=c99 -o main main.c

deepfuture@deepfuture-laptop:~/mytest$ ./main
1chen1
2chen2
3chen6
4chen24
5chen120
6chen720
7chen5040
8chen40320
9chen362880
10chen3628800
11chen39916800
12chen479001600
13chen1932053504
14chen1278945280
15chen2004310016
16chen2004189184
17chen-288522240
18chen-898433024
19chen109641728
20chen-2102132736
21chen-1195114496
22chen-522715136
23chen862453760
24chen-775946240
25chen2076180480
1add1
10000print:250
clear:10000
thread0 has been cancelled!
thread1 has been cancelled!
thread2 cannot be cancelled!

 
C code

#include <pthread.h>  
#include <stdio.h>  
  
#define MAXTHREADS 3   
void *myclear(void *x){  
   printf("clear:%d\n",*((int*)x));  
}  
void *mycompprint(void *xx){//Argument must be void * and then cast
  int oldstate,oldtype;    
  pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,&oldstate);//Setting up a thread can be aborted.  
  pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,&oldtype);//Set thread delay to abort, and pthread? Cancel? Deferred is the default value.  
  int *x=(int *)(xx);    
  void *xxx=(void *)x;  
  pthread_cleanup_push(myclear,xxx);//Push the thread cleanup stack, which contains a pointer to the routine to be executed during the cancellation process, that is, to perform a cleanup before aborting. myclear is the routine name, x is the parameter passed to the routine
  for (int i=1;i<*x;i++){  
    if ((i%250)==0) {//Cancel if i is a multiple of 250
     printf("%dprint:%d\n",*x,i);       
     pthread_testcancel();//pthread_testcancel() detects whether cancellation is needed. Set the cancellation point. If there is a pending cancellation request, terminate this thread here
    }      
  }  
  pthread_cleanup_pop(0); //Remove the cleanup function pointer from the top of the call thread cleanup stack, but do not execute it. pthread_testcancel() does not detect a cancel request, which means there is no need to cancel at present, so remove it. Pthread ﹣ cleanup ﹣ pop (1) moves and executes it, even if it does not abort the thread
}  
  
  
void *mycompadd(void *xx){//Argument must be void * and then cast
  int oldstate,oldtype;    
  pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,&oldstate);//Setting up a thread can be aborted.  
  pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,&oldtype);//Set thread thread to terminate immediately. Pthread? Cancel? Asynchronous means thread to terminate immediately.  
  int sum=0;   
  int *x=(int *)(xx);  
  int y;  
  for (int i=1;i<=*x;i++){  
    sum+=i;   
    printf("%dadd%d\n",i,sum);      
  }  
  
}  
  
  
  
  
void  *mycompchen(void *xx){//Argument must be void * and then cast
  int oldstate,oldtype;    
  pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,&oldstate);//The setting thread cannot be aborted.    
  int sum=1;   
  int *x=(int *)(xx);  
  for (int i=1;i<=*x;i++){  
    sum*=i;      
    printf("%dchen%d\n",i,sum);        
  }  
  
}  
  
  
int main(){  
  //Cannot merge after thread separation
  //main is the boss thread
   pthread_t threads[MAXTHREADS];//Create thread pool
  void *status;  
  //Create worker thread and execute thread
  int n1=25;  
  int n2=10000;  

    
  
  pthread_create(&(threads[0]),NULL,mycompprint,&n2);     
  pthread_create(&(threads[1]),NULL,mycompadd,&n1);   
  pthread_create(&(threads[2]),NULL,mycompchen,&n1);   
    
  for (int i=0;i<MAXTHREADS;i++){  
       pthread_cancel(threads[i]);    
  }    
  
  for (int i=0;i<MAXTHREADS;i++){  
       pthread_join(threads[i],&status);  //wait worker thread, and merge it into BOSS thread
       if (status==PTHREAD_CANCELED){  
        printf("thread%d Cancelled!\n",i);  
       }  
       else{  
        printf("thread%d Can't be cancelled!\n",i);  
       }      
         
  }   
  return(0);  
}  

linux thread priority

C code

#include <pthread.h>  
#include <stdio.h>  
  
#define MAXTHREADS 3   
void *myclear(void *x){  
   printf("clear:%d\n",*((int*)x));  
}  
void *mycompprint(void *xx){//Argument must be void * and then cast
  int oldstate,oldtype;    
  pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,&oldstate);//Setting up a thread can be aborted.  
  pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,&oldtype);//Set thread delay to abort, and pthread? Cancel? Deferred is the default value.  
  int *x=(int *)(xx);    
  void *xxx=(void *)x;  
  pthread_cleanup_push(myclear,xxx);//Push the thread cleanup stack, which contains a pointer to the routine to be executed during the cancellation process, that is, to perform a cleanup before aborting. myclear is the routine name, x is the parameter passed to the routine
  for (int i=1;i<*x;i++){  
    if ((i%60)==0) {//Cancel if i is a multiple of 250
     printf("%dprint:%d\n",*x,i);       
     pthread_testcancel();//pthread_testcancel() detects whether cancellation is needed. Set the cancellation point. If there is a pending cancellation request, terminate this thread here
    }      
  }  
  pthread_cleanup_pop(0); //Remove the cleanup function pointer from the top of the call thread cleanup stack, but do not execute it. pthread_testcancel() does not detect a cancel request, which means there is no need to cancel at present, so remove it. Pthread ﹣ cleanup ﹣ pop (1) moves and executes it, even if it does not abort the thread
}  
  
  
void *mycompadd(void *xx){//Argument must be void * and then cast
  int oldstate,oldtype;    
  int sum=0;   
  int *x=(int *)(xx);  
  int y;  
  pthread_attr_t attr1;  
  pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,&oldstate);//The setting thread cannot be aborted.    
  for (int i=1;i<=*x;i++){  
    sum+=i;   
    printf("%dadd%d\n",i,sum);      
  }  
}  
  
  
  
  
void  *mycompchen(void *xx){//Argument must be void * and then cast
  int oldstate,oldtype;    
  
  size_t size;    
  void *addr;  
  int priority;    
  pthread_attr_t attr1;  
  struct sched_param param;  
    
  pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,&oldstate);//The setting thread cannot be aborted.  
     
  pthread_getattr_np(pthread_self(),&attr1);//Gets the thread properties.  
     
  pthread_attr_getstack(&attr1,&addr,&size);//Thread properties, address, size
  param.sched_priority=sched_get_priority_min(SCHED_RR);//Sched get priority min of sched RR policy
  pthread_setschedparam(pthread_self(),SCHED_RR,&param);//Set scheduling policy dynamically
//Pthread ﹐ setschedprio (pthread ﹐ self(), sched ﹐ get ﹐ priority ﹐ min (sched ﹐ RR)); / / another way to set scheduling priority dynamically
  
  printf("size:%d\n",size); //Output thread stack size
  
  
  int sum=1;   
  int *x=(int *)(xx);  
  for (int i=1;i<=*x;i++){  
    sum*=i;      
    printf("%dchen%d\n",i,sum);        
  }  
  
}  
  
  
int main(){  
  //Cannot merge after thread separation
  //main is the boss thread
  pthread_t threads[MAXTHREADS];//Create thread pool
  void *status;  
  pthread_attr_t attr;  
  //Create worker thread and execute thread
  int n1=25;  
  int n2=10000;  
  int priority;    
  struct sched_param param;  
  //Statically set thread scheduling and related properties. The lower the priority value, the higher the priority value
  pthread_attr_init(&attr);  
  priority=sched_get_priority_max(SCHED_RR);//Sched get priority Max priority of sched RR policy
//Sched'rrpolling scheduling, sched'fifo first in first out, thread execution until completion, sched'other scheduling. Sched get privacy max, sched get privacy min get the maximum priority value and the minimum priority value of the scheduling policy
  param.sched_priority=priority;//Set priority member of param
  
  pthread_attr_setschedparam(&attr,&param);//Set priority through param
    

  
  pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);//Pthread? Explicit? Sched sets the scheduling attribute as the scheduling attribute of the attribute object, and thread? Inherit? Explicit? Sched is the inherited scheduling attribute
  pthread_create(&(threads[0]),NULL,mycompprint,&n2);     
  pthread_create(&(threads[1]),&attr,mycompadd,&n1);     
  pthread_create(&(threads[2]),NULL,mycompchen,&n1); \  
   
  for (int i=0;i<MAXTHREADS;i++){  
       pthread_cancel(threads[i]);    
  }   
   sleep(1);   
  for (int i=0;i<MAXTHREADS;i++){  
pthread_join(threads[i],&status);  //wait worker thread, and merge it into BOSS thread
       if (status==PTHREAD_CANCELED){  
        printf("thread%d Cancelled!\n",i);  
       }  
       else{  
        printf("thread%d Can't be cancelled!\n",i);  
       }      
         
  }   
  return(0);  

}  

linux-C calls SO dynamic library directly and generates functions of SO dynamic library

C code

#include <stdio.h>  
#include <dlfcn.h>  
  
int main(void){  
   int (*myadd)(int a,int b);//fuction pointer  
   void *handle;  
     
   handle=dlopen("./libmyadd.so",RTLD_LAZY);//open lib file  
   myadd=dlsym(handle,"output");//call dlsym function  
     
  
   int result=myadd(1,2);  
   dlclose(handle);  
   printf("%d\n",result);    
}  

The above is the calling program test8.c, and the following is the library program test7.c
C code
int output(int a,int b){  
   int x=a+b;  
   return x;  
}  
 knoppix@Microknoppix:/mnt-system/deepfuture$ gcc -shared -o libmyadd.so test7.c
knoppix@Microknoppix:/mnt-system/deepfuture$ gcc -ldl -o test8 test8.c
knoppix@Microknoppix:/mnt-system/deepfuture$ ./test8
3

Tags: Programming Attribute Linux

Posted on Fri, 06 Dec 2019 17:39:52 -0500 by maff20