美文网首页Kali Linux我用 LinuxLinux
用Pthreads进程共享内存编程

用Pthreads进程共享内存编程

作者: Superbsco | 来源:发表于2017-12-02 11:39 被阅读11次

    更多的可以参考我的博客,也在陆续更新ing
    http://www.hspweb.cn/

    1、用VIM编写创建一个进程的代码,每隔一秒递增输出1-5。

    #include <pthread.h>
    #include <stdio.h>
    void* fun(void* rank){
        int i;
        int my_rank=(int)rank;
        for(i=1;i<=5;i++){
            printf("Thread %d:%d\n",my_rank,i);
            sleep(1);
        }
        pthread_exit(0);
    }
    int main(){
      pthread_t tid1;   //the 2 thread identifier
      int num1=0;
    
      //create the thread
      pthread_create(&tid1,NULL,fun,(void *)num1);
    
      //now wait for the thread to exit
      pthread_join(tid1,NULL);
    }
    
    
    image

    2、用VIM编写创建两个进程的代码,每隔一秒递增输出1-5。

    #include <pthread.h>
    #include <stdio.h>
    void* fun(void* rank){
        int i;
        int my_rank=(int)rank;
        for(i=1;i<=5;i++){
            printf("Thread %d:%d\n",my_rank,i);
            sleep(1);
        }
        pthread_exit(0);
    }
    int main(){
      pthread_t tid1,tid2;  //the 2 thread identifier
      int num1=0,num2=1;
    
      //create the thread
      pthread_create(&tid1,NULL,fun,(void *)num1);
      pthread_create(&tid2,NULL,fun,(void *)num2);
      //now wait for the thread to exit
      pthread_join(tid1,NULL);
      pthread_join(tid2,NULL);
    }
    
    
    image

    3、用VIM编写创建两个进程计算π的代码,观察竞争条件。

    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <math.h>
    int n,thread_count;
    double sum=0.0;
    
    void* Thread_sum(void* rank){
    
        long my_rank=(long)rank;
        double factor;
        long long i;
        long long my_n=n/thread_count;
        long long my_first_i=my_n*my_rank;
        long long my_last_i=my_first_i+my_n;
    
        if(my_first_i % 2==0 ){
            factor=1.0;
        }else{
            factor=-1.0;
        }
        for(i=my_first_i;i<my_last_i;i++,factor=-factor){
            sum+=factor/(2*i+1);
        }
        return NULL;
    }
    
    int main(int argc,char* argv[]){
        long long i;
        //scanf("%d%d",&thread_count,&n);
        thread_count=atoi(argv[1]);
        n=atoi(argv[2]);
        //printf("%d\n%d\n",thread_count,n);
        //printf("%d\n%d\n%d\n",argc,argv[0],argv[1],argv[2]);
        printf("With n=%d terms\n",n);
        pthread_t thread_handles[thread_count];
        for(i=0;i<thread_count;i++){
            pthread_create(&thread_handles[i],NULL,Thread_sum,(void *)i);
        }
        for(i=0;i<thread_count;i++){
            pthread_join(thread_handles[i],NULL);
        }
        printf("our estimate of  pi = %.15lf\n",4*sum);
        sum=0;
        pthread_create(&thread_handles[0],NULL,Thread_sum,(void *)0);
        pthread_join(thread_handles[0],NULL);
        printf("Single thread of pi = %.15lf\n",4*sum);
        printf("                 pi = %.15lf\n",4*atan(1.0));
        return 0; 
    }
    
    
    image

    4、用忙等待互斥(严格轮换法)解决计算π的竞争条件。

    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <math.h>
    #include "timer.h"
    int n,thread_count;
    double sum=0.0;
    int flag=0;
    void* Thread_sum(void* rank){
        long my_rank=(long)rank;
        double factor,my_sum=0.0;
        long long i;
        long long my_n=n/thread_count;
        long long my_first_i=my_n*my_rank;
        long long my_last_i=my_first_i+my_n;
    
        if(my_first_i % 2==0 ){
            factor=1.0;
        }else{
            factor=-1.0;
        }
        for(i=my_first_i;i<my_last_i;i++,factor=-factor){
            my_sum+=factor/(2*i+1);
        }
        while(flag!=my_rank);
        sum+=my_sum;
        flag=(flag+1)%thread_count;
        // pthread_exit(NULL);
        return NULL;
    }
    int main(int argc,char* argv[]){
        double start, finish, elapsed;
        long long i;
        //scanf("%d%d",&thread_count,&n);
        thread_count=atoi(argv[1]);
        n=atoi(argv[2]);
        //printf("%d\n%d\n",thread_count,n);
        //printf("%d\n%d\n%d\n",argc,argv[0],argv[1],argv[2]);
        printf("With n=%d terms\n",n);
        pthread_t thread_handles[thread_count];
        GET_TIME(start);
        for(i=0;i<thread_count;i++){
            pthread_create(&thread_handles[i],NULL,Thread_sum,(void *)i);
        }
        for(i=0;i<thread_count;i++){
            pthread_join(thread_handles[i],NULL);
        }
        GET_TIME(finish);
        printf("    Multi-threaded estimate of  pi = %.15lf\n",4*sum);
        elapsed = finish - start;
        printf("    Elapsed time = %e seconds\n", elapsed);
    
        sum=0;
        GET_TIME(start);
        pthread_create(&thread_handles[0],NULL,Thread_sum,(void *)0);
        pthread_join(thread_handles[0],NULL);
        GET_TIME(finish);
        printf("    Single-threaded estimate of pi = %.15lf\n",4*sum);
        elapsed = finish - start;
        printf("    Elapsed time= %e seconds\n",elapsed);
        printf("    Math library estimate of    pi = %.15lf\n",4*atan(1.0));
        return 0;
    }
    
    
    image

    5、用Peterson解法解决计算π的竞争条件。

    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <math.h>
    #include "timer.h"
    int n,thread_count;
    double sum=0.0;
    
    int turn;
    int interested[2];
    
    void* Thread_sum(void* rank){
        long my_rank=(long)rank;
        double factor,my_sum=0.0;
        long long i;
        long long my_n=n/thread_count;
        long long my_first_i=my_n*my_rank;
        long long my_last_i=my_first_i+my_n;
    
        int other;
        other=(my_rank+1)%2;
        interested[my_rank]=1;
    
        if(my_first_i % 2==0 ){
            factor=1.0;
        }else{
            factor=-1.0;
        }
        for(i=my_first_i;i<my_last_i;i++,factor=-factor){
            my_sum+=factor/(2*i+1);
        }
        turn=my_rank;
        while(turn==my_rank && interested[other]==1);
        sum+=my_sum;
        interested[my_rank]=0;
        // pthread_exit(NULL);
        return NULL;
    }
    int main(int argc,char* argv[]){
        double start, finish, elapsed;
        long long i;
        //scanf("%d%d",&thread_count,&n);
        thread_count=atoi(argv[1]);
        n=atoi(argv[2]);
        //printf("%d\n%d\n",thread_count,n);
        //printf("%d\n%d\n%d\n",argc,argv[0],argv[1],argv[2]);
        printf("With n=%d terms\n",n);
        pthread_t thread_handles[thread_count];
        GET_TIME(start);
        for(i=0;i<thread_count;i++){
            pthread_create(&thread_handles[i],NULL,Thread_sum,(void *)i);
        }
        for(i=0;i<thread_count;i++){
            pthread_join(thread_handles[i],NULL);
        }
        GET_TIME(finish);
        printf("     Peterson estimate of       pi = %.15lf\n",4*sum);
        elapsed = finish - start;
        printf("    Elapsed time = %e seconds\n", elapsed);
    
        sum=0;
        GET_TIME(start);
        pthread_create(&thread_handles[0],NULL,Thread_sum,(void *)0);
        pthread_join(thread_handles[0],NULL);
        GET_TIME(finish);
        printf("    Single-threaded estimate of pi = %.15lf\n",4*sum);
        elapsed = finish - start;
        printf("    Elapsed time= %e seconds\n",elapsed);
        printf("    Math library estimate of    pi = %.15lf\n",4*atan(1.0));
        return 0;
    }
    
    
    image

    6、用互斥量解决计算π的竞争条件。

    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <math.h>
    #include "timer.h"
    int n,thread_count;
    double sum=0.0;
    pthread_mutex_t mutex;
    
    void* Thread_sum(void* rank){
        long my_rank=(long)rank;
        double factor,my_sum=0.0;
        long long i;
        long long my_n=n/thread_count;
        long long my_first_i=my_n*my_rank;
        long long my_last_i=my_first_i+my_n;
    
        if(my_first_i % 2==0 ){
            factor=1.0;
        }else{
            factor=-1.0;
        }
        for(i=my_first_i;i<my_last_i;i++,factor=-factor){
            my_sum+=factor/(2*i+1);
        }
        pthread_mutex_lock(&mutex);
        sum+=my_sum;
        pthread_mutex_unlock(&mutex);
    
        // pthread_exit(NULL);
        return NULL;
    }
    int main(int argc,char* argv[]){
        double start, finish, elapsed;
        long long i;
        //scanf("%d%d",&thread_count,&n);
        thread_count=atoi(argv[1]);
        n=atoi(argv[2]);
        //printf("%d\n%d\n",thread_count,n);
        //printf("%d\n%d\n%d\n",argc,argv[0],argv[1],argv[2]);
        printf("With n=%d terms\n",n);
        pthread_t thread_handles[thread_count];
        GET_TIME(start);
         for(i=0;i<thread_count;i++){
            pthread_create(&thread_handles[i],NULL,Thread_sum,(void *)i);
        }
        for(i=0;i<thread_count;i++){
            pthread_join(thread_handles[i],NULL);
        }
        GET_TIME(finish);
        printf("                   estimate of  pi = %.15lf\n",4*sum);
        elapsed = finish - start;
        printf("    Elapsed time = %e seconds\n", elapsed);
    
        sum=0;
        GET_TIME(start);
        pthread_create(&thread_handles[0],NULL,Thread_sum,(void *)0);
        pthread_join(thread_handles[0],NULL);
        GET_TIME(finish);
        printf("    Single-threaded estimate of pi = %.15lf\n",4*sum);
        elapsed = finish - start;
        printf("    Elapsed time= %e seconds\n",elapsed);
        printf("    Math library estimate of    pi = %.15lf\n",4*atan(1.0));
        return 0;
    }
    
    
    image

    7、用信号量实现互斥解决计算π的竞争条件。

    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <semaphore.h>
    #include <math.h>
    #include "timer.h"
    int n,thread_count;
    double sum=0.0;
    
    sem_t sem1,sem2;
    
    void* Thread_sum(void* rank){
        long my_rank=(long)rank;
        double factor,my_sum=0.0;
        long long i;
        long long my_n=n/thread_count;
        long long my_first_i=my_n*my_rank;
        long long my_last_i=my_first_i+my_n;
    
        if(my_first_i % 2==0 ){
            factor=1.0;
        }else{
            factor=-1.0;
        }
        for(i=my_first_i;i<my_last_i;i++,factor=-factor){
            my_sum+=factor/(2*i+1);
        }
        if(my_rank==0)
            sem_wait(&sem1);
        else
            sem_wait(&sem2);
        sum+=my_sum;
        if(my_rank == 0)
            sem_post(&sem2);
        else
            sem_post(&sem1);
        // pthread_exit(NULL);
        return NULL;
    }
    int main(int argc,char* argv[]){
        double start, finish, elapsed;
        long long i;
        //scanf("%d%d",&thread_count,&n);
        thread_count=atoi(argv[1]);
        n=atoi(argv[2]);
        //printf("%d\n%d\n",thread_count,n);
        //printf("%d\n%d\n%d\n",argc,argv[0],argv[1],argv[2]);
        printf("With n=%d terms\n",n);
        pthread_t thread_handles[thread_count];
        GET_TIME(start);
        sem_init(&sem1,0,1);
        sem_init(&sem2,0,0);
        for(i=0;i<thread_count;i++){
            pthread_create(&thread_handles[i],NULL,Thread_sum,(void *)i);
        }
        for(i=0;i<thread_count;i++){
            pthread_join(thread_handles[i],NULL);
        }
        sem_destroy(&sem1);
        sem_destroy(&sem2);
        GET_TIME(finish);
        printf("                   estimate of  pi = %.15lf\n",4*sum);
        elapsed = finish - start;
        printf("    Elapsed time = %e seconds\n", elapsed);
    
        sum=0;
        GET_TIME(start);
        pthread_create(&thread_handles[0],NULL,Thread_sum,(void *)0);
        pthread_join(thread_handles[0],NULL);
        GET_TIME(finish);
        printf("    Single-threaded estimate of pi = %.15lf\n",4*sum);
        elapsed = finish - start;
        printf("    Elapsed time= %e seconds\n",elapsed);
        printf("    Math library estimate of    pi = %.15lf\n",4*atan(1.0));
        return 0;
    }
    
    
    image

    8、修改信号量的初值,调换上述2中的线程顺序。

    #include <pthread.h>
    #include <stdio.h>
    #include <semaphore.h>
    
    sem_t sem1,sem2;
    void* fun(void* rank){
        int i;
        int my_rank=(int)rank;
        for(i=1;i<=5;i++){
          if(my_rank==0)
            sem_wait(&sem1);
          else
            sem_wait(&sem2);
            printf("Thread %d:%d\n",my_rank,i);
            sleep(1);
          if(my_rank==0)
            sem_post(&sem2);
          else
            sem_post(&sem1);
        }
        pthread_exit(0);
    }
    int main(){
      pthread_t tid1,tid2;  //the 2 thread identifier
      int num1=0,num2=1;
    
      sem_init(&sem1,0,1);
      sem_init(&sem2,0,0);
      //create the thread
      pthread_create(&tid1,NULL,fun,(void *)num1);
      pthread_create(&tid2,NULL,fun,(void *)num2);
      //now wait for the thread to exit
      pthread_join(tid1,NULL);
      pthread_join(tid2,NULL);
    
      sem_destroy(&sem1);
      sem_destroy(&sem2);
    }
    
    
    image

    相关文章

      网友评论

        本文标题:用Pthreads进程共享内存编程

        本文链接:https://www.haomeiwen.com/subject/rphnbxtx.html