#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
sem_t s1,s2;
pthread_mutex_t lock1,lock2;
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);
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]);
if(i==(n-1))
{
for(j=0; j<n; j++)
sem_post(&s2);
}
}
for(i=0; i<n; i++)
{
pthread_join(tid[i],NULL);
}
}
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);
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[]="free";
char str01[]="Free";
char str2[]="f9";
char str02[]="F9";
char str3[]="mi";
char str03[]="MI";
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: Free Guy \t\t\t\t");
printf("\n \t\tDirector: Shawn Levy \t\t");
printf("\n \t\tRating: 7.2 \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: F9 \t\t\t\t\t");
printf("\n \t\tDirector: Justin Lin \t\t\t\t\t");
printf("\n \t\tRating: 5.2 \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: Mission Impossible \t\t\t\t\t");
printf("\n \t\tDirector: John Woo \t\t\t");
printf("\n \t\tRating: 6.7 \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");
}
}