LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-17-2014, 09:58 PM   #1
mirage1993
Member
 
Registered: Feb 2014
Location: China
Distribution: CentOS6.4
Posts: 51

Rep: Reputation: Disabled
Question when I use sleep(1); in my program,there is a problem.I can't understand


Code:
#include<stdio.h> 

#include<unistd.h> 

#include<string.h> 

#include<stdlib.h> 

#include<pthread.h> 

#include<semaphore.h> 



void *thread_function(void *arg);

int worksize=10;

//char workarea[worksize];
char workarea[10];

sem_t sem;

sem_t full;

sem_t empty;

int in=0,out=0;



int main()

{

int res;

FILE *fp;
int ch;

pthread_t a_thread;

void *thread_result;

/////////semaphore init////////

res=sem_init(&sem,0,1);

if(res!=0)

{perror("error:");exit(1);}

res=sem_init(&full,0,0);

if(res!=0)

{perror("error:");exit(1);}

res=sem_init(&empty,0,worksize);

if(res!=0)

{perror("error:");exit(1);}



/////////creat thread///////////

res=pthread_create(&a_thread,NULL,thread_function,NULL);

if(res!=0)

{perror("error:");exit(1);}

/////////open file/////////////

if((fp=fopen("/home/mirage/Desktop/program/pie.txt","r"))==NULL)

	{perror("error:");exit(1);}



///////////producer-read from file////////////

while(1)

{

	sem_wait(&empty);

	sem_wait(&sem);		//critical section

		if((ch=fgetc(fp))==EOF)
			break;

		workarea[in]=ch;

		in=(in+1)%worksize;//

	sem_post(&sem);    //no critical section

	sem_post(&full);

}//while



sem_destroy(&sem);
sem_destroy(&empty);
sem_destroy(&full);

exit(0);

}//main



///////////consumer-output to terminal////////////

void *thread_function(void *arg)

{
	int ent=0;

	printf("pie=:3.\n");

	while(1)

	{

		sem_wait(&full);

		sem_wait(&sem);		//critical section
			//sleep(1);
			printf("%c",workarea[out]);
			ent++;
			if(ent==10)
				{printf("\n");ent=0;}

			out=(out+1)%worksize;

		sem_post(&sem);		//no critical section
		sem_post(&empty);



	}//while



}
when I enable this row
Code:
//sleep(1);
,my program will output
Code:
pie=3.
and it will wait forever.

This program has two threads,one for read from a file name pie.txt,the other for display.I want it display slowly,so I use sleep(),but it't cause a problem.

Last edited by mirage1993; 04-17-2014 at 09:59 PM.
 
Old 04-18-2014, 12:30 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,450

Rep: Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776
it works, you need to wait.... it prints 10 chars in a line, therefore you need to wait 10 seconds for every line.
inserting fflush(NULL) in that while loop will force the system to print every char immediately....
 
1 members found this post helpful.
  


Reply

Tags
program


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
How to understand the GOT in a program Richard.Yang Programming 1 04-29-2012 08:24 PM
problem in using sleep() in plugin functions in multithread program linuxsavar Programming 3 10-12-2009 01:18 PM
[SOLVED] I need help with understand a recursive assembly program. joeBuffer Programming 31 08-16-2009 10:36 PM
Again I need your explaination to understand a program. Gins Programming 7 03-24-2006 04:19 PM
could any 1 help me understand how to program? nleber01 Programming 2 03-07-2004 04:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:20 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration