how to suspend/restart processes in C (linux)
Asked Answered
M

2

5

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

Milburt answered 15/2, 2012 at 16:13 Comment(1)
Why do you use a global file1 and give it as a parameter to ajouter ? this is useless, chose between global or not but do not mix. Also, prefer next instead of pids as this is a chained listSupplant
S
1

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

Supplant answered 15/2, 2012 at 16:23 Comment(0)
I
10

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).

Isidore answered 15/2, 2012 at 16:18 Comment(0)
S
1

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

Supplant answered 15/2, 2012 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.