LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-10-2013, 07:28 AM   #1
amir8230
LQ Newbie
 
Registered: Jan 2013
Posts: 2

Rep: Reputation: Disabled
help for parking program


Hi.
I'm writing a parking program.but I press key F ,it dont work.
can you help me?
Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <pthread.h>
#include<semaphore.h>

#define TRUE 1
#define FALSE 0
 
int places=10;
sem_t evt_in,evt_out,evt_maj;
int fin=FALSE;
pthread_mutex_t mutex_pl;
pthread_cond_t cond_places ;

void * t_keyboard (void *arg)
{
char res;
 while (fin!=TRUE)
{
 res=getchar();
 //fflush(stdin);
//printf ("user input=%c\n",res);
 if(res=='f')
{
    fin=TRUE;
	
}
else if  ( res=='e')
{
          sem_post(&evt_in);
		 }
 else if (res=='s')
{
sem_post(&evt_out);
		 }
}
}
void * t_enter (void *arg){
     while (fin!=TRUE){
	 //printf("ok t enter\n");
       sem_wait(&evt_in);
	  // printf("ok t enter\n");
       pthread_mutex_lock(&mutex_pl);
       if (places==0){
         pthread_cond_wait(&cond_places, &mutex_pl);
     }
       printf(" barrier opening(IN)");
       
     places = places -1;
       sem_post(&evt_maj);
    // printf(" ok t enter\n");
       pthread_mutex_unlock(&mutex_pl);
     }
   }
   
void * t_exit (void *arg){
 
while (fin!=TRUE){
       sem_wait(&evt_out);
       pthread_mutex_lock(&mutex_pl);
       if (places==0){
         pthread_cond_wait(&cond_places, &mutex_pl);
     }
       printf("barrier opening IN");
     places = places +1;
       sem_post(&evt_maj);
       pthread_mutex_unlock(&mutex_pl);
     }
   }
      
void * t_display (void *arg){
 
while (fin!= TRUE) {
      sem_wait(&evt_maj);
      pthread_mutex_lock(&mutex_pl);
      printf ("places = %d\n",places);
      pthread_mutex_unlock(&mutex_pl);
      }
}

int main()
{

pthread_t thread_keyboard,thread_enter,thread_display,thread_exit;
int  ikeyboard,ienter,idisplay,iexit;
pthread_mutex_init(&mutex_pl,NULL);
pthread_cond_init(&cond_places,0);
sem_init(&evt_in,0,0);
sem_init(&evt_maj,0,0);

ikeyboard=pthread_create( &thread_keyboard, NULL,&t_keyboard,NULL);
ienter=pthread_create( &thread_enter, NULL,&t_enter,NULL);
iexit=pthread_create( &thread_exit, NULL,&t_exit,NULL);
idisplay=pthread_create( &thread_display, NULL,&t_display,NULL);

pthread_join( thread_keyboard, NULL);
pthread_join( thread_enter, NULL);
pthread_join( thread_exit, NULL);
pthread_join( thread_display, NULL);
 
exit(0);
}

Last edited by amir8230; 01-10-2013 at 08:06 AM.
 
Old 01-10-2013, 07:55 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Well, one way of seeing what's going wrong is putting messages at the end of each thread function to let you know when they exit. You should see that t_enter and t_exit never quit - because they're still waiting on a mutex. You just need to sem_post the relevant mutexes.
 
1 members found this post helpful.
Old 01-10-2013, 08:07 AM   #3
amir8230
LQ Newbie
 
Registered: Jan 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank a lots
 
Old 01-11-2013, 08:19 AM   #4
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
If you consider your problem solved, please mark the thread as 'SOLVED' using the link at the top of the page please

Thanks,
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
excessive head parking!! heellp! snadge Linux - Hardware 12 01-18-2011 08:02 AM
hdparm load cycle/head parking question jsteel Linux - Laptop and Netbook 1 05-30-2010 06:22 AM
Parking the hdd heads. stf92 Linux - Hardware 7 12-13-2009 10:16 AM
hard drive making emerg. parking beep at shutdown Geocritter Linux - Laptop and Netbook 4 01-20-2007 02:11 PM
LXer: Perens pushes for open source domain parking LXer Syndicated Linux News 0 04-18-2006 04:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration