473,411 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

run a command in background in C

Could any one tell me what is the best way to run a command in
background in C?

I have tried something like the following:

system("perl t.pl &");

It works fine for the system call, but I noticed that there is a
problem with the server socket I created right before the system call,
I couldn't close that socket anymore after the system call. If I remove
the "&" in the system call, the code works fine. (I am working on a
redhat linux system)

Can anyone share some light on it?

Thanks.

Nov 14 '05 #1
9 6483

"askmore" <ya**@teragram.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Could any one tell me what is the best way to run a command in
background in C?

I have tried something like the following:

system("perl t.pl &");

It works fine for the system call, but I noticed that there is a
problem with the server socket I created right before the system call,
I couldn't close that socket anymore after the system call. If I remove
the "&" in the system call, the code works fine. (I am working on a
redhat linux system)

Can anyone share some light on it?
This is off topic for comp.lang.c

<OT>
http://manpage.org/cgi-bin/man/man2html?query=system
http://manpage.org/cgi-bin/man/man2html?3+fork
http://manpage.org/cgi-bin/man/man2html?3+exec

In the case of a fork/exec on linux, open Filedescriptors (which includes
sockets) are inherited by the child process.

In the first case (with '&') the parent process will not wait for the child
to terminate. Therefore the Filedescripor for the socket you opened, is
referenced by *two* processes, and hence cannot be closed.

In the second case (w/o '&') the parent process will wait(3) for the child
to terminate (The system() call will block) and hence, when your parent
process tries to close the socket in question, it will be the sole process
referencing it. Hence the call succeeds.
</OT>
Thanks.


De nada.
Nov 14 '05 #2
"askmore" <ya**@teragram.com> writes:
Could any one tell me what is the best way to run a command in
background in C?


Not here. C has no concept of "background" (such things are often
implemented in C, but they're not part of the language). Try a
newsgroup specific to your system, perhaps comp.unix.programmer

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3
askmore wrote:

Could any one tell me what is the best way to run a command in
background in C?

I have tried something like the following:

system("perl t.pl &");

It works fine for the system call, but I noticed that there is a
problem with the server socket I created right before the system call,
I couldn't close that socket anymore after the system call. If I remove
the "&" in the system call, the code works fine. (I am working on a
redhat linux system)

Can anyone share some light on it?


Probably, but not here. Commands, background, servers, sockets
have nothing to do with the portable C language. Find a group that
deals with your particular system.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #4
Thanks for all your help, just wondering is there a way to run the
command in background and close the socket appropretely?

Nov 14 '05 #5

askmore wrote:
Thanks for all your help, just wondering is there a way to run the
command in background and close the socket appropretely?


Yes, Why don't you try using two different threads? One thread tries to
run the command and another thread runs the normal program.

Nov 14 '05 #6

"Hariprasad Govardhanam" <ha*******************@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...

askmore wrote:
Thanks for all your help, just wondering is there a way to run the
command in background and close the socket appropretely?


Yes, Why don't you try using two different threads? One thread tries to
run the command and another thread runs the normal program.


<OT>
That would not help, since threads are implemented using lightweight
processes. I.e. a thread is a
process, too.

The "proper" answer is simply not to share the socket, i.e. open it after
your fork-exec, or close it
by when you are sure the child has terminated.
</OT>
Nov 14 '05 #7
Hariprasad Govardhanam wrote:
askmore wrote:
Thanks for all your help, just wondering is there a way to run
the command in background and close the socket appropretely?


Yes, Why don't you try using two different threads? One thread
tries to run the command and another thread runs the normal
program.


Thread? I see no thread here. Please point out where one is
mentioned in the Bible, otherwise known as K & R or in N869 or in
"The Standard".

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #8
"askmore" <ya**@teragram.com> writes:
Thanks for all your help, just wondering is there a way to run the
command in background and close the socket appropretely?


We don't know, and if someone posts an off-topic answer we can't check
whether it's accurate. You'll get much better information if you ask
in an appropriate newsgroup, probably comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #9
Thanks all for your kindly response and sorry about off-topic here.

The reason I choose fork/exec is that I would like to have the perl
script running in the background even after the user kills the main
program. I know this is a rare usage, but I have no choice here. :-(

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: MTriggs886 | last post by:
There isn't a command that exists that can serve as an alternative to EMBED in HTML 4.01 Transitional coding, is there? I've historically used this command to play MIDI files in the background of...
1
by: René | last post by:
Hello group, I have a question about a script i use. The function of the script is to dynamically write an ftp script and execute this with dos ftp.exe . This script always worked on a Windows...
8
by: Susan Bricker | last post by:
Is there a way to change the background color of command buttons? I know that I saw some VBA code somewhere that showed how to do this. I think it involved making the button transparent and then...
3
by: Susan Bricker | last post by:
Is it possible to make command buttons (in forms) have a background color? Or is there a means to simulate a command button and make it have a background color and able to kickoff a 'click' event?...
21
by: MLH | last post by:
If I choose a command button on a form in design view, open the properties box and click the Picture property and its wiz button, I'm presented with a lengthy list of pictures. Everything from Add...
2
by: ¹é¿ø¼® | last post by:
Hello, everybody. I want to call any shell command from php script using shell_exec(). The problem is that the next line of the php script would not be run until a few minutes after running the...
1
by: hsmcdonald | last post by:
Hello all, I have a mail function that sends parsed information to an employee distribution list. I was trying to setup a process where the admin can initiate a letter to this list, and...
1
by: Brice | last post by:
Hello I am new to python and I am really a bad programmer... As a linux user, I use quodlibet for listening to music. Quodlibet allows me to write plugins in python. I am trying to write...
5
by: WestAussie | last post by:
Hello I'm new to .NET and have been working through some exercises to get a bit more fluent. I have no idea what I have done to cause command buttons to have their background appearance...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.