want to run a function in background


 
Thread Tools Search this Thread
Top Forums Programming want to run a function in background
# 1  
Old 01-16-2006
want to run a function in background

consider the given prg.

main() {
.....

function1(); /* to write into a file or log */

printf(" ");
.....
}

when the control reaches function1(), it should get executed in the
background.At the same time main's printf(" ") statement should also get executed.i.e function1() should be independent of the main after it is called.Both should run simultaneously.


Also the background process should not return to main and handle other requests in background (e.g waiting for the msg Q to get filled up).
# 2  
Old 01-16-2006
Quote:
Originally Posted by bankpro
consider the given prg.

main() {
.....

function1(); /* to write into a file or log */

printf(" ");
.....
}

when the control reaches function1(), it should get executed in the
background.At the same time main's printf(" ") statement should also get executed.i.e function1() should be independent of the main after it is called.Both should run simultaneously.


Also the background process should not return to main and handle other requests in background (e.g waiting for the msg Q to get filled up).
fork within the program to create a child process and execute the function1 inside the child process
and printf would resume with the parent process

Code:
pid=fork();
if( pid == 0 ) //child
{
function1();
}
else if( pid > 0 ) //parent
{
printf("");
}

you need to control regarding which should run first parent or child.
# 3  
Old 01-16-2006
but fork returns

thanks matrixmadhan, but while using fork, it will return to the main.
we want it to run in the background handling requests(writing to file whenever
messge Q is full)
# 4  
Old 01-17-2006
user seperate Threads for each operation.
# 5  
Old 01-20-2006
ok bankpro, fork is just a diversion from the main program. now you dont want it to return to main, then you do one thing, put the function part as a different program. and instead of calling the function, call "exec" function with the program name containing your function as the argument.
and since you want to make it run in background, you have to make the new program called in child using fork, as a daemon program.
I am sure you will find code snippets for a daemon program on the forum, if you do a search, if not check out this link
http://www.geocities.com/hemantborole/
click on KnowledgeBank, on the next page, click on C under My Programs, and in the list of programs, click on daemon.c to see the sample of the daemon process.
# 6  
Old 02-01-2006
thanks

Hello Linuxpenguin


ur logic helped us to solve our issue.

thanks 4 ur help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execute a function in background and then suspend it

Here is some back ground on the script. The script is to poll an arbitrary number of DB's. To do this I am creating a function that takes the file_path to the DB and the min poll interval as arguments. The function will be called for each DB and then ran in the background. The function I was... (6 Replies)
Discussion started by: ryandavison
6 Replies

2. Shell Programming and Scripting

How to run Background process one after another

Hii Friends, I am using Perl CGI. I am running A SCP Command via Perl CGI in Background. Like system("scp -r machinename:/PathOfFile/ /Path/WhereToCopyIt/ &) This Copy Process takes some times lets say 15 min. Now I want When This copy process gets complete then send me... (5 Replies)
Discussion started by: Navrattan Bansa
5 Replies

3. Shell Programming and Scripting

how to run in background mode.

Hi All, i'm a newbie here, i'm just wondering in how do i run my script in background then echo it if it's done. Please advise, Thanks, -nik (1 Reply)
Discussion started by: nikki1200
1 Replies

4. Shell Programming and Scripting

Run wineconsole in background

Hello everybody, I'm making a script for running a .bat process on wineconsole, but I want that wineconsole doesn't show up when I call it from the script. The script is named "reset" and it looks like this: When I execute it, it will show up the wineconsole window on top, and that is... (3 Replies)
Discussion started by: taurokpo
3 Replies

5. Shell Programming and Scripting

background scripts run-help

Looking for a logic where say i have a script called parent_script which is used to call other 4 to 5 child scripts in background as.. cat parent_script # containing 65 lines 1 2 .. 35 while read child_script 36 do 37 ./child_script_name& 38 done< ${SCRIPT_LISTS} 39 40 # Need to have... (2 Replies)
Discussion started by: michaelrozar17
2 Replies

6. Shell Programming and Scripting

A question about the PID of a background function

Dear all, I'm writing a KornShell script that calls inside it a function in background mode #!/bin/ksh function myfunction { . . .} myfunction |& . . . How can I capture the PID of the function myfunction that runs in background? Thanks in advance :) (2 Replies)
Discussion started by: dariyoosh
2 Replies

7. UNIX for Dummies Questions & Answers

can we run ssh2 in background

Hi, I was trying to run ssh2 command in background... but i got follwoing error message saying that process has been stopped.. + Stopped(SIGTTOU) Anyone have any idea about this??? Appreciated your help.. (3 Replies)
Discussion started by: pvamsikr
3 Replies

8. Shell Programming and Scripting

how to run script at background

Hi all, I have a script like: echo Please input list file name: read listn for file in `cat $listn.txt` do send_file $file done normally, I will run the script like: :. resendfile Please input list filename: list1 #Then, the script will resend all file from the list1. However,... (4 Replies)
Discussion started by: happyv
4 Replies

9. Shell Programming and Scripting

run a shell in the background

How can I run a shell in the background? cat test.sh #!/bin/sh sleep 600 Thank u very much indeed! (2 Replies)
Discussion started by: GCTEII
2 Replies

10. UNIX for Dummies Questions & Answers

subshell & background function

Hello all, Can someone explain to me the advantage between using subshell over a function call in scripts? To me these are the same. Am I wrong to think this? (4 Replies)
Discussion started by: larry
4 Replies
Login or Register to Ask a Question