linux下的互斥和同步


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <pthread.h>
#include </usr/include/bits/pthreadtypes.h>
 #include <sys/stat.h>
 #include <fcntl.h>

pthread_mutex_t mutex1;
pthread_cond_t cond1;

FILE* fp;
void* fun1(void){
    printf("fun1 exit is called !\n");
}

void* fun2(void){
    printf("fun2 exit is called!\n");
}
//thread 2 progress
void pthread1_prc1(void){    
    char buffer[1024];  //定义一个buffer装数据
    char path[50]="/home/dzk/Documents/test_proj/readme.txt";  //我的文件路径,你们看着改
    printf("%s\n",path);
    //lock initialize    
    int ret = pthread_mutex_lock(&mutex1);    
    //open file
    fp=NULL;
    fp=fopen(path,"r");
       fread(buffer,200, 1, fp);
       printf("this is thread1:%s\n", buffer); /* 读取并显示数据 */
       fclose(fp);

    //cond signal
    pthread_cond_signal(&cond1);  //这里是cond信号哟,

//这里发出信号->另外一个线程接收这个信号->接受到信号后才能执行下去

    //unlock
    ret = pthread_mutex_unlock(&mutex1); //文件处理完了,就要解锁,释放

    pthread_exit(0);
    
}


//thread 2 progress
void pthread1_prc2(void){    
    char buffer[1024];
    char path[50]="/home/dzk/Documents/test_proj/readme.txt";
    printf("%s\n",path);
    

    //lock initialize    
    int ret = pthread_mutex_lock(&mutex1);
     pthread_cond_wait(&cond1, &mutex1); 

//这里收到信号然后卡住了哟,知道进程1结束了,这里才卡结束,然后继续往下走

    //open file
    fp=NULL;
    fp=fopen(path,"w+");
    char str[] = "This is dzk.com";
    fwrite(str, sizeof(str) , 1, fp );
       fclose(fp);
       printf("thread2 write is over!\n");

    //unlock
    ret = pthread_mutex_unlock(&mutex1);//解锁

    pthread_exit(0);
    
}

int main(){
    printf("real uid is %d,real gid is %d!\n",getuid(),getgid());

    atexit(fun1()); //这两个是exit函数不用关心
    atexit(fun2());

    pid_t pid1, pid2;
    pid1=fork(); //创建两个进程
    pid2=fork();
    pthread_t  thread1, thread2; //两个线程

    /lock
    if(pthread_mutex_init(&mutex1, NULL)==-1)
    {
        perror("mutex init error!\n");
        pthread_exit(0);
    }    
    cond
    
    int ret=pthread_cond_init(&cond1,NULL);
    
    ///
    if(pid2==0){ //如果pid2==0那么意味着我们现在在线程2里面
        printf("thread2 is working!\n");
        int thread1_id=pthread_create(&thread1,NULL,(void*)pthread1_prc1,NULL); //开始吧两个线程跑起来
        int thread2_id=pthread_create(&thread2,NULL,(void*)pthread1_prc2,NULL);
        
    }
    
    if(pid1==-1){    
        printf("pid = -1 ;fork error!\n");
        exit(0);
    }
    else if(pid1==0){
        int i=0;
    
        while(1){
            printf("pid=0,in child process!\n");
            sleep(1);
            i++;
            if(i==5) exit(0);
        }
        
        exit(0);
    }
    main thread
        if(waitpid(pid1,NULL,0)==-1)  //这是进程1的wait函数,是阻塞运行的哟
    {
        printf("waitpid is error!\n");
        exit(0);
    };
    if(pthread_join(thread2,NULL)==-1){ //这是线程的join函数
    
        printf("thread join is error!\n");
        exit(0);
    }

 if(pthread_join(thread1,NULL)==-1){ //这是线程的join函数
    
        printf("thread join is error!\n");
        exit(0);
    }
    printf("pid = %d; in father process!\n",getpid());
    return 0;

    //destory cond
    ret = pthread_cond_destroy(&cond1);
    
    //destory lock
    ret = pthread_mutex_destroy(&mutex1);
    exit(0);

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值