Programming :: Passing Arguments To A Child Process In C++?
Feb 7, 2010
what I am trying to do is to pass an argument to the standard input stream of a child process. I mean I create two programs .. first one invokes the other. second one contains something like
Code:
scanf("%d", &n);
now I want my first program to be able to pass a value to the second one so that it gets stored in n.
View 7 Replies
ADVERTISEMENT
Dec 7, 2009
Consider the following code:
Code:
int main()
{
int i=0;
pid_t pid;
for(i=0;i<2;i++)
[code]....
I get the following output:
Parent: chid_pid=4356 i=0 parent's pid=4355
This is child 4356 i=0
This is child 4357 i=1
[code]....
I can observe instead of two children(as I expect) processes there are three. This is because child process 4356 creates its own child. Why all the messages of the type "This is child X i=Y" are concentrated one under another? How exactly fork works? Is affected by the fact that I have a dual-core processor?
View 3 Replies
View Related
May 23, 2011
I have a doubt about signals in C programming. I have done this little program to explain it. It creates a child process with fork and, when the child ends, receives the SIGCHLD signal and wait for its termination.Ok, quite easy, BUT when I execute this code the SIGCHLD signal is received twice, first as an error (returns -1) and the second one to finish the child process.I don't understand the meaning of the first received signal. Why is it generated? Is the code wrong? (if you add the SIGINT and press Ctrl+C during the execution it also receives two signals instead of one)
Code: #include <stdio.h>
#include <unistd.h>
#include <string.h>
[code]....
View 4 Replies
View Related
Dec 17, 2009
I am calling another executable in my application (C programing) using "system" command
which is user interactive program. now i want to pass those args in system command only.
system(" executable ");
Executable will expect 1,2 or 3.
1 is to continue
2 for do changes in settings
3 exit from application
how to pass these in to system command
View 1 Replies
View Related
Mar 22, 2009
I want to write a little time-saving alias for my .cshrc file that will move files and then cd to the directory I've moved them to. What I can't quite figure out is the syntax to say 'move all the arguments except the last one.' Here is what I have:
alias follow 'mv !:1-$-1 !:$; cd !:$'
This actually seems to work, but it also gives me an irritating error:
mv: cannot stat `destinationdirectory/-1': No such file or directory
Similarly, I tried:
alias follow 'mv !:*$#argv-1 !:$; cd !:$'
Again the move and cd are successful, but again there is an error:
mv: cannot stat `destinationdirectory/0-1': No such file or directory
View 1 Replies
View Related
Apr 24, 2010
Is it possible to pass arguments to a source file in a bash script? For example
#!/bin/sh
#
. /dir1/dir1/funclib -a -b
How would you check for the passed arguments in funclib without getting confused with any arguments passed to the main script?
View 5 Replies
View Related
May 18, 2009
I need to write a wrapper function around the mvprintw function, like so:
int smvprintw( ? )
{
// Do various checks/modifications on the first two arguments (int y,
int x) first,
// and then
return mvprintw ( ? );
}
How should I write the args for smvprintw so that I can pass all the data correctly to mvprintw, and also do my checks on the first two args (for example, to modify them)? I'm confused by the prototype of mvprintw: This is mvprintw as listed in the man pages:
int mvprintw(int y, int x, const char *fmt, ...);
And this is how I saw it in ncurses.h:
extern NCURSES_EXPORT(int) mvprintw (int,int, const char *,...)
I believe the "..." refers to the unknown number of items after const char * fmt. Will it suffice to do something like "int smvprintw(int y, int x, const char * fmt, void * etc)" and then "mvprintw(y, x, fmt, etc)" inside the function?
View 2 Replies
View Related
Aug 7, 2010
I've started dabbling with the case statement in order to pass some option's arguments into variables. I do not think I am doing this right.
Code:
usage() {
echo "Usage: $0 [-z|--snooze] [-c|--channel] [-p|--playlist]
[-m|--message] [-v|--mpcvolume]"
[code]....
As you can see, I want to pass arguments depending on the option(s) chosen by the user; ie. --snooze, or --channel. By default, if no options are chosen, I'll display a usage message; though in the future I'll provide some sane defaults. I'd like to create a case statement to handle passing arguments to any number of options; something like:
Code:
wakethehellup.sh --snooze 20 --message 'wake up!'
and for the other arguments, it would have a default set. The case statement I provided fails with a syntax error "syntax error near unexpected token `$2'" near the '--snooze' in the statement, so I take it you can't pass a parameter in this way; but I'm confused as to how I'm supposed to pass different parameters to different options without the options being confused as parameters.
View 2 Replies
View Related
Jul 13, 2009
how to use QGLviewer. I want to give my program a file name as a command line argument. All of the sample programs I find have a main.cpp file like this:
Quote:
#include <QApplication>
#include "window.h"
int main(int argc, char *argv[])
[code]....
Then the Window class, which is derived from QGLViewer, does all the program's actual work. If I want access to argc and argv, for example, to open and read a file that's passed as an argument, what would handle that? Is there a built-in way to get the arg variables to the window class, or do I need to just write a loadfile function and pass them?
View 1 Replies
View Related
Aug 18, 2010
I have a root process (on linux) that forks a child and the child process then drops privileges by doing a setuid() to a normal user. After the child setuid()'s, it is of course impossible for it to gain root again by itself. But since the main process is still running as root, i was wondering if there was a simple/smart way of getting the root-master-process to elevate the child back to root (or maybe just to another non-privi uid). Is there some way to do a setuid() on another pid? or maybe something can be done through /proc/<pid>/? Killing the child is not an option (because its what it does today and im trying to find a smarter way). (The program is apache2's mpm-itk worker and the "child" is the actual apache2 process serving a page.)
View 11 Replies
View Related
Jul 14, 2011
Im using gdb for debugging my application.. I was able to debug child process(when fork comes) .. and in child process we have an exec call to .... So the problem is, when the control come to exec , the exec process is executing at a time... I could not debug the exec. process... error is stack curruption due to same frame So, is there any way to debug the exec process
View 1 Replies
View Related
Mar 28, 2011
I'm writing a sort of toy shell using fork() and execv(). It runs smoothly enough untill the user enters an invalid command at which point the program hangs, so I need a way to check if the program loaded using execv returned correclty or not in the parent process and kill it if it didntI tried writing stderr to a text file to see if something whent wrong but doesnt really work out. Try running ./digenv GDM -i -Q for instance. (-Q is an invalid option for grep which the program runs at a certain point).Heres the code:
Code:
//digenv
//Carl Reg�rdh 2011-03-24
[code]....
View 13 Replies
View Related
Aug 18, 2010
I'm trying to write a shell script that do ftp and download file periodically, this script should be called by a daemon running in the background.
the shell script "script.sh" is as follows:
Code:
yafc ftp://test:test@192.168.1.225:21 < commands
and the "commands" files is
Code:
d Root/md5* /
quit
if I run script.sh it will work just fine. But when the daemon software calls the "script.sh", the script will send ftp login request to the ftp server, but will not even answer the username or anything.
I believe it is something about child process redirection, but I don't know how to deal with it.
This problem is not only with yafc, it is the same with any ftp client or any application like telnet and so.
View 4 Replies
View Related
Mar 10, 2011
I want to kill parent process after "fork()" method. but if I kill parent process with "exit(0)" method, main() thread is terminated as well so child prosess doesn't work anymore. Is there any way to kill only parent process without affecting to child process?
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
[code]....
View 1 Replies
View Related
May 12, 2011
i got basic knowledge about creating a single child from a parent using fork(). But when it comes into creating multiple children, i am simply stuck. I am trying to create two processes from a parent and it would wait for both two processes to finish. my attempt is as below
[Code]....
View 3 Replies
View Related
Aug 2, 2010
I am developing a application. In this I fork() 3 childs(lets say child1 , child2, child3) . The parent is now waiting for some input from keyboard.Child3 is continously getting data from child1 and child2 using pipe which it then will print using printf.Now as the parent is waiting for input from user through keyboard while child3 is continously printing the data. I want to do it in different terminals.Can you please guide me how to proceed ahead so that on one terminal , the parent waits for input fromser while on other terminal child3 prints data.
View 1 Replies
View Related
Mar 22, 2011
I am going to create a parent process and fork a child process from it. I want to write a code in such a way that whenever my child process end it must indicate that the child process is terminated by a signal or not. This code must be written in the parent process block.
View 6 Replies
View Related
Jul 30, 2009
I am troubleshooting something and I got this problem.
If I do "pstree -p"
It shows,
Code:
[code]....
However, it does NOT show up in "ps -elf"
Code:
ps -elf | grep soffi
0 S whho 7734 1 0 80 0 - 36435 - 11:14 pts/2 00:00:03 /usr/lib/openoffice/program/soffice.bin -splash-pipe=5
0 S whho 7833 7759 0 80 0 - 751 - 11:21 pts/3 00:00:00 grep soffi
I was wondering if 7735, 7736, 7737, 7743 were really processes. Then I checked /proc, I could cd to /proc/7735, /proc/7736, etc, but I could not ls them out. I looked at the man page of "pstree", it says,
Code:
Child threads of a process are found under the parent process and are shown with the process name in curly braces, e.g.
icecast2---13*[{icecast2}]
So, what does all this mean? Does it mean that 7735, 7736, 7737, 7743 are just threads but not processes? If so, why could I cd to /proc/<id> but not see them in "ps -elf".
View 10 Replies
View Related
Mar 8, 2011
I need to disable file access (fopen, freopen, open etc) for application which is running under chroot jail and with restrictions (rlimit) via execv. Before that I redirected stdin/out to files within jail. I tried this:
Code:
// Redirect stdin/stdout to files
int fd = open (file_input, O_RDONLY);
if (fd < 0)
fatal_error ("input open failed!");
[Code].....
View 6 Replies
View Related
Feb 24, 2010
Code:
int main()
{
int pi;
int i=10;
pid=fork();
[code]....
Q 1. The value of the variable pid returned by the fork() function will be greater than 0 in the parent process and equal to zero in the child process? but during forking, there values are exactly copied so what's went wrong here?
Q 2. "changes to the variable in one process is not reflected in the other process" why it is so? >> Even if we have variable i declared as a pointer or a global it wont make a difference.
Code:
int main()
{
int i, pid;
i=10;
printf("before fork i is %d
[code]....
Q. Through this program it is clear that both process is using the data from the same location, so where's the original value is residing when the child process is in execution.?
View 6 Replies
View Related
Sep 15, 2010
What i am trying to do is i want to add numbers from 1 to 100. but that too using multiprocessing. So i made a c programme and using fork() command made two child processes. Now in one child process i am adding from 1 to 50. and in another i am adding 51 to 100. and then in the parent process adding the two results to get the final one. Now the result from the two function i am getting correctly. But after the wait() call the value returned is lost : See the programme below for reference
# include<stdio.h>
# include<unistd.h>
# include<sys/wait.h>
# include<stdlib.h>
[code].....
View 6 Replies
View Related
Mar 13, 2011
I have some source code of an application called gmapping.
I am pasting the source code documentation below:
SCANMATCHER:
How do I run this file. When I enter the filename parameter, and press enter, I get
COMMAND LINE: parameter aces not recognized no filename specified
GridFastSlam: Initialization Error!
(Did you specified an input file for reading?)
What is the right line to type in the terminal to sun this code?
View 1 Replies
View Related
Nov 18, 2009
I'm using gdb to debug my program. My program requires arguments (e.g., ./prog -dfile).But if I use gdb as in gdb ./prog -dfile, gdb wants to interpret the -d argument. How do I pass an argument to my program via gdb?
View 2 Replies
View Related
Feb 28, 2010
It seems incrontab wont see spaces properly at all. I setup a script to echo the arguments passed to it by incrontab to a file, and no matter what I put around the arguments on the incrontab file it will count a space as the next argument.
I have written a script to automatically retrieve imdb artwork for a given filename. Here is the script:
Code:
You can ignore all the commented "echo" commands that was just me testing. Anyway, the script work fine, however I am trying to use incrontab to monitor a folder, when a new (video) file is moved into the folder, it should execute the script and retrieve the artwork. My problem is, when incrontab passes the $# argument to my script, the script wont work because the spaces aren't escaped.
Here is some more detail:
Incrontab
Code:
Code:
The problem is, the script GetArtwork, doesn't see "Bangcock Dangerous" it just sees "Bangcock"
I have tried putting quotes around the $# in the incrontab - this just makes the script see "Bangcock (notice the single quote character)
View 2 Replies
View Related
Apr 2, 2011
how to pass an array as a command line argument in a shell script?
View 5 Replies
View Related
Dec 11, 2010
The script receives multiple files as parameters and it is supposed to count the number of lines in each of them and write that number in another file.
This is my script:
Code:
while [ -n "$1" ]
do
lines=`cat $1 | wc -l`
echo "The number of lines in file $1 is $lines." >> lines.txt
shift
done
Is there any other way to do the same thing, without using shift?
View 7 Replies
View Related
Apr 26, 2011
The code:
Quote:
Problem: I need a method to maintain the $i variable. In fact, actually, this variable get lost when executed. I think that an escape can preserve this variable and permit its execution inside the function, but I've no idea about.
View 3 Replies
View Related
May 31, 2010
I wrote a simple bash script to let me treat any set of programs like a deamon. For example if I configure the script a certain way I can start/stop/get the status of apache, mysql and php all from one command. I am having a bit of a problem though. I am passing commands as strings to a function and then depending on the arguments to the script it might run one of these commands or another. Some of these commands need to beun in the background though, such as deluge-web. When I send "deluge-web &" to the function and it execute it deluge-web does not start in the background. I can't figure out why this is. I have tried escaping the & with ''s and with a , but nothing seems to work. I know that this is some idiotic thing that I am overlooking, but I am a bit stumped. Here is the script configured to start/stop/get status of deluged and deluge-web.
Code:
#!/bin/bash
function checkanddosomething {
[code]...
View 3 Replies
View Related
Sep 8, 2010
Code:
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
[code]....
Description of what the code does or what i intended to do:
1. Created a child process from parent process using 'fork()'
2. Sent a signal 'SIGALRM' from child process to parent process using 'sigqueue' function.
(The Third parameter of 'siqueue' function contains the message (message msg) which the child process wants to send to the parent process.'msg' is a stucture instance containing a) pid of child and b) string) 5. Print the 'msg' sent by child process inside the signal handler function 'sig_action_function' of the parent process I am getting some junk value when this line is executed
Code:
printf("%d
",msg->cpid);
I expected to get the pid of child process, which the child process sent to parent process through the signal.
View 3 Replies
View Related
Sep 3, 2010
I'm at the bottom of the bash learning curve, looking up, hoping someone can toss me a line. I need to update tracker on my system but this will erase the metatag database I've been building up over the course of months for the purpose of indexing a news archive. So the solution seems to be, 1) save the output of tracker-tag to a text file for all relevant files within a directory, 2) upgrade tracker (since the version in the Ubuntu repositories is very much out of date) and then 3) use a script to parse the text file and pass appropriate arguments back to tracker-tag to rebuild the database. It sounds as though it ought to be simple enough, but I need a push in the right direction, which hopefully will not be off the cliff. Before I confuse my metaphors any further, here's what the text file looks like.
[Code]....
View 3 Replies
View Related