LinuxQuestions.org
Review your favorite Linux distribution.
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 09-10-2003, 09:10 AM   #1
marek
Member
 
Registered: Aug 2003
Location: Poland
Distribution: Slackware 9.1
Posts: 34

Rep: Reputation: 15
break loop in C++


Hi, Has anybody got an idea how interrupt loop in C++ program?. I'll show you situation:

I've created program with Qt which turn on and off the keyboard lights with some frequency. It is done with a loop :

start()
{
while(1)
{
....
}
}
which is a part of C++ class. I execute start() when i cilck on button. But how to stop this loopback?? (i mean by clicking different buton)
I tested solutions with using function fork() to creating new process, but it doesn't want to work well (i usually received the zombie process), i think about threads, but i am not sure if it works.
How is possibly to break loops in C++ class?????

ps. breaking loop in C is really simple - enough to send a signal from other process, but i have to use a Qt classes, so i have to work in C++
 
Old 09-10-2003, 09:16 AM   #2
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
the 'continue' and 'break' keywords break out of a loop.

Since you seem to know quite a bit about C, I'm not sure this is the answer you're looking for. If not, let me know and I'll try to help you further.
 
Old 09-10-2003, 09:58 AM   #3
marek
Member
 
Registered: Aug 2003
Location: Poland
Distribution: Slackware 9.1
Posts: 34

Original Poster
Rep: Reputation: 15
You right but, but i want to break loop which exists in function1 from function2,
of course i may test - every time i make a looping - value of some global valuable (private for C++ class), and change this value in function2, but do you think it is a good idea?
 
Old 09-10-2003, 10:15 AM   #4
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
It's been a while since I've done anything with inter-process communication, but I seem to recall that there is a way for one process to send a signal to another. If you fork the process before entering the loop, then have a test for "signal received from other process?" as the while condition, it should do what you're after.

I am afraid I cannot offer any details on how to do this, though...

Is there a particular reason that the loop has to be interrupted by an outside process? If a click starts the keyboard lights blinking, then shouldn't you just have a click of some kind fo stop it also? If that's the case, perhaps what you want is a loop that says "has user clicked mouse? If not, go ahead and blink lights. Loop."
 
Old 09-10-2003, 10:22 AM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
I think you'll have to implement it in a thread. The problem you describe boils down to one thing. You want your program to do two things at once: wait for input from the user and blink lights in a continuous loop.

You could do it with forks, but I like threads much more.

Essentially, you will have a main thread that handles user input. Then, when you click a button, your main thread will call an interface task with your thread(s). That task will alter some internal variable for the thread; a flag to indicate some new behavior. Then your main thread will resume waiting for input.

Meanwhile, your thread's code would look something like this:
Code:
private:
  int enable_blinking; // internal thread variable
  int kill_thread;  // flag to terminate thread's execution

...

// Public interface function
void thread_class::change_blink_status(int new_status)
{
  enable_blinking = new_status;
}

// Function that is executed by the thread when it is
// created
void thread_class::start() 
{
  while( kill_thread == 0 )
  {
    if( enable_blinking == 1 )
    {
      toggle_lights();
      // put in some delay here to get your frequency
    }
  }
}
It's been a while since I've played with threads, but i think that will work. It would also be cleaner to have a timer signal when to toggle the lights on or off. Lots of different ways of doing it.

Last edited by Dark_Helmet; 09-10-2003 at 10:24 AM.
 
Old 09-10-2003, 11:26 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: break loop in C++

Quote:
Originally posted by marek
I've created program with Qt which turn on and off the keyboard lights with some frequency. It is done with a loop :

start()
{
while(1)
{
....
}
}
which is a part of C++ class. I execute start() when i cilck on button. But how to stop this loopback?? (i mean by clicking different buton)
The problem is that the program does not react to the "stop"-button while the loop is running, so that's why you are thinking about threads, or a second process to interrupt it. Did I get this right?

The simplest solution, if I understand your question right, is not to use threads or processes, and not even a loop. You are using Qt, so why not use a timer ?

You can make a timer call with a (Qt slot-) function periodically (with a minimum of about 20 ms, which can be too slow in some cases, but not in your case I guess.)

Using a timer, the Qt framework does handle button-clicks and other events, while your light switching stuff is done in intervals.

Example:

Code:
//
//       Header file (.h)
//

#include <qwidget.h>
#include <qtimer.h>

class yourMainWidget: public QWidget
{
     Q_OBJECT

public:
     yourMainWidget(QWidget *parent=0, const char *name=0);
     ~yourMainWidget();
     
     // more stuff, like the buttons

protected:
     void timerEvent(QTimerEvent *); 

private:
     int timerID;
     
};


//
//    C++ file (.cpp)
//

void MMGameboard::timerEvent(QTimerEvent *e)
{
     // do your light switching stuff here.
     // *without* looping.
}


// ...then in the "start"- button slot, do:

timerID = startTimer(1000);  // 1000 ms = 1 second. try 10 or 100.

// and in the "stop"-button slot, do:

killTimer(timerID);
See for details: http://doc.trolltech.com/3.2/qobject.html#killTimer

Hope this helps.

Last edited by Hko; 09-10-2003 at 11:42 AM.
 
Old 09-10-2003, 12:23 PM   #7
marek
Member
 
Registered: Aug 2003
Location: Poland
Distribution: Slackware 9.1
Posts: 34

Original Poster
Rep: Reputation: 15
It's me again. Hko, i am sure you have a right , i tested QTimer earlier and i created application which was working great, but i have (maybe) crazy idea to connect the old function from Linux with graphical interface (i chose Qt). Anyway i have one more question, what is it [defunct]

process -> zombie[defunct]???

Everybody knows what it is zombie, but Could you tell me what it is defunct???
 
Old 09-10-2003, 12:27 PM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by marek Anyway i have one more question, what is it [defunct]

process -> zombie[defunct]???

Everybody knows what it is zombie, but Could you tell me what it is defunct??? [/B]
"defunct" is the original (UNIX) term for the same thing.
In Linux a "defunct" process is called "zombie".
 
Old 09-10-2003, 12:33 PM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by marek but i have (maybe) crazy idea to connect the old function from Linux with graphical interface (i chose Qt). [/B]
Then why not start the old C program from your Qt app with system(), exec...() whatever. Only add a signalhandler that sets a global var, and have the loop watch that var.

Then from your Qt app, send it a signal. You can use all normal C-functions like kill(), and so in a C++/QT program.
 
Old 09-11-2003, 07:38 AM   #10
marek
Member
 
Registered: Aug 2003
Location: Poland
Distribution: Slackware 9.1
Posts: 34

Original Poster
Rep: Reputation: 15
This is my program:


THIS IS FILE form1.ui.h created by QtDesignera:

#include <sys/perm.h> //niezbedne do wywolania funkcji
#include <asm/io.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/time.h>
#include <stdlib.h>


unsigned char lights_off = 0x0;
const unsigned char keyb_inp_port = 0x060;
pid_t parent_process = 0;
pid_t child_process = 0;
int zmienna = 0;
unsigned char num_lock_on = 0x02;
unsigned char caps_lock_on = 0x04;
unsigned char scroll_lock_on = 0x01;

struct itimerval my_itimerval;


void Form1::init()
{

}

void Form1::destroy()
{

}


void Form1::start_lampki()
{

printf("Numer PID dla start_lampki = %d\n", getpid());
child_process = fork();
if (child_process == 0)
{
ioperm(keyb_inp_port, 16, 1);
while(1)
{
outb(0xed, keyb_inp_port);
outb(num_lock_on, keyb_inp_port);
sleep(1);
outb(0xed, keyb_inp_port);
outb(caps_lock_on, keyb_inp_port);
sleep(1);
outb(0xed, keyb_inp_port);
outb(scroll_lock_on, keyb_inp_port);
}
exit(1);
}
else if (child_process > 0)
{
printf("weee\n");
wait(&status);
}

}


void Form1::stop_lampki()
{
pid_t p = getpid();
kill(p + 1, SIGTERM);
printf("Numer PID dla stop_lampki = %d\n", getpid());
printf("Numer PID parenta dla stop_lampki = %d\n", getppid());

outb(0xed, keyb_inp_port);
outb(lights_off, keyb_inp_port);
ioperm(keyb_inp_port, 16, 0);
}


THIS IS MY FILE main.cpp

#include <qapplication.h>
#include "zabawa.h"
#include <qpushbutton.h> //ten plik naglowkowy musi byc, zeby mozna bylo korzystac z PushButton'a dla funckji connect() !!

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Form1 z;// = new Form1(0,0,FALSE,0);
z.show();
a.connect(z.startPushButton, SIGNAL(clicked() ), &z, SLOT(start_lampki() ));
a.connect(z.stopPushButton, SIGNAL(clicked() ), &z, SLOT(stop_lampki() ));
a.connect(&a, SIGNAL(lastWindowClosed() ), &a, SLOT(quit() )); //po zamknieciu ostatniego okna formatka (QDialog) bedzie zamkniety a wraz z nim cala aplikacja
a.connect(&a, SIGNAL(aboutToQuit() ), &z, SLOT(stop_lampki() )); //w momencie, w ktorym aplikacja bedzie zamykana przerwe sterowanie swiatelkami
return a.exec();
}

except these two files written above i have two files form1.h and form1.cpp (declaration and implementation for Qt class)

What about trouble - when i click on one button, it execute slot "start_lampki", and that's all application is blocked and i can't click another buttons!!
 
Old 09-11-2003, 10:59 AM   #11
marek
Member
 
Registered: Aug 2003
Location: Poland
Distribution: Slackware 9.1
Posts: 34

Original Poster
Rep: Reputation: 15
Ok, i made a lot of mistakes, which i have resolved now. Thank you everybody for help
 
  


Reply



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 break if loop in Perl? Barca Programming 9 08-03-2011 01:15 PM
Possible Break In??? stlyz3 Linux - Security 9 10-26-2005 02:43 PM
How does it all break down? Bu3Nix Slackware - Installation 5 09-15-2005 02:50 PM
could I break my pc? linuxhippy Slackware 9 04-02-2005 07:15 AM
Could someone please break it down for me...? Pwcca Slackware 6 01-23-2003 10:05 AM

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

All times are GMT -5. The time now is 07:19 AM.

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