用c++写一个杀死指定进程的代码
时间: 2023-03-12 15:50:57 浏览: 79
#include <stdio.h>
int main()
{
// Pid of the process to kill
int pid;
printf("输入要杀死的进程ID: ");
scanf("%d", &pid);
// Kill the process
kill(pid, 9);
return 0;
}
相关问题
用c++写一个获取指定的进程并杀死的代码
答:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>int main(int argc, char *argv[])
{
if (argc > 1) {
char *proc_name = argv[1];
pid_t pid = 0; // 获取指定进程的 PID
FILE *fp = popen(strcat("pidof ", proc_name), "r");
fscanf(fp, "%d", &pid); if (pid != 0)
{
// 向指定进程发送 SIGKILL 信号
kill(pid, SIGKILL);
printf("成功杀死进程 %s, PID 为 %d\n", proc_name, pid);
}
else
{
printf("没有找到进程 %s\n", proc_name);
} pclose(fp);
} return 0;
}
用C++写一个计算电能量代码
下面是一个简单的计算电能量的C++代码:
```c++
#include <iostream>
using namespace std;
int main()
{
float voltage, current, time;
cout << "请输入电压值(伏特):";
cin >> voltage;
cout << "请输入电流值(安培):";
cin >> current;
cout << "请输入电器使用时间(小时):";
cin >> time;
float energy = voltage * current * time;
cout << "电能量为:" << energy << " 焦耳" << endl;
return 0;
}
```
这段代码首先要求用户输入电压、电流和使用时间,然后使用公式 `电能量 = 电压 x 电流 x 时间` 计算电能量,并将结果输出到屏幕上。注意,这里的电能量单位是焦耳(J),也可以使用其他单位,比如千瓦时(kWh)。