LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   start two programs at the same time. (https://www.linuxquestions.org/questions/programming-9/start-two-programs-at-the-same-time-395365/)

Brian1 12-21-2005 02:44 PM

start two programs at the same time.
 
I was looking to start two programs at the same time. Reason & will not work is one needs to run before the other. The first one if using & still will not contuining until it is stopped adn then the next one can start.

So what I am thinking is in a script somehow tell it to start maybe two terminal sessions and then each one run the needed app. Is this called forking or am I off base here.


Thanks for any help.
Brian1

bulliver 12-21-2005 03:10 PM

Yes, you will need to fork. If your functionality was written directly in your program, then you could also use threads, but I don't think threading will work for launching external programs.

As far as starting two terminal session, why bother? After your fork, you can start the two programs directly from either a "system" or one of the "exec" family of system calls.

However, you cannot use bash scripting, it will not fork. You must use a higher-level language such as perl. python, C, C++ etc...

If you don't know how to do this, I could probably whip you up a wrapper in python quite quickly.

Brian1 12-21-2005 03:19 PM

Thanks for the info. time to learn a little more perl. Never tried python before, think it would be a better language to learn.

Thanks
Brian1

bulliver 12-21-2005 04:09 PM

Yeah, I find python considerably easier than Perl. Perl just makes me want to bash my head against the wall...

Also, for clarification, bash will fork, but it will not run forked processes concurrently, so it is of no use to your application here.

bigearsbilly 12-22-2005 04:19 AM

why not?
program1 && program2 &
or you can use wait:

program1 &
wait $!
program2

what exactly is the problem/

bulliver 12-22-2005 05:26 AM

bigearsbilly:

He wants them to run at the _same_ time.

Code:

program1 && program2 &
This will not run program2 until program1 has successfully completed...

Code:

program1 &
wait $!
program2

I cannot tell from the wait manpage what exactly this is doing. From what I gather, this is doing much the same as your first example ... can you explain?

bigearsbilly 12-22-2005 05:41 AM

Right.
He hasn't explained the problem clearly.

start 2 terminals and run?

Code:

xterm -e  ksh "ls;sleep 5"
xterm -e  ksh "date;sleep 5"


jlliagre 12-22-2005 05:54 AM

Try:

Code:

(program1 & sleep 10) ; program2

elyk1212 12-23-2005 01:29 PM

or, if you want a low level efficient solution, why not use C? Here it is:


Code:

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

int main(void)
{

  int process = fork();

  if(process == 0)
  {
      char* arguments[] = {"mkdir", "folder"};
      execvp("mkdir", arguments);
  }

  else
  {
      // Reapeat similar code here for other process, etc
  }
  return 1;
}

This example is bad code since the program is hard coded, and the example is mkdir, but you get the idea.

Now, it will run without interpreters or any unnecessary overhead.

jlliagre 12-23-2005 04:59 PM

It would help if you Brian1 explain with more details what you want to achieve.
The more I read your initial posting, the less I understand the problem ...

Brian1 12-23-2005 06:20 PM

Thanks for the ideas. What I want to do is either use one command or a click of the mouse to start 2 programs. The issue is the #1 program must be started before #2 program. The problem is if using either & or && does not help with the #1 program. When the #1 program starts it waits for one to press ctrl-c to stop it. It will not allow anything to start in that terminal. I next need to open a 2nd terminal to start the #2 program. #2 program runs it job and then returns back to a command prompt. But #1 must run before #2.

So using the example above program1 && program2 & with not goto the second program because progrma #1 is waiting for one to press ctrl-c to stop it. If ctrl-c ispressed the the #2 program starts but is of no use because #1 has stopped.

What this is on is activating the slmodemd program for my winmodem. Once it is running then I can either start kppp or wvdial program. Just wanted one single click to get it all going.

This is what I have started with. I can do it with starting 2 scripts but wanted to make it all work in one script.

Contents of modemstart.
Code:

#!/bin/bash

kdialog --msgbox "Started slmodemd --alsa"
/sbin/slmodemd --alsa
kdialog --msgbox "Sucessfully stopped slmodemd --alsa"


# if [[ $? -ne 0 ]]
# then
#    kdialog --error "Problem doing slmodemd --alsa"
# else
#    kdialog --msgbox "Successful doing slmodemd --alsa"
# fi

Contents of modemstop
Code:

#!/bin/bash
/usr/bin/kppp
killall slmodemd

So now from program one I send a dailog box saying the slmodemd program has started but it actually starts in the next line. I have to do it that way because slmodemd next thing is waiting for ctrl-c to stop it. I then press modemstop script which actual will dail my ISP when not at home on dsl for getting mail. When I exit kppp it then runs the pkill command which will kill slmodemd and then run the dailog box message to the screen that says the modem has stopped.

If I could just start slmodemd from the startup it would be great, but anywhere I putting it of course cause the startup to hang because it is waiting for ctrl-c to stop it.

Hope this is clearer. I have not checked out python or perl yet. Might have time of the weekend for a crash coarse. Might get a nice book for christmas.

Thanks for all your time.
Brian1

Harshandu 12-24-2005 01:38 AM

One more solution, write a program and create two threads in it.in each thread using exec call or system command in each of the threads you can run the two programs at the same time.

bulliver 12-24-2005 01:53 AM

Isn't slmodemd a daemon that runs in the background anyway?

Does this do what you need:
Code:

#!/bin/bash

/sbin/slmodemd --alsa
/usr/bin/kppp
killall slmodemd

I took out your "kdialogue"s, because they seem to go against your desire for starting with 1 click/or script. Why slow yourself down by having to click "ok"?

elyk1212 12-24-2005 12:04 PM

Quote:

Originally Posted by Harshandu
One more solution, write a program and create two threads in it.in each thread using exec call or system command in each of the threads you can run the two programs at the same time.

This is a good idea, but they will share the same memory space if you do it this way. Depends on your needs, I guess. Your method would consume less memory, as the fingerprint would be smaller. Also, if the process crashes, both threads crash. If you use fork() then you will have 2 different robust processes with their own memory space.

Brian1 12-25-2005 04:33 PM

No that does not work. once /sbin/slmodemd --alsa is started the rest of that seesion is stalled. As soon as one hits ctrl-C then it will continue. When ctrl-c is pressed that stops the modem driver. So starting kppp is of no use since the modem driver is stopped.

Thanks for the help.
Brian1


All times are GMT -5. The time now is 11:42 PM.