操作系统上机编程2-----unixc实现进程同步

撰写时间:2017.6.10
通过信号量机制和条件变量机制实现进程同步

pc1.c: 使用条件变量解决生产者、计算者、消费者问题

  • 系统中有3个线程:生产者、计算者、消费者
  • 系统中有2个容量为4的缓冲区:buffer1、buffer2
  • 生产者生产’a’、’b’、’c’、‘d’、’e’、’f’、’g’、’h’八个字符,放入到buffer1
  • 计算者从buffer1取出字符,将小写字符转换为大写字符,放入到buffer2
  • 消费者从buffer2取出字符,将其打印到屏幕上

pc2.c: 使用信号量解决生产者、计算者、消费者问题

  • 功能和前面的实验相同,使用信号量解决

条件变量

1.api

初始化条件变量

#include<pthread.h>
int pthread_cond_init(pthread_cond_t*cond,const pthread_condattr_t* attr);
int pthread_cond_destory(pthread_cond_t*cond);

等待条件变量满足

#include<pthread.h>
int pthread_cond_wait(pthread_cond_t* cond,pthread_mutex_t* mutex);

通知条件变量满足

#include<pthread.h>
int pthread_cond_signal(pthread_cond_t* cond);

2.使用

注:所谓的条件变量并没有控制所谓的条件,一般需要我们在外围增加判断条件.所以,条件变量一般的使用方式是:
->最原始

///////#thread1
if(条件不满足){
    pthread_cond_wait(&cond,&mutex);
}

/////#thread2
pthread_cond_signal(&cond)

说明:上述只是最简单的条件变量的实现方式.其中会造成的问题就是:如果程序中运行着多个线程1,那么在if语句判断结束之后,在wait函数执行之前.被中断,而在中断线程中改变条件,使得条件满足,再次切换到thread1中的时候,由于程序已经执行过判断条件.就会出现前后不一致的问题!所以我们需要增加一个互斥量,保护条件中涉及到的变量的安全,也就是保证线程安全!这也就是为什么条件变量要与互斥量共用的原因.
->修改1:

pthread_mutex_t mutex;
pthread_cond_t cond;

#thread1
pthread_mutex_lock(&mutex);
if(条件不满足){
    pthread_cond_wait(&cond,&mutex);
}
pthread_mutex_unlock(&mutex);

/#thread2
pthread_cond_signal(&cond)

运行过程:thread1获得mutex锁->thread1判断条件不满足->thread1调用cond_wait函数(线程挂起+释放mutex锁)->thread2调用signal(唤醒所有的被该条件变量阻塞的进程)->执行结束.
虽然看起来好像已经可以啦,但是还有一个问题没有解决就是使用while还是if的问题.关于这个问题.可以参考下面这篇博客
->修改2

pthread_mutex_t mutex;
pthread_cond_t cond;

#thread1
pthread_mutex_lock(&mutex);
while(条件不满足){
    pthread_cond_wait(&cond,&mutex);
}
pthread_mutex_unlock(&mutex);

/#thread2
pthread_cond_signal(&cond)

pc1.c: 使用条件变量解决生产者、计算者、消费者问题

#include<stdio.h>
#include<pthread.h>
//#include<semaphore.h>

//变量储存区
char buff1[5];
char buff2[5];
int index_buff1 = 0;
int index_buff2 = 0;

pthread_mutex_t buff1_mutex;
pthread_mutex_t buff2_mutex;
pthread_cond_t buff1_empty_cond;
pthread_cond_t buff1_full_cond;
pthread_cond_t buff2_empty_cond;
pthread_cond_t buff2_full_cond;

void init_mutex(){
    pthread_mutex_init(&buff1_mutex,NULL);
    pthread_mutex_init(&buff2_mutex,NULL);
    pthread_cond_init(&buff1_empty_cond,NULL);
    pthread_cond_init(&buff1_full_cond,NULL);
    pthread_cond_init(&buff2_empty_cond,NULL);
    pthread_cond_init(&buff2_full_cond,NULL);
}

void destory_mutex(){
    pthread_mutex_destroy(&buff1_mutex);
    pthread_mutex_destroy(&buff2_mutex);
    pthread_cond_destroy(&buff1_empty_cond);
    pthread_cond_destroy(&buff1_full_cond);
    pthread_cond_destroy(&buff2_empty_cond);
    pthread_cond_destroy(&buff2_full_cond);
}
void *producer(void *arg){

    int index;
    for(index=0;index<8;index++){
        pthread_mutex_lock(&buff1_mutex);
        while(index_buff1>=4){
            pthread_cond_wait(&buff1_empty_cond,&buff1_mutex);
        }
        buff1[index_buff1] = index+'a';
        printf("1--------\n");
        index_buff1++;
        pthread_cond_signal(&buff1_full_cond);
        pthread_mutex_unlock(&buff1_mutex);
    }
    return (void*)0;
}

void *lowToUp(void *arg){

    int index;
    for(index=0;index<8;index++){
        pthread_mutex_lock(&buff1_mutex);
        pthread_mutex_lock(&buff2_mutex);
        while(index_buff1<1){
            pthread_cond_wait(&buff1_full_cond,&buff1_mutex);
        }
        while(index_buff2>=4){
            pthread_cond_wait(&buff2_empty_cond,&buff2_mutex);
        }
        printf("2--------\n");
        index_buff1--;
        buff2[index_buff2] = buff1[index_buff1]-32;
        index_buff2++;
        pthread_cond_signal(&buff1_empty_cond);
        pthread_cond_signal(&buff2_full_cond);      
        pthread_mutex_unlock(&buff2_mutex);
        pthread_mutex_unlock(&buff1_mutex);
    }
    return (void*)0;
}

void *consume(void *arg){
    int index;
    for(index=0;index<8;index++){
        pthread_mutex_lock(&buff2_mutex);
        while(index_buff2<1){
            pthread_cond_wait(&buff2_full_cond,&buff2_mutex);
        }
        index_buff2--;
        printf("3--------%c\n",buff2[index_buff2]);
        pthread_cond_signal(&buff2_empty_cond);
        pthread_mutex_unlock(&buff2_mutex);
    }
    return (void*)0;
}

int main(){
    init_mutex();
    pthread_t produce_thread;
    pthread_t calc_thread;
    pthread_t consume_thread;
    pthread_create(&produce_thread,NULL,producer,NULL);
    pthread_create(&calc_thread,NULL,lowToUp,NULL); 
    pthread_create(&consume_thread,NULL,consume,NULL);

    pthread_join(produce_thread,NULL);
    pthread_join(calc_thread,NULL);
    pthread_join(consume_thread,NULL);
    destory_mutex();
    return 0;
}

信号量

信号量机制就是传统的pv操作.当资源不足的时候等待,当资源充足的时候,等待系统的线程调度.不存在上述条件变量所谓的通知.下面直接贴代码,很简单.

#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<pthread.h>
#include<semaphore.h>

//储存变量
char buff1[5];
char buff2[5];
int index_buff1 = 0;
int index_buff2 = 0;

//定义两个buff的互斥信号量
sem_t buff1_mutex;
sem_t buff2_mutex;
//定义进程内的互斥信号量
sem_t buff1_full;
sem_t buff1_empty;
sem_t buff2_full;
sem_t buff2_empty;

void init_mutex(){
    sem_init(&buff1_mutex,0,1);
    sem_init(&buff2_mutex,0,1);
    sem_init(&buff1_full,0,0);
    sem_init(&buff1_empty,0,4);
    sem_init(&buff2_full,0,0);
    sem_init(&buff2_empty,0,4);
}
void destroy_mutex(){
    sem_destroy(&buff1_mutex);
    sem_destroy(&buff2_mutex);
    sem_destroy(&buff1_full);
    sem_destroy(&buff1_empty);
    sem_destroy(&buff2_full);
    sem_destroy(&buff2_empty);
}


void *producer(void *arg){

    printf("hello world\n");
    int index;
    for(index=0;index<8;index++){
        //printf("%d\n",index);
        sem_wait(&buff1_empty);
        sem_wait(&buff1_mutex);
        buff1[index_buff1] = index+'a';
        //printf("%c\n",buff1[index_buff1]);
        //printf("1--------%c--%d\n",buff1[index_buff1],index_buff1);
        //index_buff2--;
        index_buff1++; 
        sem_post(&buff1_mutex);
        sem_post(&buff1_full);
    }
    return (void*)0;

}

void *lowToUp(void *arg){
    int index;
    for(index=0;index<8;index++){
        sem_wait(&buff1_full);
        sem_wait(&buff2_empty);
        sem_wait(&buff1_mutex);
        sem_wait(&buff2_mutex);
        index_buff1--;
        buff2[index_buff2] = buff1[index_buff1]-32;
        //printf("2-------%c--%d\n",buff2[index_buff2],index_buff2);
        index_buff2++;
        sem_post(&buff2_mutex);
        sem_post(&buff1_mutex);
        sem_post(&buff2_full);
        sem_post(&buff1_empty);
    }
    return (void*)0;
}
void *consume(void *arg){
    int index;
    for(index=0;index<8;index++){
        sem_wait(&buff2_full);
        sem_wait(&buff2_mutex);
        index_buff2--;
        printf("3--------%c \n",buff2[index_buff2]);
        sem_post(&buff2_mutex);
        sem_post(&buff2_empty);
    }
    return (void*)0;
}


int main(){
    pthread_t produce_thread;
    pthread_t calc_thread;
    pthread_t consume_thread;
    printf("hello wrold");
    //初始化所有的信号量
    init_mutex();
    printf("hello wrold\n");

    pthread_create(&produce_thread,NULL,producer,NULL);
    pthread_create(&calc_thread,NULL,lowToUp,NULL);

    pthread_create(&consume_thread,NULL,consume,NULL);
    pthread_join(produce_thread,NULL);
    pthread_join(calc_thread,NULL);
    pthread_join(consume_thread,NULL);
    destroy_mutex();
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实验题目: 生产者消费者(综合性实验) 实验环境: C语言编译器 实验内容: ① 由用户指定要产生的进程及其类别,存入进入就绪队列。    ② 调度程序从就绪队列中提取一个就绪进程运行。如果申请的资源被阻塞则进入相应的等待队列,调度程序调度就绪队列中的下一个进程。进程运行结束时,会检查对应的等待队列,激活队列中的进程进入就绪队列。运行结束的进程进入over链表。重复这一过程直至就绪队列为空。    ③ 程序询问是否要继续?如果要转直①开始执行,否则退出程序。 实验目的: 通过实验模拟生产者消费者之间的关系,了解并掌握他们之间的关系及其原理。由此增加对进程同步的问题的了解。 实验要求: 每个进程有一个进程控制块(PCB)表示。进程控制块可以包含如下信息:进程类型标号、进程系统号、进程状态、进程产品(字符)、进程链指针等等。 系统开辟了一个缓冲区,大小由buffersize指定。 程序中有三个链队列,一个链表。一个就绪队列(ready),两个等待队列:生产者等待队列(producer);消费者队列(consumer)。一个链表(over),用于收集已经运行结束的进程 本程序通过函数模拟信号量的操作。 参考书目: 1)徐甲同等编,计算操作系统教程,西安电子科技大学出版社 2)Andrew S. Tanenbaum著,陈向群,马红兵译. 现代操作系统(第2版). 机械工业出版社 3)Abranham Silberschatz, Peter Baer Galvin, Greg Gagne著. 郑扣根译. 操作系统概念(第2版). 高等教育出版社 4)张尧学编著. 计算操作系统教程(第2版)习题解答与实验指导. 清华大学出版社 实验报告要求: (1) 每位同学交一份电子版本的实验报告,上传到202.204.125.21服务器中。 (2) 文件名格式为班级、学号加上个人姓名,例如: 电子04-1-040824101**.doc   表示电子04-1班学号为040824101号的**同学的实验报告。 (3) 实验报告内容的开始处要列出实验的目的,实验环境、实验内容等的说明,报告中要附上程序代码,并对实验过程进行说明。 基本数据结构: PCB* readyhead=NULL, * readytail=NULL; // 就绪队列 PCB* consumerhead=NULL, * consumertail=NULL; // 消费者队列 PCB* producerhead=NULL, * producertail=NULL; // 生产者队列 over=(PCB*)malloc(sizeof(PCB)); // over链表 int productnum=0; //产品数 int full=0, empty=buffersize; // semaphore char buffer[buffersize]; // 缓冲区 int bufferpoint=0; // 缓冲区指针 struct pcb { /* 定义进程控制块PCB */ int flag; // flag=1 denote producer; flag=2 denote consumer; int numlabel; char product; char state; struct pcb * processlink; …… }; processproc( )--- 给PCB分配内存。产生相应的的进程:输入1为生产者进程;输入2为消费者进程,并把这些进程放入就绪队列中。 waitempty( )--- 如果缓冲区满,该进程进入生产者等待队列;linkqueue(exe,&producertail); // 把就绪队列里的进程放入生产者队列的尾部 void signalempty() bool waitfull() void signalfull() void producerrun() void comsuerrun() void main() { processproc(); element=hasElement(readyhead); while(element){ exe=getq(readyhead,&readytail); printf("进程%d申请运行,它是一个",exe->numlabel); exe->flag==1? printf("生产者\n"):printf("消费者\n"); if(exe->flag==1) producerrun();

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值