LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 04-19-2014, 10:48 PM   #1
mirage1993
Member
 
Registered: Feb 2014
Location: China
Distribution: CentOS6.4
Posts: 51

Rep: Reputation: Disabled
Question How to implement a function that the program can pause at any time and continue


I want to write a program .its function very simple.
when I run it ,it will display some numbers. when I take some action such as press 's',it will pause until I press 'g'.
in short,I want to implement a function that the program can pause at any time,and continue.
I tried to use semaphore,but I failed.

reference the original code:
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;//critical
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);
    fclose(fp);
    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
            usleep(80000);
            printf("%c",workarea[out]);
            fflush(stdout);
            ent++;
            if(ent==10)
                {printf("\n");ent=0;}
            out=(out+1)%worksize;
        sem_post(&sem);     //no critical section
        sem_post(&empty);
    }//while
}
 
Old 04-20-2014, 05:30 AM   #2
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Instead of semaphores, use POSIX conditional variables:
Code:
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condvar = PTHREAD_COND_INITIALIZER;
bool paused = false;

void thread1() {
	for (;;) {
		sleep(10);

		pthread_mutex_lock(&mutex);
		paused = !paused;
		if (!paused) {
			pthread_cond_broadcast(&cond);
		}
		pthread_mutex_unlock(&mutex);
	}
}

void thread2() {
	for (;;) {
		pthread_mutex_lock(&mutex);
		while (paused) {
			pthread_cond_wait(&cond, &mutex);
		}
		pthread_mutex_unlock(&mutex);

		puts(".");
		sleep(1);
	}
}
(Not tested, not compiled).
 
1 members found this post helpful.
  


Reply

Tags
multithreading, programing



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
Pause, hit key to continue, then proceed... stevo520 Linux - Newbie 1 07-06-2012 11:59 PM
pause yum update and continue later av.dubey Linux - Newbie 5 07-10-2008 02:18 PM
how can you pause child/parent processes in C and execute another function. expeliarmus Programming 5 08-06-2007 01:04 AM
how to implement a function table dragondad Programming 4 09-16-2005 12:37 PM

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

All times are GMT -5. The time now is 03:50 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