Thread: Starting Programs from a C program in Linux

  1. #1
    Registered User sonnichs's Avatar
    Join Date
    Jul 2011
    Posts
    24

    Starting Programs from a C program in Linux

    I have a C program running on Ubuntu that starts up a power supply for a period of time and then stops it. During this period I need to start some ancillary programs on the same linux machine that read some sensors. The main program, and the ancillary programs are quite independent and do not need further intercommunication. (e.g. I don't need to wait or interrogate status). This helps insure the main program, which is critical, keeps running regardless of the outcome of the ancillary ones.
    --to put it simply, I want to start the ancillary tasks as though I entered them separately from the shell/keyboard that started the main task.

    So my question is how to accomplish this. I am looking at system, spawn, fork and exec and it is not entirely clear form the posts here or the man pages which fits. It seems as though some of these will "wait" the calling tasks. Others will share memory etc. I think maybe fork is the way to go, but I never like this method as apparently you have to copy all the code form both main and ancillary task into the same program and then select/run the appropriate code. system will apparently make the ancillary programs subsidiary to the calling program.

    Can someone point me to the best way to do this?

    Thanks
    Fritz

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,485
    Do the ancillary programs stop themselves when your main program stops the power supply?

    > --to put it simply, I want to start the ancillary tasks as though I entered them separately from the shell/keyboard that started the main task.
    A cheap way might be to do this in your main process
    system("ancillary &");
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2019
    Posts
    209
    Fork is an odd little thing. It is actually very close to the internal processes of UNIX/Linux design, but for an application it is an oddity.

    Spawn is what you really want to use. System might work, it is like your application launches a command at a shell prompt. If you issue that command with the "&", it tells Linux (whether from a system call or when you type a command manually) to run that command in a "background" process. Meaning, the system call will return quickly while the command goes on separately. It is the effect you want.

    Fork will do that too, but in a duplicate of your running program. The two are twins at that moment, where one goes off "inside" the fork function, and the other "skips" the fork function. The two processes are otherwise identical at that moment. It's a bit odd to get used to.

    Exec is going to replace the calling process. When you call exec, you're ending the process making the call and replacing it with the one you're calling. Not what you want.

    Spawn (on Linux might be clone) will start a new process, like system does (and fork, too). This is a "light" version of fork, much easier to deal with and conceptually quite natural to your intent. It works like "system", but does not take the command as if at a shell prompt - it runs the executable code you specify with arguments you provide. This is most likely the best choice.

  4. #4
    Registered User sonnichs's Avatar
    Join Date
    Jul 2011
    Posts
    24
    Salem- I should have added, that the ancillary tasks run quickly, opening the serial port and finishing well before the main task shuts down the power supply and sleeps.
    I tried "system" and it seems to work. I was just worried about what happens to memory, locks on the serial ports or other vestiges when the ancillary tasks finish. All these must be relinquished to the OS after completion. The main task runs for weeks and we must be sure that there are no memory overflows etc. I do not recall what happens to the task associated space when the "&" is specified but it is a good idea and I would think accomplishes the clean up when the task finishes, much as an independent task would.
    Niccolo's comments on spawn and fork are interesting. I certainly agree that fork is peculiar.....it has been over 35 years since I studied unix internals and I do not recall all of the nuances of fork startup, allocation and cleanup. I will try spawn a little later this week when I have time but between that and "system" I expect something will work.

    Thanks for the help
    cheers
    Fritz

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,485
    With few exceptions, when a process terminates, all the resources held by that process will be freed.

    So if repeated runs of ancillary are presently fine in that respect, then system("ancillary &"); will be as well.
    All the & means is "run in the background", which has the effect of making system() return immediately, rather than waiting for the sub-process to finish.

    > The main task runs for weeks and we must be sure that there are no memory overflows etc.
    Code:
    $ # Find the PID of the process, change 'fox' to suit
    $ ps -A | grep fox
    $ # Watch the process
    $ top -p 2359
    Use this to spot possibly increasing trends in say memory usage over time. If memory keeps creeping up, you have a leak.

    'top' is a swiss army knife kinda tool, so it pays to have a good read through the manual page.
    You can
    - choose the columns to display.
    - run in batch mode to log to a file.
    - other stuff
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User sonnichs's Avatar
    Join Date
    Jul 2011
    Posts
    24
    Thank you Salem----watching with top is a good idea--I will do that once I get this all running
    cheers
    fritz

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic C Programs Starting Up Slowly
    By ryant324 in forum C Programming
    Replies: 12
    Last Post: 02-05-2014, 09:44 AM
  2. Starting a detached process in Linux
    By Benji Wiebe in forum Linux Programming
    Replies: 3
    Last Post: 12-09-2011, 09:03 PM
  3. Starting programs
    By Molokai in forum C Programming
    Replies: 1
    Last Post: 04-16-2009, 10:10 AM
  4. starting linux programming
    By Micko in forum Linux Programming
    Replies: 11
    Last Post: 11-29-2005, 04:45 PM
  5. Starting Programs
    By CumQuaT in forum Windows Programming
    Replies: 5
    Last Post: 11-17-2003, 04:47 AM

Tags for this Thread