Solved

Problem with Struct object for parallel process checking in 2 diff. functions..

Posted on 2006-07-19
12
271 Views
Last Modified: 2010-04-15
I am trying to access a variable of an object which is declared inside a struct. But my logic is failing. Let me explain my logic below.

My struct is as follows in a header file "defs.h",

typedef      struct      tagCON {
      BOOL      fAbort;  // This is set to TRUE if the engine should immediately end its search.

               BOOL      fTimeout;

             SEARCH_STATUS      ss;

}      CON, * PCON;


#include "defs.h"
int main()
{
char line[256], command[256];
static CON s_con;
PCON pcon = &s_con;

for(;;)
      {
if (!fgets(line, 256, stdin))
return;
sscanf(line, "%s", command);

if (!strcmp(command, "go")) {
computer = side;
continue;
}

if (side == computer)
            {
think(pcon, tmMyTime, tmTheirTime);
// Some ouputs i display.....
continue;
}

if (!strcmp(command, "quit")) {
pcon->fAbort = TRUE;
return;
}
} //end for loop

}

So think() function executes for very long time. And inside think() function there is a condition if (pcon-> fAbort) return;
So while think is processing, here in main(), I make pcon->fAbort = TRUE when "quit" command is inputed by the user. But the problem is think() is not getting breaked. Actually this program is hooked to a GUI called s Winboard. When I close Winboard, my program is not getting killed. In task manager its running and each and every time I have to "end process" it.

  So whats wrong with this? Thanks.
0
Comment
Question by:gopikrish
12 Comments
 
LVL 8

Accepted Solution

by:
manish_regmi earned 63 total points
ID: 17140042
what is computer and side?

lets assume they are integers.

When the code first enters the loop, lets say user types "go";
think runs and returns.

When it enters the second time, strcmp(command, "go") becomes false

but computer == side becomes true becaue computer has become side previously.

so after think just set computer to something else

if (side == computer)
          {
think(pcon, tmMyTime, tmTheirTime);
// Some ouputs i display.....
computer = <somethingelse>;   //add this
continue;

i hope this helps.

regards
Manish Regmi
]
0
 

Author Comment

by:gopikrish
ID: 17140306
Yep side and computer are integers.
I did computer = EMPTY;
where EMPTY is defined as #define EMPTY            6

But still its not quitting. Any other ideas please ?
0
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17140345
oops it seems that your code never reached there
 //// remove that continue in strcmp(command , "go").
continue will again take you to fgets.

for(;;)
     {
if (!fgets(line, 256, stdin))
return;
sscanf(line, "%s", command);

if (!strcmp(command, "go")) {
computer = side;
//continue;  //// remove that continue
}

if (side == computer)
          {
think(pcon, tmMyTime, tmTheirTime);
// Some ouputs i display.....
computer = <somethingelse>;   //add this
continue;
}

if (!strcmp(command, "quit")) {
pcon->fAbort = TRUE;
return;
}
} //end for loop


regards
Manish regmi
0
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17140381
a better code will be

for(;;)
{
    if (!fgets(line, 256, stdin))
        return;
     sscanf(line, "%s", command);

    if (!strcmp(command, "go")) {
     think(pcon, tmMyTime, tmTheirTime);
   }
   else if(!strcmp(command, "quit"))
   {
       break;
   }
   else
   {
     ?? whatever error you want to show
    }
}


in your code there is no use of
pcon->fAbort = TRUE;

because just after that you are returning from main.
also there is no use of computer and side.


regards
Manish Regmi

0
 

Author Comment

by:gopikrish
ID: 17140489
No think runs for long time (more than 10 mins !)
So while think is running, user inputs "quit"
I hope you got it. So as soon as "quit" is inputted, my program has to stop.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Expert Comment

by:manish_regmi
ID: 17140572
ok, i understood. It is not possible unless you write a multithreaded program.

main thread process input
thread1 process think.

which platform are you in?

for posix threads. this is a start
http://yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
http://sourceware.org/pthreads-win32/ (pthreads for windows)

for windows threads
http://www.oreilly.com/catalog/multithread/excerpt/ch01.html

you will also need a method to notify another thread about an event ie quit pressed by user. Threads library has a lot of things to handle that.

regards
Manish Regmi

0
 

Author Comment

by:gopikrish
ID: 17141367
hey I made it to work using another method !

From my think() function after certain time interval, I call checkup() regularly and see if user has entered "quit". If so it will set pcon->fAbort to TRUE. And so I after I return back to think() I am checking if (pcon->fAbort == TRUE) and hence I return; (ie quit from think() ) :)

void checkup(PCON pcon)
{
      char line[256], command[256];

      if (!fgets(line, 256, stdin))
            return;
      sscanf(line, "%s", command);
      if (!strcmp(command, "quit")) {
      pcon->fAbort = TRUE;
      return;
      }
}

Anyway thanks for the help !


0
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17143033
i was surprised to know it work. possibly it was waiting in fgets and you typed "exit" and thought it worked.

the problem with that code the checkup function blocks. I.e your other parts of think function will NOT work until the user types some characters followed by return key.

suppose your think function shuld run for 10 mins and you are checking for quit every 1 second. you cave to type return key 6000 times before that function runs whole and you type "exit". If you do not type return key it will wait indefinately and think() will never complete its work.

regards
Manish Regmi
0
 
LVL 7

Assisted Solution

by:nafis_devlpr
nafis_devlpr earned 62 total points
ID: 17144729
I agree with Manish,

you code the program in such a way unless it is a multithreaded program its impossible that it will check the user input and run the think() function simultaneously. Once you get into the chceckUp() function the the think() function will stop and transfer the control to checkUp() function. And checkUp will not the rertun from the function until the user sends the quit message, as the program was interrupted by the input interrupt for user input, so it will stuck on that position until the user inputs something. So what you can do is to make it multithreaded and then code the way you were coding, but in the main function you should wait until think() function ends, this you can done by using a global variable so that when think() will be returning it will set a status flag so that the main function understands that think() has been terminated, otherwise you might endup not terminating the think() function and have to use the Taskmanager to kill this thread.

Nafis
0
 
LVL 11

Expert Comment

by:cup
ID: 17163197
If it is multithreaded, the communication structure should be declared as volatile.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now