LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-01-2009, 04:45 AM   #1
aryan1
Member
 
Registered: Jul 2009
Posts: 50

Rep: Reputation: 16
Question signalling a multi-threaded daemon from the command line


Hi All,

I have the following main application which runs as a daemon in Linux:

Code:
bool commandEntered = false;

int main()
{
     // Daemon specific code goes here
     ...
     // Disable standard output and error file descriptors
     // Standard input is intentionally not disabled
     close(STDOUT_FILENO);
     close(STDERR_FILENO);
     ...
     // Perform some file IO here
     ifstream inputFile("sample.txt");
     readFile(inputFile);
     ...
     createThread(commandLineListener);

     while(1)
     {
           if(commandEntered)
           {
                 // Process the same file
                 readFile(inputFile);
                
                 commandEntered = false;
           }

           // Perform some processing based on updated sample.txt here
          ...
     }
}
In the above code, The user-defined createThread() function simply calls pthread_create() to create the thread.
readFile() function opens up the specified file and initializes some variables based on that is read from the file.

Here is the only user-defined thread in the daemon:

Code:
void *commandLineListener(void* threadID)
{
      string command;
      while(1) {
            cin >> command;

            if(command.compare("refresh") == 0) {
                    commandEntered = true;
            }
      }
}
As you can see from the above code, the only thread listens for a specific command entered from the command line, and notifies the main application (daemon) when it is entered.

However, I have the following issues:

1. After starting the daemon, Linux command prompt, where I started it, appears to be "disabled". However, when I check the daemon logs, I see that only the entered characters are not visible -- they are actually there. Even though the enetered characters are invisible, commandLineListener() detects the command and notifies the daemon successfully.

2. After a few command entries ('refresh' or anything else), the command prompt becomes enabled again. But, in this case, commandLineListener() looses its control, and its command detection and notification mechanism do not work anymore.

3. If I start the daemon with standard out enabled, command detection and notification mechanism do not work at all.

Questions:
1. What is causing this problem ?
2. What is the neatest way of implementing this mechanism ?

Thanks.
 
Old 09-01-2009, 05:52 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
A daemon does not take input from a keyboard A daemon runs in the background and therefore has no connection to stdin. So your program is not a daemon.

What do you call the linux command prompt? Something like wim@btd-techweb01:~$? If you don't see that, you're application is running in the foreground. Once you see it again, I think your application has crashed.

My C coding knowledge is a bit rusty but if you disable stdout, where should characters be echoed to?


PS1:
There is a dedicated programming section here at LQ. You can ask a moderator to move your thread (use the report button in your opening post).
 
Old 09-01-2009, 06:04 AM   #3
aryan1
Member
 
Registered: Jul 2009
Posts: 50

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by Wim Sturkenboom View Post
A daemon does not take input from a keyboard A daemon runs in the background and therefore has no connection to stdin. So your program is not a daemon.

What do you call the linux command prompt? Something like wim@btd-techweb01:~$? If you don't see that, you're application is running in the foreground. Once you see it again, I think your application has crashed.

My C coding knowledge is a bit rusty but if you disable stdout, where should characters be echoed to?


PS1:
There is a dedicated programming section here at LQ. You can ask a moderator to move your thread (use the report button in your opening post).
Yes, I mean something like wim@btd-techweb01:~$ by "command prompt".

My application still runs as a daemon with one "unusual" feature - it can be still signalled from the command line (I am not sure if I am using the concepts with their correct meaning).

When I first start the daemon, I can see the command prompt (When I perform pgrep for the daemon, I see that it is running).

However, the characters that I typed are not visible even though they are indeed there (This is expected since I disabled stdout).

If you understood the framework that I described in my previous posting, what do you suggest to implement this mechanism ? Do you suggest using IPC instead of using threads ?
 
Old 09-01-2009, 06:37 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,577
Blog Entries: 31

Rep: Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196Reputation: 1196
Quote:
Originally Posted by aryan1 View Post
Yes, I mean something like wim@btd-techweb01:~$ by "command prompt".

My application still runs as a daemon with one "unusual" feature - it can be still signalled from the command line (I am not sure if I am using the concepts with their correct meaning).

When I first start the daemon, I can see the command prompt (When I perform pgrep for the daemon, I see that it is running).

However, the characters that I typed are not visible even though they are indeed there (This is expected since I disabled stdout).

If you understood the framework that I described in my previous posting, what do you suggest to implement this mechanism ? Do you suggest using IPC instead of using threads ?
We need to use a common language if we are to communicate effectively. Because you are asking about Linux, can we agree to use Linux vocabulary with the conventional meanings? The alternative is that you define your terms -- very difficult to do concisely and precisely and some potential answerers will not bother to answer if they have to learn a new vocabulary (yours) first.

Both daemon and signal conventionally have very specific meanings in Linux.

Your difficulties are not with the specifics of daemons, signals and threads but with basic C programming -- how to write and use functions. The quickest way to progress may be to master a simple multi-function "Hello World" C program first. Once you have mastered that then move on to threads and then signals and then daemons. Jumping in at the deep end is seldom the fastest (or easiest!) way to learn.
 
Old 09-01-2009, 06:57 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
What I don't understand is why your daemon still gets data from stdin; I know you did not close stdin and I see that you have a cin but I would expect the shell not to pass it on. This is probably a limitation in my knowledge.

My first question would be why you have choosen this approach. If I have a (real) daemon that needs commands from a user, I would personally make a client/server type solution. The client can be telnet that you let connect to a specific port (where the server is listening on) and you can type commands in there that will be handled by the server (daemon).

Is there only one command that you need to handle? If so, you can consider to use a signal and you can use kill to send that signal to the application and let it do whatever you want it to do.

Last edited by Wim Sturkenboom; 09-01-2009 at 06:58 AM.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
wget: Multi-Threaded downloading wwnexc Linux - Networking 8 05-15-2010 08:40 PM
multi-line command doesn't wrap correctly spiffytech Linux - Software 2 12-28-2008 06:18 PM
apache: running multi-threaded or multi fork? Swakoo Linux - General 1 03-20-2008 07:18 AM
Send a message via command line without sendmail daemon. sonenberk Linux - Software 1 11-22-2005 01:31 AM
Multi-Threaded C pragti Programming 1 06-01-2004 10:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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