如何在 C++ 中使用 kill 函数终止进程?
How to kill a process with kill function in c++?
我正在学习 Linux 中的流程。我需要编写一个操作进程的任务(创建、终止、更改精度和暂停)。我已经编写了那个程序,但是 kill 和 suspend 不起作用。为了解决这个问题,我以超级用户身份启动了该程序,但这对我没有帮助。我需要做什么才能修复该错误?
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstring>
#include <unistd.h>
#include <sys/resource.h>
using namespace std;
int main(){
float l,r;
// system("gnome-terminal -e 'sh -c \" /home/neo/Desktop/OSLab8/childprocess/a.out 1 2 3\"'");
cout<<"Enter left and right side of the loop\n";
cin>>l>>r;
int n;
cout<<"Enter number of processes\n";
cin>>n;
// cout<<"Process 0";
int currentPid;
string cmd;
double s = l;
double step = (r-l)/n;
cout<<"Father id "<<getpid()<<endl;
int mode;
cout<<"Enter 1 to time measure mode, 2 - demo mode\n";
cin>>mode;
for(int i=0;i<n;i++) // loop will run n times (n=5)
{
switch(currentPid = fork()){
case 0:
cmd = "gnome-terminal -e 'sh -c \" /home/neo/Desktop/OSLab8/childprocess/a.out "+to_string(s)+" "+to_string(s+step)+" "+((mode==1)?"":"1 ")+"\"'";
system(cmd.c_str());
return 0;
case -1:
printf("Error when forking\n");
break;
default:
break;
}
cout<<"Son process id - "<<currentPid<<endl;
s+=step;
}
if(mode==1){}
else{
int choise;
do{
cout<<"1 Set priority of processes "<<endl;
cout<<"2 Kill processes "<<endl;
cout<<"3 Suspend:"<<endl;
cout<<"4 Exit";
cout<<"Your command: ";
cin>>choise;
switch(choise)
{
case 1:
{
cout << "Enter ID of the process: ";
int id; cin >> id;
cout << "Enter the priority (from -20 up to 19): ";
int prior; cin >> prior;
setpriority(PRIO_PROCESS,id,prior);
break;
}
case 2:
{
cout << "Enter ID of the process: ";
pid_t id; cin >> id;
kill(id,9);
break;
}
case 3:
{
cout << "Enter ID of the process: ";
int id; cin >> id;
kill(id,SIGSTOP);
break;
}
case 4:
{
cout << "Enter ID of the process: ";
int id; cin >> id;
kill(id,SIGCONT);
break;
}
}
}while(choise!=4);
}
}
我删除了不相关的“东西”并得出了这个:
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstring>
#include <unistd.h>
#include <sys/resource.h>
#include <stdlib.h>
#include <string>
int main(int argc, char **argv) {
if (argc != 2) {
std::cerr << "Usage: kil <process ID\n";
return EXIT_FAILURE;
}
pid_t process = std::stoi(argv[1]);
if (kill(process, 9)) {
std::cerr << strerror(errno) << '\n';
return EXIT_FAILURE;
}
}
我做了一个快速测试,杀死了几个进程,它似乎工作正常。如果你想更多地终止进程......轻轻地,你可能想使用 kill(process, SIGINT)
代替。
我正在学习 Linux 中的流程。我需要编写一个操作进程的任务(创建、终止、更改精度和暂停)。我已经编写了那个程序,但是 kill 和 suspend 不起作用。为了解决这个问题,我以超级用户身份启动了该程序,但这对我没有帮助。我需要做什么才能修复该错误?
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstring>
#include <unistd.h>
#include <sys/resource.h>
using namespace std;
int main(){
float l,r;
// system("gnome-terminal -e 'sh -c \" /home/neo/Desktop/OSLab8/childprocess/a.out 1 2 3\"'");
cout<<"Enter left and right side of the loop\n";
cin>>l>>r;
int n;
cout<<"Enter number of processes\n";
cin>>n;
// cout<<"Process 0";
int currentPid;
string cmd;
double s = l;
double step = (r-l)/n;
cout<<"Father id "<<getpid()<<endl;
int mode;
cout<<"Enter 1 to time measure mode, 2 - demo mode\n";
cin>>mode;
for(int i=0;i<n;i++) // loop will run n times (n=5)
{
switch(currentPid = fork()){
case 0:
cmd = "gnome-terminal -e 'sh -c \" /home/neo/Desktop/OSLab8/childprocess/a.out "+to_string(s)+" "+to_string(s+step)+" "+((mode==1)?"":"1 ")+"\"'";
system(cmd.c_str());
return 0;
case -1:
printf("Error when forking\n");
break;
default:
break;
}
cout<<"Son process id - "<<currentPid<<endl;
s+=step;
}
if(mode==1){}
else{
int choise;
do{
cout<<"1 Set priority of processes "<<endl;
cout<<"2 Kill processes "<<endl;
cout<<"3 Suspend:"<<endl;
cout<<"4 Exit";
cout<<"Your command: ";
cin>>choise;
switch(choise)
{
case 1:
{
cout << "Enter ID of the process: ";
int id; cin >> id;
cout << "Enter the priority (from -20 up to 19): ";
int prior; cin >> prior;
setpriority(PRIO_PROCESS,id,prior);
break;
}
case 2:
{
cout << "Enter ID of the process: ";
pid_t id; cin >> id;
kill(id,9);
break;
}
case 3:
{
cout << "Enter ID of the process: ";
int id; cin >> id;
kill(id,SIGSTOP);
break;
}
case 4:
{
cout << "Enter ID of the process: ";
int id; cin >> id;
kill(id,SIGCONT);
break;
}
}
}while(choise!=4);
}
}
我删除了不相关的“东西”并得出了这个:
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstring>
#include <unistd.h>
#include <sys/resource.h>
#include <stdlib.h>
#include <string>
int main(int argc, char **argv) {
if (argc != 2) {
std::cerr << "Usage: kil <process ID\n";
return EXIT_FAILURE;
}
pid_t process = std::stoi(argv[1]);
if (kill(process, 9)) {
std::cerr << strerror(errno) << '\n';
return EXIT_FAILURE;
}
}
我做了一个快速测试,杀死了几个进程,它似乎工作正常。如果你想更多地终止进程......轻轻地,你可能想使用 kill(process, SIGINT)
代替。