LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-25-2023, 07:49 AM   #1
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,622
Blog Entries: 40

Rep: Reputation: Disabled
[C/C++] arguments to child process.


I am *not* programming this but try to understand a program. I have a feature-request in mind but cannot say, if it makes sense: Allow an external process to receive an optional argument,

Situation as I interpret it:
In a fork()-induced child process, an external routine is started with execl(), posix_execl being an alias:
Code:
posix_execl(path, pathname, (char*) NULL);
Then, in the main-process, the pipes are set for IPC:
Code:
rv = posix_fcntl(fd_out[1], POSIX_F_SETFL, POSIX_O_NONBLOCK);
if(-1 != rv)
  {
     rv = posix_fcntl(fd_in[0], POSIX_F_SETFL, POSIX_O_NONBLOCK);
  }
Afterwards, the content of a text-file is fed to the STDIN of the running external program.
Code:
rv3 = posix_write(fd_out[1], (void*) &content[i], len - i);
My question:
I understand that the external routine receives arguments in the call to execl(). But if the actual content which is piped to STDIN – in the last line of code above – sets a condition, how would you communicate this condition to the external process?

Let's say the content can be of diverse nature and the specific type of content should be known to the external program ...

As I see it, my idea does not justify the profound changes which it would necessitate.

This is basically about execl() and write(), but please say, if my scenario is not clear.

Thank you
 
Old 04-25-2023, 09:26 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,246

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
Quote:
But if the actual content which is piped to STDIN – in the last line of code above – sets a condition, how would you communicate this condition to the external process?
That would be communicated via the actual content that is piped to the external process' STDIN. Which the external process reads and presumably understands.

Did I miss something?

(If you just open a text file and pipe its contents into a child process, then the contents of that file would not be setting a condition. Code in either the parent or child process would be setting the condition).

Last edited by dugan; 04-25-2023 at 12:46 PM.
 
Old 04-26-2023, 12:58 PM   #3
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,622

Original Poster
Blog Entries: 40

Rep: Reputation: Disabled
Quote:
Originally Posted by dugan View Post
That would be communicated via the actual content that is piped to the external process' STDIN. Which the external process reads and presumably understands.

Did I miss something?
No, but I may have expressed myself poorly. What I am hoping for, is a way to communicate an optional additional value – as an argument – to the external process. It should certainly be possible to define a delimiter and then to append anything to the text which is piped-in to STDIN, but as I would like the additional data to be optional, an argument would probably be better.

As I see it, the “additional data” must already be known upon calling execl() or the whole procedure would have to be changed, in a way that I do not understand and which can have too many consequences. In both cases, I refrain from suggesting a new feature.

Last edited by Michael Uplawski; 04-26-2023 at 01:00 PM. Reason: cosmetics
 
Old 04-26-2023, 03:17 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,246

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
Are you asking if you could code the child process to take a command-line argument, and also code the parent process to launch it with that command-line argument?

If you're writing the child process, then that's easy.

Last edited by dugan; 04-26-2023 at 11:38 PM.
 
Old 04-27-2023, 07:30 AM   #5
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,622

Original Poster
Blog Entries: 40

Rep: Reputation: Disabled
Quote:
Originally Posted by dugan View Post
Are you asking if you could code the child process to take a command-line argument, and also code the parent process to launch it with that command-line argument?

If you're writing the child process, then that's easy.
I have done that... but have never used the technique of the program I am talking about: The Newsreader flnews.
My code to launch an external application contained all, the path to the program and all possible arguments. What I see here is a process which is started *without* any arguments but will later processes data that comes in via STDIN, a text in this case.

What I am wondering about is,
  • whether the arguments could come to existence *only when* the data to pipe into the child process is available
  • whether this will need a different function than execl(), a modification that I consider too important.
  • (most of all) whether I miss something and thus cannot (yet) evaluate the consequences of my potential feature request.
Maybe shared memory would help, but I cannot say what the caveats are.

Put another way, my C-knowledge is rotten and dates from many standards ago.

Last edited by Michael Uplawski; 04-27-2023 at 07:32 AM.
 
Old 04-27-2023, 08:11 AM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,246

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
Link to the actual code you’re asking us to help you look at.
 
Old 04-27-2023, 09:07 AM   #7
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,153

Rep: Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265Reputation: 1265
Command line arguments must exist when the program is started. There is no standard mechanism to add more elements to argv later, or any way to tell a program to look at it's arguments again. Of course you could come up with your own nonstandard way, but it seems far more complicated than other ways to provide input to a program: files, signals, shared memory, sockets, etc.
 
Old 04-28-2023, 03:24 AM   #8
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,622

Original Poster
Blog Entries: 40

Rep: Reputation: Disabled
Quote:
Originally Posted by smallpond View Post
Command line arguments must exist when the program is started. There is no standard mechanism to add more elements to argv later, or any way to tell a program to look at it's arguments again. Of course you could come up with your own nonstandard way, but it seems far more complicated than other ways to provide input to a program: files, signals, shared memory, sockets, etc.
Thank you.
Without wanting to modify profoundly the program, appending to the pipe appears to be the simplest way to implement my idea. The problem is then only, that the receiving process must be prepared to parse *or to actively ignore* the additional data.

Cheerio.
 
Old 06-21-2023, 07:10 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
blah

You could set an Environment variable or pass it in using 'execvpe' which the child can test with 'getenv'
You could have a simple protocol for the stdin, say start a line with a token and work accordingly:

GBP .....
USD .....
CHF .....

simplicity
 
Old 01-21-2024, 06:42 AM   #10
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,622

Original Poster
Blog Entries: 40

Rep: Reputation: Disabled
Quote:
Originally Posted by bigearsbilly View Post
You could set an Environment variable or pass it in using 'execvpe' which the child can test with 'getenv'
You could have a simple protocol for the stdin, say start a line with a token and work accordingly:

GBP .....
USD .....
CHF .....

simplicity
Yes. I will consider this.
For the time, I have evaded the situation that I hoped to handle by additional program arguments, i.e. evaded the need for FLNews to talk to a post-processor application, by imposing more configuration to the end-user. The (more or less) result of my endeavor is https://rubygems.org/gems/flnews_post_proc.
 
Old 01-21-2024, 09:15 AM   #11
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Very old blog I wrote about fork() and exec() and passing arguments to the child.

https://www.linuxquestions.org/quest...ocesses-35540/

Skip to the section titled Creating Child Processes for a description of what you're asking about.

There's another blog about the use of select() to wait on data from pipes.
 
Old 01-22-2024, 12:26 AM   #12
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,622

Original Poster
Blog Entries: 40

Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
Very old blog I wrote about fork() and exec() and passing arguments to the child.

https://www.linuxquestions.org/quest...ocesses-35540/
I must read it again and maybe once more during my own experimenting, but that is a cool doc.
 
Old 01-22-2024, 12:52 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,976

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
what you are speaking about is (are) two different things.
You can specify command line arguments when you start a program to pass parameters to it.
You can use pipes (for example) to communicate with a running process, for example send some data/command to it and also it may respond using pipe.
 
1 members found this post helpful.
Old 01-23-2024, 12:21 AM   #14
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,622

Original Poster
Blog Entries: 40

Rep: Reputation: Disabled
I will mark this thread as [Solved] for now.

While my solution to use a permanent configuration instead of parameters for the child process is much less flexible than the communication via pipes, it has however clarified for me that the optional behavior that I want to enable, should not be part of the main program. It should probably not be in the responsibility of the latter to prepare alterations in the called routine, and – as the programmer cannot know what users might want from the child process – should not be changed to allow them.

I will experiment with a graphical input dialog (which may again become optional), where a user can choose to override standard behavior, if needed. After that, “sophistry” might become a better title for my thread. It is a little inconvenient that I did not need any GUI-elements until now and that the GUI-library used in the man program (fltk) is none of my closer friends. I shall instead check the system for Yad and Zenity and call one of those, if available.

Thank you, I have learned a few things – and remembered others – by this thread.

Last edited by Michael Uplawski; 01-23-2024 at 12:45 AM. Reason: input-dialog executablem destroyed thhe article and restored it.
 
  


Reply

Tags
execl(), fork(), stdin



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
Difference between Child THREAD and Child PROCESS whho Linux - Newbie 12 02-16-2015 12:22 AM
passing arguments to a child process in C++ bhanuvrat Programming 7 02-07-2010 10:01 PM
Under which circumstances a child process creates another child process using fork? mitsulas Programming 3 12-08-2009 08:16 AM
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 12:40 AM
Killing a child process from another child marri Programming 6 10-01-2004 07:08 PM

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

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