LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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
 
LinkBack Search this Thread
Old 07-24-2003, 03:37 AM   #1
Acherion
LQ Newbie
 
Registered: Jul 2003
Posts: 3

Rep: Reputation: 0
Running bash script files in C++ (Kylix)


Hey all,

I'm running Kylix Open edition on Red Hat 7.3, and im coding a front end for a project of mine (for university).

The project consists of running a few script files, and the front end consists of buttons that when clicked, runs the script files.

However i've been having difficulties in actually making the buttons work. Ive tried spawn() and fork() however these seem not to work. Any ideas?

Thanks
 
Old 07-24-2003, 04:28 AM   #2
DIYLinux
Member
 
Registered: Jul 2003
Location: NL
Distribution: My own
Posts: 92

Rep: Reputation: 17
Try system(3) from the standard C (thats right, plain vanilla C) library. Basically, it runs a command, passed in a C style string (char*) just like it would be done by /bin/sh
 
Old 07-24-2003, 04:52 AM   #3
Acherion
LQ Newbie
 
Registered: Jul 2003
Posts: 3

Original Poster
Rep: Reputation: 0
I have tried using system() calls as well, however I would really like for the script files to run (and terminate) concurrently with the GUI (ie. multitask). I want the script files to "run in the background", so that the user can still click in the GUI and not having to wait until the script file has finished running.

I have tried making the button run xterm and then run the script file, however the xterm window just pops up and then disappears, or sometimes not show up at all. The script files are just scripts that configure and make some applications that are stored in the home directory. After the script file has "executed", I go to the folders that the script file should have configured and maked, but it seems like it hasn't done anything at all.

I hope you can see why I would like the script files to multitask with the GUI, as some programs take quite a while to compile, configure and make. Having the GUI not responding to user's clicks while the script file is running makes it look like the GUI has "hung", and doesn't produce a professional-looking program

Any commentary would be greatly appreciated.
 
Old 07-24-2003, 05:47 AM   #4
DIYLinux
Member
 
Registered: Jul 2003
Location: NL
Distribution: My own
Posts: 92

Rep: Reputation: 17
First of all: I mean the function 'system', documented in manual section 3. That why I wrote system(3). (Ancient UNIX lore).
System calls are the way a program tells the kernel to do something for it. The function system(3) would use the system calls fork(2), exec(2) and others.

Back to the question how to multitask/thread.

In shell scripts, use the ampersand (&) after a command to place it in the background, wait to wait for completion of a job, fg to put a background job in the foreground and jobs to list all currently running jobs. The bash help function and manual provide more info.

Example:
> sleep 1000 &
[1]
> sleep 999 &
[2]
> fg 1
(sleep 1000 now in foreground)

I think this is the info you need. Just to be complete, this is how its done by the pro's:

In C/C++, the classic way to multitask is using fork(2). It returns twice (!), once for the parent process, and once for the newly created child. In the child, you would typicaly exec another program; this is how the system(3) function is implemented.

Another easy way is popen, which gives you a stdio stream thats connected to the stdin/stdout of your newly created child process.

Multitasking is rather easy, because synchronzation is simple. You can use lock files (just an ordinary file, whose presence indicates a certain action has taken place). Multiple programs can simultaneously write to the same file in append mode (not on NFS though).

Multithreading involves using the pthread functions. A running program can create aditional 'threads', which are like programs that share their data running at the same time. It is rather involved, because you have to deal with synchronization yourself, using semaphores, mutexes and condition variables. Because everything is shared, this can be quite hard. And then there is the dreaded deadlock....
 
Old 07-25-2003, 11:35 PM   #5
Acherion
LQ Newbie
 
Registered: Jul 2003
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for that, much appreciated information

I've got the whole front end running using popen, seems to be the most simple implementation.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script running as daemon Satriani Programming 12 03-29-2011 05:23 PM
Bash Script - How do I make sure my program is still running? nro Linux - Newbie 4 08-28-2005 01:57 PM
Running two copy's of the same bash script AndiOliver Programming 2 10-06-2004 03:46 AM
bash script, monitoring running PID? thebover Programming 4 08-26-2004 02:42 PM
Running script with -m option / Kylix installation pchaisty Linux - Software 1 05-27-2004 03:01 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration