LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack Search this Thread
Old 09-06-2007, 02:55 PM   #1
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-12/ fc-11-live-usb/ aix
Posts: 1,829

Rep: Reputation: 134Reputation: 134
run c function in the background


for example in bash:
Code:
(
while [ 1 ]
do
 echo hello-world
 sleep 1
done) &
cd p2501
will go to the project-2501 directory while echo-ing hello-world in the background.

how does one do something similar in c ?

Code:
while(1)
{
 printf("hello-world\n");
 usleep(1000000);
}     // background ?
chdir("p2501");
i figure some mixture of fork but i dont want it to overwrite the current process in memory.

am i not understanding how fork works ?

Last edited by schneidz; 09-07-2007 at 11:58 AM.
 
Old 09-06-2007, 03:10 PM   #2
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-12/ fc-11-live-usb/ aix
Posts: 1,829

Original Poster
Rep: Reputation: 134Reputation: 134
why is there 2 of these ?

amways, i think i answered it here:
http://www.linuxquestions.org/questi...highlight=fork

i am out of practice so it isnt obvious to me how to make it work. but i will try:
Code:
int p;
p = fork();
if(p > 0)
{
 while(1)
 {
  printf("hello-world\n");
  usleep(1000000);
 }     // background ?
}
chdir("p2501");
 
Old 09-06-2007, 03:19 PM   #3
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-12/ fc-11-live-usb/ aix
Posts: 1,829

Original Poster
Rep: Reputation: 134Reputation: 134
confirmed, works
 
Old 09-06-2007, 11:41 PM   #4
lazyFoot_theMighty
LQ Newbie
 
Registered: Jul 2006
Location: Canberra Australia
Posts: 28

Rep: Reputation: 16
If that is what you wan't, then go ahead. Using fork(), however, will create a new full process (Address space, stack, heap etc). It is often useful to just create multiple threads (control flows), within a single process. Taking this approach will allow shared memory access, as opposed to using pipes or sockets, and reduce memory overheads. Look at what is provided by the pthread library. I'm not a fan of the concurrency in posix, but pthread will provide everything you need. Saying this, fork() is fine for many applications as it has conceptual simplicity going for it.
 
Old 09-07-2007, 08:29 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,016

Rep: Reputation: 91
or alternatively,

threads considered harmful

http://www.google.co.uk/search?hl=en...G=Search&meta=
 
  


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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
poepn or system function call executed by a background process kshkid Programming 1 08-24-2006 04:04 AM
How do I run a function within a function arguement? C/C++ RHLinuxGUY Programming 12 05-19-2006 03:29 PM
how to run any binary in background - background process tej Linux - Newbie 1 08-16-2004 01:27 AM
Run a program in the background??? KaptinKABOOM Linux - Software 5 07-06-2004 07:40 AM
alias or function in .bashrc, which sends command to background ngomong Linux - General 1 04-23-2002 10:50 PM


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

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration