일반적인 프로세스 간 통신 IPC 문제 - 생산자 소비자 문제
#include <stdio.h>
#include <pthread.h>
#define MAX 10000000000 //       ,       
pthread_mutex_t  the_mutex;
pthread_cond_t  condc,condp;
int buffer=0;
void *product(void *ptr)
{
    int i;
    for(i=1;i<MAX;++i)
    {
        pthread_mutex_lock(&the_mutex);//       
        while(buffer!=0)
            pthread_cond_wait(&condp,&the_mutex);
        buffer=i;//       
        pthread_cond_signal(&condc);//     
        pthread_mutex_unlock(&the_mutex);//     
    }
    pthread_exit;
}
void *consumer(void *ptr)
{
    int i;
    for(i=1;i<MAX;++i)
    {
        pthread_mutex_lock(&the_mutex);//    
        while(buffer==0)
            pthread_cond_wait(&condc,&the_mutex);
        buffer=0;
        pthread_cond_signal(&condp);//       
        pthread_mutex_unlock(&the_mutex);
    }
    pthread_exit(0);
}
int main(int argc,char ** argv)
{
    pthread_t  pro,con;
    pthread_mutex_init(&the_mutex,0);
    pthread_cond_init(&condp,0);
    pthread_cond_init(&condc,0);
    pthread_create(&con,0,consumer,0);
    pthread_create(&pro,0,product,0);
    pthread_join(pro,0);
    pthread_join(con,0);
    pthread_cond_destroy(&condc);
    pthread_cond_destroy(&condp);
    pthread_mutex_destroy(&the_mutex);
}main 함수에는 스레드 처리 문제를 처리할 때의 기본 조작이 포함되어 있으며, 스레드 생성, 상호 배율 성명, 스레드 등록의 기본 API 함수를 포함한다.이 실례는 제12장 스레드 제어를 참고할 수 있다.
4
4
4
4
4
사라진 자물쇠는 어떻게 피할 수 있습니까?마찬가지로 네 가지 방법이 있다. - 이 문제를 소홀히 한다. 만약 네가 그를 소홀히 한다면 그도 너를 소홀히 할 것이다. 유명한 타조 알고리즘이 아니다.그러나 이런 생각은 바로 죽은 자물쇠를 검측하고 회복하며 죽은 자물쇠가 발생했는지 검측하고 일단 발생하면 조치를 취해 구제하고 양을 잃고 외양간을 고치는 것이다.자원을 꼼꼼히 분배하여 동적 잠금을 피한다.-사라진 자물쇠를 파괴하는 네 가지 필요조건, 임의의 조건이 충족되지 않으면 사라진 자물쇠는 발생하지 않는다.
스케줄이 뭐예요? -하나의 파이프라인은 과정, 변수, 그리고 데이터 구조로 구성된 집합이다.그들은 특수한 모듈과 소프트웨어 패키지를 구성하여 프로그램은 임의의 시간에 파이프라인 중의 과정을 호출할 수 있지만 파이프라인 이외의 성명 과정에서 파이프라인 중의 데이터 구조를 직접 사용할 수 없다.또한 루틴은 개념 언어이고 C 언어는 루틴을 지원하지 않는다.
monitor example
    integer i;
    condition c;
    produce producer();
    end;
    producer  consumer();
end monitor;다음은 자바로 프로그램 관리 방식을 실현하는 생산자 소비자의 문제이다. 말하자면 사실은 하나의 보조류를 채택한 것이다.
public class ProducerConsumer{
    static final int N=100;
    static producer p=new producer();
    static consumer c=new consumer();
    static our_monitor mon =new our_monitor();//      
    public static void main (String args[]){
        p.start();
        c.start();
    }
    static class producer extend Thread {
        public  void run(){
            int item;
            while(true){
                item=produce_item();
                mon.insert(item);
            }
        }
        private int produce_item(){//     。。。}
    }
    static class consumer extend Thread{
        public void run(){
            int item ;
            while(true){
                item=mon.remove();
                consumer_item(item);
            }
        }
        private void consumer_item(int item){//  。。。。}
    }
    static class our_monitor{
        private int buffer[]=new int[N];
        private int count=0,io=0,hi=0;//      
        public synchronized void insert (int val){
            if(count==N) 
                go_to_sleep();
            buffer[hi]=val;
            hi=(hi+1)%N;
            count++;
            if(count==1)
                notify();
        }
        public synchronized int remove(){
            int val;
            if(count==0)
                go_to_sleep();
            val=buffer[io];
            io=(io+1)%N;
            count--;
            if(count=N-1)
                notify();//        ,     
            return val;
        }
        private void go_to_sleep(){
            try{
                wait();
            }
            catch(interruptedException exc){}
        }
    }
}이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Java 다중 스레드를 순차적으로 실행하는 몇 가지 방법 요약Java 다중 스레드를 순차적으로 실행하는 몇 가지 방법 요약 동료는 무심결에 이 문제를 제기하고 두 가지 방법을 직접 실천했다.물론 더 좋은 방법이 있을 거야. 방법 1 이런 방법은 비교적 흔히 볼 수 있는 해결 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.