C++/Solaris: retrieving args from an execv() call - and I am an idiotwith pointers

M

M Herriott

Hello to all -

#1: I would have rather used an AND with the choice of newsgroups. I
would like to talk to those who subscribe to comp.lang.c++ AND
comp.unix.solaris - but I'm stuck with OR. Please accept my sincerest
apologies for what I am about to do, I know it is, in the purest sense,
incorrect.

Having said that much, let me describe my problem. I am running two
processes on a Solaris server. Process 1 forks and calls process 2 using
execv(). All well and good. The problem comes in when I try and pull the
passed args from the pointer array - I get nonsense where once there
was sense.

Now, I am full willing to admit I'm not sure what exactly I am doing
here. I believe my pointers have gotten fouled up, and I've been staring
at them for so long that they have ceased to mean anything. Please advise:

// Process 1

#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <iostream.h>

void main()
{
int forkA, proc_id00, sleep_err, kill_err;
int time = 3;
int kill_sig = 9;

char *argv[3];

argv[0] = "one";
argv[1] = "two";
argv[2] = NULL;

forkA = fork ();

if (forkA == 0)
{
int error = 0;
int halftime = 5;
error = execv ("testit2", argv);
sleep_err = sleep(halftime);
exit (101);
}

else
{
sleep_err = sleep(time);
kill_err = kill (forkA, kill_sig);
exit (100);
}
}

// Process 2

#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <iostream.h>

void main(int arg_count, char *arg_val)
{
char **argv = &arg_val;
int i = 0;

cout << "Program 2: ";

while (i < arg_count)
{
cout << argv[i++];
}

cout << endl;

exit(103);
}


My output looks like this: Program 2: ÿ¾ñÿ¾ñ(null)
 
R

Rich Teer

// Process 1

#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <iostream.h>

void main()

This is your first mistake: main() is NEVER void. It should
ALWAYS be defined as returning an int.
{
int forkA, proc_id00, sleep_err, kill_err;

The value returned by fork() has a type of pid_t, not int.
int time = 3;
int kill_sig = 9;

char *argv[3];

argv[0] = "one";
argv[1] = "two";
argv[2] = NULL;

forkA = fork ();

if (forkA == 0)
{
int error = 0;
int halftime = 5;
error = execv ("testit2", argv);

Is testit2 in your PATH? If so, that means that "." is in it,
which is a security no-no.
sleep_err = sleep(halftime);
exit (101);

If execv() is successful, those last two lines will never get
executed. Also, _exit () should be used here, and why are you
sleepiing?

WHat about if fork() fails, returning -1?
{
sleep_err = sleep(time);
kill_err = kill (forkA, kill_sig);
exit (100);

Why are you sleeping and sending a kill signal to the child?
Calling wait or waitpid is sufficient.
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <iostream.h>

void main(int arg_count, char *arg_val)

There's that "void main" again - and is there any reason why
you're not using the conventoinal variable names of argc and
argv here? Whatever, "arg_val" should be a "char **", not a
"char *".
{
char **argv = &arg_val;
int i = 0;

cout << "Program 2: ";

while (i < arg_count)
{
cout << argv[i++];
}

Might I suggest using a for loop instead of a while? E.g.,

for (i = 0; i < arg_count; i++)
cout << argv;

In fact, I'd writ the whole thing lijke this:

int main (int argc, char **argv)
{
int i;

cout << "Program 2: ";

for (i = 0; i < argc; i++)
cout << argv;

cout << endl;

return (103);
}

(Using return instead of exit here keeps the compiler happy.)

HTH,

--
Rich Teer, SCNA, SCSA

President,
Rite Online Inc.

Voice: +1 (250) 979-1638
URL: http://www.rite-online.net
 
A

Artie Gold

M said:
Hello to all -

#1: I would have rather used an AND with the choice of newsgroups. I
would like to talk to those who subscribe to comp.lang.c++ AND
comp.unix.solaris - but I'm stuck with OR. Please accept my sincerest
apologies for what I am about to do, I know it is, in the purest sense,
incorrect.

There are many problems with your code, but I'll only point out the
specific one that's causing you this particular problem.

[snip]
void main(int arg_count, char *arg_val)
int main(int arg_count, char **arg_val)
{
// char **argv = &arg_val;
int i = 0;

cout << "Program 2: ";

while (i < arg_count)
{
cout << arg_val[i++];
}

cout << endl;

exit(103);
}


My output looks like this: Program 2: ÿ¾ñÿ¾ñ(null)

HTH,
--ag
 
M

M Herriott

Rich, Artie -

Your help has been exactly what I have needed. Thank you so much for
your efforts. The code works now - or at least what parts of the code
you corrected. I will try and incorporate the rest of your suggestions
throughout this program.

Thank you again for your comments,

- Mark Herriott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top