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

发布于 2025-01-06 01:52:17 字数 999 浏览 1 评论 0原文

您好,我必须为系统调用编写两个函数来管理操作系统中任务的执行。我找不到暂停/重新启动进程的方法。我找到了一个信号列表,我知道终止函数,这是我的代码:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏尔 2025-01-13 01:52:17

发送进程 SIGSTOP 来暂停它,发送 SIGCONT 来恢复它。

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

Send a process SIGSTOP to pause it and SIGCONT 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).

梦纸 2025-01-13 01:52:17

kill 不仅仅会“杀死”您为其提供 ID 的进程。它会发送您发出的信号。

kill will not just 'kill' the process whose ID you gave it. It sends the signal you give it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文