如何在C语言(Linux)中暂停/重启进程

5

您好,我需要编写两个系统调用函数来管理操作系统中任务的执行。但我找不到挂起/恢复进程的方法。我已经查找了信号列表并了解了kill函数,以下是我的代码:

#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;
}

备注:这段代码并不真正起作用,只是一个小的模拟。非常感谢提前的帮助。


1
为什么要使用全局变量 file1 并将其作为参数传递给 ajouter 函数?这是没有必要的,应该选择使用全局变量或者不使用,但不要混合使用。此外,建议使用 next 而不是 pids,因为这是一个链表。 - Eregrith
2个回答

10

发送进程信号SIGSTOP以暂停它,发送SIGCONT以恢复它。

这些信号用于实现shell作业控制,因此不能被捕获、阻塞或忽略(否则应用程序可能会违反作业控制)。


1

kill 不只是“杀死”你给它的进程ID,它还会发送你给它的信号。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接