LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 08-20-2005, 03:31 PM   #1
vishamr2000
Member
 
Registered: Aug 2004
Posts: 210

Rep: Reputation: 31
paralell program execution


Hi to all,

I have 2 functions - func1() and func2(). func1() starts executing first. Before it returns i want it to send a signal to func2() so that it can start. But after the signal is sent i want func1() to continue its execution (without having to wait for func2() to return. I have only one processor.

What can be used - threads, writing a value in a file and have a daemon check that file continuously.

Any input will be very much appreciated..

Regards,
Visham
 
Old 08-20-2005, 03:45 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Threads share variables. If you make a global one, all thread have access to it. Waiting in a loop is not an elegant solution, however. You can use a mutex, for example. When func1 releases it, func2 (waiting on the mutex) will be released. With func1 still running.
 
Old 08-21-2005, 12:03 AM   #3
vishamr2000
Member
 
Registered: Aug 2004
Posts: 210

Original Poster
Rep: Reputation: 31
Hi Mara,

first of, many thx for the reply..

I am sure you are right...wanted to ask you if you know of a sample program like the one you are telling me..i don't have an idea how to do it..

Thx in advance...

Warm regards,
Visham
 
Old 08-21-2005, 12:51 AM   #4
vishamr2000
Member
 
Registered: Aug 2004
Posts: 210

Original Poster
Rep: Reputation: 31
Hi Mara,

I actually made a mistake in explaining my problem..There are actually two programs. func1() is found in Program1 (code found in the kernel) and there is Program2 that I have written (not func2()). I want to execute Program2 everytime func1() in Program1 is executed. But func1() must not wait for Program2 to return. It should continue its execution after it signal Program2 to start executing. Since there are two distinct processes, will the solution you suggested hold good?

Any input will be very much appreciated..

Warm regards,
Visham
 
Old 08-21-2005, 01:52 PM   #5
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
There are important design decisions you must made before deciding on the synchronization method. Depending on it, you may use semaphore (but different than used in one process), pass signal to process on change its status (scheduling).

The thing is if you want program2 to run in kernel or user mode. It affects everything.
 
Old 08-21-2005, 04:08 PM   #6
vishamr2000
Member
 
Registered: Aug 2004
Posts: 210

Original Poster
Rep: Reputation: 31
Program2 runs in user mode. It's like every time a particular function in Program1 is called, it raises a signal for user space Program2 to execute. After the signal is sent, execution of the rest of the code in that function in Program1 continues.

I really have no clue how to do it..do tell me what you think will be the best way to do this and if you any example of sth resembling this or that explains how to create such a signalling function, pls do paste it here.

Pls help..

Warm regards,
Visham
 
Old 08-22-2005, 06:53 AM   #7
vishamr2000
Member
 
Registered: Aug 2004
Posts: 210

Original Poster
Rep: Reputation: 31
Hi to all,

Does anyone have a simple working example of sigaction()? I want to do sth like this:

Program1:

void main()
{
.
.
.
sigaction() code

printf("Program1 finished execution");

}


Program2:

void main()
{

.
.
.
signal handler() code

printf("Program2 finished execution");

}


When Program1 sends the signal, Program2 starts executing. Program1 continues to execute. The statement in Program1 is to be printed before that in Program2. Also, Program1 must not wait for the signal handler to return nor must the signal handler return anything to Program. It's like Program1 sends a signal and then doesn't care what happens to it (forgets about it).

Is this possible...

I badly need help with this..

Warm regards,
Visham
 
Old 08-22-2005, 07:45 AM   #8
sateeshgalla
LQ Newbie
 
Registered: Apr 2005
Location: Bangalore
Posts: 16

Rep: Reputation: 0
Hi Visham,

You can try this way. In program 1, to start program 2, you call fork. Then in the fork, u call exev group function to run program 2. Now program 1 and program 2 forms two processes and execute independently.

One more option is you call detached thread in program 1. In the thread call program 2. so any both threads run independetly. If you want the statement to be executed before program 2 start, use semaphore.

regards,
sateesh
 
Old 08-22-2005, 09:01 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
why is everyone obsessed with threads?
processes are efficient in linux.


http://www.radwin.org/michael/blog/2...d_harmful.html
 
Old 08-22-2005, 01:25 PM   #10
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
here is some more reading.

http://www.linuxquestions.org/questi...light=schneidz

hope this helps,
 
Old 08-22-2005, 03:48 PM   #11
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Quote:
Originally posted by bigearsbilly
why is everyone obsessed with threads?
processes are efficient in linux.


http://www.radwin.org/michael/blog/2...d_harmful.html
In my opinion threads are much more natural in cases when you need more shared data. Using IPC (queues, semaphores etc etc) is harder than doing it with threads. If there's nearly no interaction between processes, it's OK to stay with them. But thread synchronization and process synchronization (when needed) are nearly exactly the same when we think about concepts and difficulties.

Threads are shown (and heavily used) for less than intermediate level programming classes. People deal with them well, so they don't seem to be hard.

Finishing my thread offtopic when waiting for orginal poster response...
 
Old 08-22-2005, 11:52 PM   #12
vishamr2000
Member
 
Registered: Aug 2004
Posts: 210

Original Poster
Rep: Reputation: 31
Hi to all,

Many many thx for all your replies...

The problem that still remains with the solutions proposed is that the overhead is far too much than what is acceptable for my system. Programs 1 & 2 execute on a packet basis, i.e all packets that are declared valid and are accepted by my firewall. Creating processes (and even threads) for each packet and then terminating them, will tax my system considerably. I've been told that using signals is better off in the sense that it can be made not to return anything to the signal sender. Once the signal is sent, Program1 doesn't care what happens to it. The signal is used to let Program2 know that it has to execute. Once execution of Program2 is complete, it stops and waits for the nest signal from Program1.

Program1 is basically IPTables kernel code...i'm trying to modify a function in it for this. Program2 is a user space code that I must write...the signal part in both these programs is causing problems..

Anyone has examples of creating signal handlers...the ones i've seen are so complicated...

Your comments and input are most welcome...

Thx again..

Warm regards,
Visham
 
Old 08-24-2005, 12:52 PM   #13
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
There's a nice text about signal handling: http://www.linux-mag.com/content/view/394/2241/ (with simple example). The thing is easy, handler is just a normal function. The only thing is to register it correctly.
 
Old 08-25-2005, 05:47 AM   #14
vishamr2000
Member
 
Registered: Aug 2004
Posts: 210

Original Poster
Rep: Reputation: 31
Hi to all,

Many thanks for the link Mara..i'll have a look at it and see if I can do it.

Warm regards,
Visham
 
Old 08-25-2005, 10:12 AM   #15
sudhir_pandey
LQ Newbie
 
Registered: Aug 2005
Posts: 5

Rep: Reputation: 0
a example of signal programme

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>

/*********Signal catcher***************/
static void catch_sig(int signo)
{
if( signo == SIGINT)
{
alarm(0); /* Cancel the timer */
write(1,"CAUGHT SIGINT.\n",15);
}
else if (signo == SIGALRM)
write(1,"CAUGHT SIGALRM.\n",16);
}

int main(int arc,char *argv[])
{
sigset_t sigs; /* SIGINT + SIGALRM */
struct sigaction sa_old; /* Saved Signals */
struct sigaction sa_new; /* New Signals */

sa_new.sa_handler = catch_sig; /* Signal handler */
sigemptyset(&sa_new.sa_mask); /* Empty mask */
sigaddset(&sa_new.sa_mask,SIGALRM); /* Add SIGALRM */
sigaddset(&sa_new.sa_mask,SIGINT); /* Add SIGINT */
sa_new.sa_flags = 0; /* No FLags */

sigaction(SIGINT,&sa_new,&sa_old); /* catch SIGINT */
sigaction(SIGALRM,&sa_new,0); /* catch SIGALRM */

sigfillset(&sigs); /* All signals */
sigdelset(&sigs,SIGINT); /* Exclude SIGINT */
sigdelset(&sigs,SIGALRM); /* Exclude SIGALRM */

puts("You have three seconds to SIGINT");

alarm(3); /*Time out in three seconds*/
sigsuspend(&sigs); /*Wait for SIGINT or SIGALRM*/

puts("Done.");
return(0);
}

just try if this example is of any help....an do tell if it is?
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ redirect program execution blizunt7 Programming 6 09-13-2005 12:09 PM
execution of one program inside of another microsoft/linux Programming 2 03-18-2005 10:24 AM
Key bindings program (custom pasting, program execution, etc.) jrdioko Linux - Software 2 02-05-2005 09:09 PM
program execution at startup DeeDub Linux - Software 2 09-11-2003 01:25 PM
program execution monitoring imp Linux - General 1 05-26-2002 09:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:36 PM.

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
Open Source Consulting | Domain Registration