ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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?
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.
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.
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).
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.
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...
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...
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.