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 12-21-2005, 02:44 PM   #1
Brian1
LQ Guru
 
Registered: Jan 2003
Location: Seymour, Indiana
Distribution: Distribution: RHEL 5 with Pieces of this and that. Kernel 2.6.23.1, KDE 3.5.8 and KDE 4.0 beta, Plu
Posts: 5,700

Rep: Reputation: 65
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
 
Old 12-21-2005, 03:10 PM   #2
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
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.
 
Old 12-21-2005, 03:19 PM   #3
Brian1
LQ Guru
 
Registered: Jan 2003
Location: Seymour, Indiana
Distribution: Distribution: RHEL 5 with Pieces of this and that. Kernel 2.6.23.1, KDE 3.5.8 and KDE 4.0 beta, Plu
Posts: 5,700

Original Poster
Rep: Reputation: 65
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
 
Old 12-21-2005, 04:09 PM   #4
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
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.
 
Old 12-22-2005, 04:19 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

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

program1 &
wait $!
program2

what exactly is the problem/
 
Old 12-22-2005, 05:26 AM   #6
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
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?
 
Old 12-22-2005, 05:41 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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"
 
Old 12-22-2005, 05:54 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Try:

Code:
(program1 & sleep 10) ; program2

Last edited by jlliagre; 12-22-2005 at 05:58 AM. Reason: I misread the OP first question
 
Old 12-23-2005, 01:29 PM   #9
elyk1212
Member
 
Registered: Jan 2005
Location: Chandler, AZ USA
Distribution: Mandrake/Mandriva 10.2
Posts: 186

Rep: Reputation: 30
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.

Last edited by elyk1212; 12-23-2005 at 01:32 PM.
 
Old 12-23-2005, 04:59 PM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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 ...
 
Old 12-23-2005, 06:20 PM   #11
Brian1
LQ Guru
 
Registered: Jan 2003
Location: Seymour, Indiana
Distribution: Distribution: RHEL 5 with Pieces of this and that. Kernel 2.6.23.1, KDE 3.5.8 and KDE 4.0 beta, Plu
Posts: 5,700

Original Poster
Rep: Reputation: 65
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
 
Old 12-24-2005, 01:38 AM   #12
Harshandu
LQ Newbie
 
Registered: Dec 2005
Posts: 1

Rep: Reputation: 0
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.
 
Old 12-24-2005, 01:53 AM   #13
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
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"?
 
Old 12-24-2005, 12:04 PM   #14
elyk1212
Member
 
Registered: Jan 2005
Location: Chandler, AZ USA
Distribution: Mandrake/Mandriva 10.2
Posts: 186

Rep: Reputation: 30
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.
 
Old 12-25-2005, 04:33 PM   #15
Brian1
LQ Guru
 
Registered: Jan 2003
Location: Seymour, Indiana
Distribution: Distribution: RHEL 5 with Pieces of this and that. Kernel 2.6.23.1, KDE 3.5.8 and KDE 4.0 beta, Plu
Posts: 5,700

Original Poster
Rep: Reputation: 65
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
 
  


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
Start Date + Time Duration = End Date/Time calculator? ToBe Linux - General 3 09-26-2005 10:17 AM
Programs in FluxBox take ages to start or don't start at all hubabuba Slackware 1 02-20-2005 09:21 AM
start date and start time of a process - jiffies Yoko Programming 0 12-04-2004 08:12 AM
how to do real time programs? frostmagic Programming 4 02-02-2004 10:53 AM
GUI based interphase to start programs at boot time. softgun Debian 2 10-24-2003 01:45 AM

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

All times are GMT -5. The time now is 02:30 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