Untitled

 avatar
unknown
plain_text
a month ago
6.8 kB
6
Indexable
#include <stdio.h>

int mutex = 1;
int full = 0;
int empty = 3;

int x = 0;

// Wait operation
int wait(int s)
{
        return (--s);
}

// Signal operation
int signal(int s)
{
        return (++s);
}

void producer()
{
        mutex = wait(mutex);

            full = signal(full);

                empty = wait(empty);

                    x++;

                        printf("\nProducer produces item %d", x);

                            mutex = signal(mutex);
}

void consumer()
{
        mutex = wait(mutex);

            full = wait(full);

                empty = signal(empty);

                    printf("\nConsumer consumes item %d", x);

                        x--;

                            mutex = signal(mutex);
}

int main()
{
        int choice;

            while(1)
                {
                            printf("\n\n1. Produce");
                                    printf("\n2. Consume");
                                            printf("\n3. Exit");

                                                    printf("\nEnter your choice: ");
                                                            scanf("%d", &choice);

                                                                    switch(choice)
                                                                            {
                                                                                            case 1:

                                                                                                            if((mutex == 1) && (empty != 0))
                                                                                                                            {
                                                                                                                                                    producer();
                                                                                                                            }
                                                                                                                                            else
                                                                                                                                                            {
                                                                                                                                                                                    printf("\nBuffer is Full");
                                                                                                                                                            }

                                                                                                                                                                            break;

                                                                                                                                                                                        case 2:

                                                                                                                                                                                                        if((mutex == 1) && (full != 0))
                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                consumer();
                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                        else
                                                                                                                                                                                                                                                        {
                                                                                                                                                                                       œ                                                                                         printf("\nBuffer is Empty");
                                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                                                        break;

                                                                                                                                                                                                                                                                                    case 3:
                                                                                                                                                                                                                                                                                                    return 0;
                                                                            }
                }

                    return 0;
}

                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                        }
                                                                                                                                                            }
                                                                                                                            }
                                                                            }
                }
}
}
}
}
}
Editor is loading...
Leave a Comment