Torin
Torin

Reputation: 1

Using pid passed from argv

Currently, I have one file calling another, and as an argument it passes over a pid converted into a string. Then, in a signal handler, I need to send a signal to the process given by the pid.

So I have:

//initialized as a global variable
pid_t* distantPid;

//in my main function
distantPid = &argv[1];

//then, in my signal handler
kill(*distantPid, WARNING_SIGNAL);

Whenever I try to compile this, though, I get an error saying 'Assignment from incompatible pointer type [assigned by default]'

I can't simply use argv[1] in the signal handler, because it's not in the main function.

Where am I going wrong?

Upvotes: 0

Views: 1114

Answers (2)

Paresh Dhandhukiya
Paresh Dhandhukiya

Reputation: 212

convert string to int and pass it to kill command : kill(atoi(argv[1]), WARNING_SIGNAL);

Upvotes: 0

nemequ
nemequ

Reputation: 17492

You need to convert from a string to an integer. Try something like

//initialized as a global variable
pid_t distant_pid;

//in my main function
long long int pid_ll = strtoll(argv[1], NULL, 0);
distant_pid = (pid_t) pid_ll;

//then, in my signal handler
kill(distantPid, WARNING_SIGNAL);

If this is production code you'll want to make sure strtoll() was successful; I'll leave that as an exercise for you… see the man page for info about how.

Upvotes: 2

Related Questions