生产者/消费者中fork()的用法

时间:2016-05-19 16:37:34

标签: c linux operating-system

我用c语言编写了生产者 - 消费者。我使用进程作为资源。 我在Queue中输入新进程的PID。使用fork()之后,我终止了父进程,以避免重复printf()代码。 我的代码落入循环中。我的错误是什么?

#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");
    }
}
}

1 个答案:

答案 0 :(得分:2)

您的scanf()在子进程中失败,因为当父进程终止时,它会关闭父进程和子进程共享的stdin。所以让父进程等待孩子。

      p = fork ();
      if (p == 0) {
          ch = 0;
          tempp = getpid ();
      } else {
        int status;
        wait( &status );
        return 0;
      }