Ich schreibe Produzent - Verbraucher in c-Sprache.Ich benutze Prozess als Ressource. Ich gebe die PID des neuen Prozesses in der Warteschlange ein. Nach der Verwendung von fork(), töte ich den Elternprozess, um das Duplikat von printf() zu vermeiden. Mein Code fällt in loop.what ist mein Fehler?Verwendung von fork() in Hersteller/Verbraucher
#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");
}
}
}