I am creating my own shell (like bash) and I have a function that calls the execv function and passes in a specific process pid to kill. I basically run a sleep command for 10 seconds, and halfway through I want to kill it. But my kill command does not seem to be working. Does anyone have any ideas how I can fix it?
Below are functions related to execv
void run_execv(char *path, char *args[])
{
int result = execv(path, args);
}
void kill_process(char *target_pid)
{
char *bin_path = "/bin/kill";
char *args[] = {bin_path, "-15", target_pid, NULL};
run_execv(bin_path, args);
}
void ps()
{
char *bin_path = "/bin/ps";
char *args[] = {bin_path, NULL};
run_execv(bin_path, args);
}
And the calling of the kill_process function is shown below. Basically I am calling the kill command as a child process.
else if (strncmp(shellInput, "pkill", strlen("pkill")) == 0 || strncmp(shellInput, "kill", strlen("kill")) == 0)
{
char *target_pid = strtok(NULL, " \n");
int childStatus;
pid_t spawnPid = fork();
switch (spawnPid)
{
case -1:
perror("fork()\n");
exit(1);
break;
case 0:
// This is the child process where we will call the ls function
kill_process(target_pid);
perror("execv");
exit(2);
break;
default:
// This is the parent process that takes control back after child process finishes.
spawnPid = waitpid(spawnPid, &childStatus, 0);
printf("CHILD STATUS: %d\n", childStatus);
processStatus = childStatus;
break;
}
Even after killing that sleep process, it still shows up when I check my current processes running with ps. Please see my screenshot below for the same execution.
~/Desktop/OSU/CS-344 (Operating Systems)/assignment3(main*) » ./a.out sampai@sams-mbp-2
: ps
PID TTY TIME CMD
25223 ttys003 0:06.24 /bin/zsh -l
41134 ttys003 0:00.00 ./a.out
32018 ttys004 0:01.49 -zsh
30707 ttys005 0:00.14 /bin/zsh --login -i
: sleep 30 &
background pid is 41143
: ps
PID TTY TIME CMD
25223 ttys003 0:06.24 /bin/zsh -l
41134 ttys003 0:00.00 ./a.out
41143 ttys003 0:00.00 sleep 30
32018 ttys004 0:01.49 -zsh
30707 ttys005 0:00.14 /bin/zsh --login -i
: kill 41143
CHILD STATUS: 0
: ps
PID TTY TIME CMD
25223 ttys003 0:06.24 /bin/zsh -l
41134 ttys003 0:00.00 ./a.out
41143 ttys003 0:00.00 (sleep)
32018 ttys004 0:01.49 -zsh
30707 ttys005 0:00.14 /bin/zsh --login -i
:
CodePudding user response:
Figured it out! Basically like kaylum said in the comments, I needed to add a waitpid call outside of my switch statement. And I needed to wait on the correct pid, which I needed to get inside of the child process. Please see my updated code below.
int process_pid;
pid_t spawnPid = fork();
switch (spawnPid)
{
case -1:
perror("fork()\n");
exit(1);
break;
case 0:
// This is the child process where we will call the ls function
process_pid = getpid();
kill_process(target_pid);
perror("execv");
exit(2);
break;
default:
// This is the parent process that takes control back after child process finishes.
spawnPid = waitpid(process_pid, &childStatus, 0);
processStatus = childStatus;
break;
}
spawnPid = waitpid(process_pid, &childStatus, 0); // Need this to kill zombie process.
printf("CHILD STATUS: %d\n", childStatus);
CodePudding user response:
try kill -9 <PID>
it must work