Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
5.1 kB
2
Indexable
Never
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
sem_t s1,s2;
pthread_mutex_t lock1,lock2;      //protect share recourse
int count=0;
void *search(void *pid);
void Database(char ch[],int id);


int main()
{
    int choice;
    while(1)
    {

        printf("\n \t\t\t\t1.Search Movie\t\t\t\t");
        printf("\n \t\t\t\t2.Exit\t\t\t\t\t");
        printf("\n \t\t\t Input Your choice:");
        scanf("%d",&choice);

        switch (choice)
        {

        case 1:

        {


            int n;
            printf("\t\tHow many movie want to search: ");
            scanf("%d",&n);
            int i,c=0,x=0;
            sem_init(&s1,0,2);
            sem_init(&s2,0,0);
            pthread_mutex_init(&lock1,0);           //use for serializing critical recourses
            pthread_mutex_init(&lock2,0);
            pthread_t tid[n];
            char ch[n][20];
            int in;
            int j;

            if(n<=3)
            {
                for(i=0; i<n; i++)
                {

                    printf("\t\tEnter movie name: %d : ",(i+1));
                    scanf("%s",ch[i]);
                    pthread_create(&tid[i],NULL,search,(void*)&ch[i]);      //create a new thread
                    if(i==(n-1))
                    {
                        for(j=0; j<n; j++)
                            sem_post(&s2);       //unlock semaphore
                    }
                }

                for(i=0; i<n; i++)
                {
                    pthread_join(tid[i],NULL);            //wait a thread for terminate
                }
            }
            else
                printf("\t\tEnter integer Value 1 to 3:");
            printf("\n\n\t\t:::Enter any key to go to main function::::");
            getch();
            system("cls");
            break;
        }
        case 2:
        {
            exit(0);
            break;
        }

        }
    }
}
void *search(void *pid)
{
    sem_wait(&s2);				//for wait this semaphore
    printf("Process %d reached\n",pthread_self());          // pthread_self() use for get pid current thread
    sleep(1);                   //wait for a short interval
    pthread_mutex_lock(&lock1);
    if(count>=2)
    {
        printf("Process %d is waiting to search\n",pthread_self());
    }
    count++;
    pthread_mutex_unlock(&lock1);

    sem_wait(&s1);

    printf("Process %d started searching\n",pthread_self());
    sleep(1);
    printf("Process %d searching for %s\n",pthread_self(),pid);

    pthread_mutex_lock(&lock2);
    Database(pid,pthread_self());

    printf("\n\nProcess%d finished it's search\n\n",pthread_self());
    pthread_mutex_unlock(&lock2);
    sem_post(&s1);
}
void Database(char ch[],int id)
{

    char str1[]="dragon";
    char str01[]="Dragon";
    char str2[]="nun";
    char str02[]="Nun";
    char str3[]="dark";
    char str03[]="Dark";

    if(strcmp(str1,ch)==0||strcmp(str01,ch)==0)
    {


        printf("\n \t\t Showing result for Process%d and keyword is %s\t",id,ch);
        printf("\n \t\t Searching Result = 2\t\t\t\t\t");
        printf("\n \t\tName: Dragon Ball Z(1989)\t\t\t\t");
        printf("\n \t\tDirector:  Sean Schemmel, Christopher Sabat\t\t");
        printf("\n \t\tRating:  8.7\t\t\t\t\t\t");


        printf("\n \t\tName: How to Train Your Dragon(2010)\t\t\t");
        printf("\n \t\tDirector:  Chris Sanders, Dean DeBlois\t\t\t");
        printf("\n \t\tRating:  8.1\t\t\t\t\t\t||");
        printf("\n \t\t::Enter any key to go to main function::");
        getch();
        system("cls");
    }


    else if(strcmp(str02,ch)==0||strcmp(str2,ch)==0)
    {

        printf("\n \t\tShowing result for Process%d and keyword is %s\t\t ",id,ch);
        printf("\n \t\tSearching Result = 1\t\t\t\t\t");

        printf("\n \t\tName: The NUN\t\t\t\t\t");
        printf("\n \t\tDirector:   Corin Hardy\t\t\t\t\t");
        printf("\n \t\tRating:  5.3\t\t\t\t\t\t");
        printf("\n \t\t::Enter any key to go to main function::");
        getch();
        system("cls");

    }

    else if(strcmp(str3,ch)==0||strcmp(str03,ch)==0)
    {

        printf("\n \t\tShowing result for Process%d and keyword is %s\t\t",id,ch);
        printf("\n \t\tSearching Result = 1\t\t\t\t\t");
        printf("\n \t\tName: Dark(2017)\t\t\t\t\t");
        printf("\n \t\tDirector:  Baran bo Odar, Jantje Friese\t\t\t");
        printf("\n \t\tRating:  8.8\t\t\t\t\t\t||");
        printf("\n \t\t:::Enter any key to go to main function::");
        getch();
        system("cls");

    }

    else
    {

        printf("\n \t\tShowing result for Process%d and keyword is %s\t\t",id,ch);
        printf("\n \t\tSearching Result = 1\t\t\t\t\t");
        printf("\n \t\tNo matches found\t\t\t\t\t||");
        printf("\n \t\t::Enter any key to go to main function::");
        getch();
        system("cls");

    }
}