The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
background process vinod.thayil Shell Programming and Scripting 1 08-17-2007 02:56 AM
background process jerardfjay Shell Programming and Scripting 1 09-07-2005 08:03 AM
process in background agoyal Shell Programming and Scripting 1 07-22-2004 04:56 AM
cause a process to be in background avnerht Shell Programming and Scripting 0 06-22-2004 06:29 AM
capture the process id when starting a background process jleavitt Shell Programming and Scripting 10 04-04-2002 05:04 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-21-2006
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
using popen with background process

hi,

how to work with a background process without a controlling terminal to make use of popen or system call ?

when ever i use popen or system function call in foreground process, there is no problem with respect to that .. but when the same program is run as a background process without a controlling terminal .. am receiving SIGSEGV to the program,

when i traced the program using truss the program is receiving ENOTTY...

pt to be noted is just as a background process with a controlling terminal there is no problem, only as a background process without any controlling terminal to it causes the problem

i just want to find whether a process is running or not with the above scenario..

could somebody show me the right pointer to proceed with ?

thanks a lot in advance
Reply With Quote
Forum Sponsor
  #2  
Old 08-21-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
I'm confused. Are you trying to call popen() against an already existing process?
Or is the process being created becoming a background process with no controlling terminal?

In either case popen will not work. As you know. You can test to see if popen is still working with a valid stream. popen returns a FILE *ptr, so you can call (on Linux) isastream(fileno(ptr)) or more generally ioctl(fileno(ptr), I_CANPUT,0). These will return errors approiately. Assuming I understand what you're asking...

You may also want to check out the isatty() function.
Reply With Quote
  #3  
Old 08-21-2006
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Quote:
Originally Posted by jim mcnamara
I'm confused. Are you trying to call popen() against an already existing process?
Or is the process being created becoming a background process with no controlling terminal?

In either case popen will not work. As you know. You can test to see if popen is still working with a valid stream. popen returns a FILE *ptr, so you can call (on Linux) isastream(fileno(ptr)) or more generally ioctl(fileno(ptr), I_CANPUT,0). These will return errors approiately. Assuming I understand what you're asking...

You may also want to check out the isatty() function.
this is the situation...
dedicated server program which is a background process without any controlling terminal which forks another server instance to handle client requests...
in that the program handling a client request - it checks whether a particular process is running or not..
the command string is framed it and it is passed to the popen function call and it fails there...
Reply With Quote
  #4  
Old 08-21-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
You actually should be using sockets or pipes. popen will fail as you see. fork does not create any stream which was not already there. If there were no tty streams to start with, forking will not create new ones for you unless you deliberately open them in the new process. You will have to use another IPC mechanism. popen requires tty streams.

By creating your own pipes you will be doing exactly what popen does, except that popen connects one pipe via stdout or stdin.
Reply With Quote
  #5  
Old 08-21-2006
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
jim,

thanks for the reply..

i can very well understand that forking will not create new streams...

to check out whether process is running or not .. i tried out a simple code

Code:
# include<stdio.h>
int main()
{
  char ps[100];
  char bin[100]="/users/mad/net/small";
  
  sprintf(ps, "sh -c /usr/bin/ps -fu %s | /usr/bin/grep '%s' | /usr/bin/grep -cv 'grep' > two.tmp", getlogin(), bin);
  system(ps);
  system("sh -c \"compress ftplog.log\"");
  return 0;
}
i ran this with controlling terminal - no problem - worked as expected
ran this as background and without any controlling terminal - none of the operation specified in system command is working..

where am i making the mistake ?
Reply With Quote
  #6  
Old 08-21-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
I can't try this, but you need stdin, stdout, stderr for that command line to work.
The only thing I can think of is to force creation of a new process with default streams to login on the same box. rexec, ssh, maybe telnet.

I don't know how to set up localhost authentication for ssh or remsh, but assuming it can be done try:
Code:
sprintf(ps,
"remsh localhost - l  %s /usr/bin/ps -fu %s | /usr/bin/grep '%s' | /usr/bin/grep -cv 'grep' > two.tmp", getlogin(), getlogin(), bin);
Reply With Quote
  #7  
Old 08-21-2006
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
jim, thanking u once again for the reply...

and the truth really is .. i cannot corelate what you are trying to explain...

to identify whether process is running or not why it should be complicated, a process on running on box A, to identify whether a process B running on the same box A.. why it should force a new process to be created and find out whether process is running or not ...

and i suspect the trap to be in this point .. (coloured one)
/usr/bin/ps -fu %s | /usr/bin/grep '%s' | /usr/bin/grep -cv 'grep' > two.tmp

when the redirection operator is used by a process without controlling terminal running in background, it expects a valid tty and hence throwing out ENOTTY..

could that be the problem underlying?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0