如何在 C (linux) 中挂起/重新启动进程
您好,我必须为系统调用编写两个函数来管理操作系统中任务的执行。我找不到暂停/重新启动进程的方法。我找到了一个信号列表,我知道终止函数,这是我的代码:
#include <stdlib.h>
#include <signal.h>
typedef struct File
{
int pid;
struct File *pids;
} *file;
file file1 = NULL;
//this fonction is to suspend a process using its pid number
int V(int pid_num)
{
//does the kill fonction just kill the process or will it take into account the signal argument?
kill(pid_num, SIGSTOP);
file1 = ajouter(file1, pid_num);
return 0;
}
//this is to restart the process
int C()
{
if (file1 != NULL)
{
//I know the kill fonction is not the right one but I just don't know any other to take as argument the pid of a process to restart it
kill(file1->pid, SIGCONT);
}
return 0;
}
//this fontion adds pid number to our structure
file ajouter(file file_n, int pid)
{
file nouveau = malloc(sizeof(file));
nouveau->pid = pid;
nouveau->pids = file_n;
return nouveau;
}
备注:这段代码不应该真正起作用,它只是一个小模拟 预先非常感谢
Hi I have to write 2 fonctions for system calls that will manage the execution of task in an operating system. I couldn't find a way to suspend/restart processes. I have found a list of signals and I know the kill function and here is my code:
#include <stdlib.h>
#include <signal.h>
typedef struct File
{
int pid;
struct File *pids;
} *file;
file file1 = NULL;
//this fonction is to suspend a process using its pid number
int V(int pid_num)
{
//does the kill fonction just kill the process or will it take into account the signal argument?
kill(pid_num, SIGSTOP);
file1 = ajouter(file1, pid_num);
return 0;
}
//this is to restart the process
int C()
{
if (file1 != NULL)
{
//I know the kill fonction is not the right one but I just don't know any other to take as argument the pid of a process to restart it
kill(file1->pid, SIGCONT);
}
return 0;
}
//this fontion adds pid number to our structure
file ajouter(file file_n, int pid)
{
file nouveau = malloc(sizeof(file));
nouveau->pid = pid;
nouveau->pids = file_n;
return nouveau;
}
remark: this code is not supposed to really work it's just a little simulation
thanks a lot in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发送进程
SIGSTOP
来暂停它,发送SIGCONT
来恢复它。这些信号用于实现 shell 作业控制,因此它们无法被捕获、阻止或忽略(否则应用程序可能会违反作业控制)。
Send a process
SIGSTOP
to pause it andSIGCONT
to resume it.These signals are used to implement shell job control and because of that they can't be caught, blocked or ignored (otherwise an application could defy job control).
kill
不仅仅会“杀死”您为其提供 ID 的进程。它会发送您发出的信号。kill
will not just 'kill' the process whose ID you gave it. It sends the signal you give it.