2016-05-19 17 views
1

プロデューサ - コンシューマをC言語で記述します。プロセスをリソースとして使用します。 私は新しいプロセスのPIDをQueue.Afterでfork()した後、コードのprintf()の重複を避けるために親プロセスを終了します。 私のコードはループしています。私の間違いですか?プロデューサ/コンシューマでfork()を使用する

#include<stdio.h> 
#include <stdlib.h> 
#include <sys/types.h> 
#include <unistd.h> 

struct node{ 
    int data;   
    struct node *next; 
}; 
struct queue{ 
    struct node *first; //pointer to the first item 
    struct node *last; //pointer to the last item 
}; 
void insert (struct queue *q, int value){ 
    struct node *newnode = malloc(sizeof(struct node)); 
    newnode->data = value; 
    newnode->next = NULL; 
    if(q->first == NULL){ 
     q->first = q->last = newnode; //if list is empty, first and last = newnode 
    } 
    else{   
     q->last->next = newnode; //append newnode after last element       
     q->last = q->last->next; // point "last" pointer to the new node 
    }      
}   
int Remove(struct queue *q){ 
    int value = q->first->data;   
    struct node *temp = q->first;   
    q->first = q->first->next; // moves the first pointer to the next item   
    free(temp); //deletes the old first node 
    return value; //returns the first value;    
} 
void print(struct queue *q, int c){ 
printf("\n---------------------------------------------------------------------------\n"); 
if(c==0) 
printf("\tQueue is EMPTY\n\t\t(Now It is sleeping)"); 
else { 
    int value=0;     
    int i; 
    struct node* p; 
    p = q->first; 
    for(i=0; i<c; i++){ 
     value = p->data; 
     printf("\t%d", value); 
     p = p->next; // moves the first pointer to the next item 
    } 
} 
printf("\n---------------------------------------------------------------------------\n"); 
} 
    void main() { 
int ch,n,c=0; 
int tempp=0; 
pid_t p; 
struct queue q; 
q.first = NULL; 
q.last = NULL; 
printf("\tEnter Queue Size : "); 
scanf("%d",&n); 
while(1) { 
    printf("\n\n\t\tBuffer state (Queue Size : %d )\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~",n); 
    print(&q, c); 
    printf("\n\t\tCHOICES\n\t\t~~~~~~~\n\t1.Producer\n\t2.Consumer\n\t3.Exit\nEnter your choice : "); 
    scanf("%d",&ch); 
    switch(ch) { 
    case 1: 
     if(c==n) 
      printf("Queue is FULL. So Producer goes to SLEEP\n"); 
     else { 
      c++; 
      p=fork(); 
      if(p == 0){ 
       ch=0; 
       tempp = getpid(); 
      } 
      else 
       return 0; 
      insert(&q, tempp); 
     } 
     break;           
     case 2: 
      if(c==0) 
       printf("\tQueue is EMPTY\n"); 
      else { 
       Remove(&q); 
       printf("\t\tCONSUME one item"); 
       c--; 
      } 
      break; 
      case 3: 
       exit(0); 
      default: 
       printf("\tIt is Wrong choice,Please enter correct choice!............\n"); 
    } 
} 
} 

答えて

2

親プロセスが死んだとき、それは親と子で共有されているSTDINを閉じたので、あなたのscanf()は子供に失敗しています。親プロセスが子供を待つようにしてください。

 p = fork(); 
     if (p == 0) { 
      ch = 0; 
      tempp = getpid(); 
     } else { 
     int status; 
     wait(&status); 
     return 0; 
     } 
関連する問題