Reputation: 1805
I write producer - consumer in c language.I use process as resource. I enter the PID of new process in Queue.After use fork(), I kill the parent process for avoid duplicate of printf() of code. My code fall in loop.what's my mistake?
#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");
}
}
}
Upvotes: 1
Views: 2194
Reputation: 824
Your scanf()
is failing in the child because when the parent process dies it closes stdin which is shared by the parent and the child. So have the parent process wait for the child.
p = fork ();
if (p == 0) {
ch = 0;
tempp = getpid ();
} else {
int status;
wait( &status );
return 0;
}
Upvotes: 2