如何在C(linux)中挂起/重启进程

时间:2012-02-15 16:13:10

标签: c system process suspend

您好我必须为系统调用编写2个函数,这些函数将管理操作系统中的任务执行。我找不到暂停/重启进程的方法。我找到了一个信号列表,我知道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;
}

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

2 个答案:

答案 0 :(得分:5)

发送流程SIGSTOP暂停,SIGCONT恢复流程。

这些信号用于实现shell作业控制,因此无法捕获,阻止或忽略它们(否则应用程序可能无法控制作业)。

答案 1 :(得分:1)

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

相关问题