myshell 2.0.0
Loading...
Searching...
No Matches
msh_jobs.cpp File Reference

Job control related utilities. More...

Functions

void init_job_control ()
 Initialize job control.
 
void sigchld_handler (int)
 SIGCHLD handler.
 
int wait_for_process (pid_t pid, int *status)
 Wait for the process to finish.
 
int reap_children ()
 Wait for all background processes to finish.
 
int no_background_processes ()
 Return the number of currently running background processes.
 
void print_processes ()
 Print all internal processes.
 
void remove_completed_processes ()
 Remove all completed processes, i.e. whose status is DONE from the internal process table.
 
void print_completed_processes ()
 Print all completed background processes.
 
void update_jobs ()
 Update the internal process table.
 
void add_process (pid_t pid, int flags, const std::vector< std::string > &args)
 Add a process to the internal process table.
 
void remove_process (pid_t pid)
 Remove a process from the internal process table.
 
void set_process_status (pid_t pid, status_t status)
 Change the status of a process in the internal process table.
 

Variables

std::map< pid_t, processprocesses
 The internal process table.
 

Detailed Description

Job control related utilities.

Simplest job control implementation. Just for demonstration purposes. Features only process tracking and printing.

MAYBE: In future implement full job control supporting foreground and background processes with respecting process groups and signals handling process pipelines.

Function Documentation

◆ add_process()

void add_process ( pid_t pid,
int flags,
const std::vector< std::string > & args )

Add a process to the internal process table.

Parameters
pidThe process ID.
flagsThe process flags.
argsThe process arguments.
See also
process()

◆ init_job_control()

void init_job_control ( )

Initialize job control.

Set SIGCHLD handler.

See also
sigchld_handler()

◆ no_background_processes()

int no_background_processes ( )

Return the number of currently running background processes.

Returns
The number of currently running background processes.

◆ print_completed_processes()

void print_completed_processes ( )

Print all completed background processes.

Note
Only prints processes that were started asynchronously.

◆ reap_children()

int reap_children ( )

Wait for all background processes to finish.

Returns
The number of reaped processes.
Note
All processes reaped by this function are immediately removed from the internal process table.
See also
remove_process()

◆ remove_process()

void remove_process ( pid_t pid)

Remove a process from the internal process table.

Parameters
pidThe process ID.

◆ set_process_status()

void set_process_status ( pid_t pid,
status_t status )

Change the status of a process in the internal process table.

Parameters
pidThe process ID.
statusThe new process status.

◆ sigchld_handler()

void sigchld_handler ( int )

SIGCHLD handler.

Reaps the process and updates its internal status.

See also
waitpid()
set_process_status()
Parameters
signoThe signal number.

◆ update_jobs()

void update_jobs ( )

Update the internal process table.

Is exactly equivalent to calling print_completed_processes() and remove_completed_processes() sequentially.

Note
This function should be called before every command execution.
See also
print_completed_processes()
remove_completed_processes()

◆ wait_for_process()

int wait_for_process ( pid_t pid,
int * status )

Wait for the process to finish.

Parameters
pidThe process ID.
statusThe process status.
Returns
The process exit status.
Note
Call to this function leads to explicit removal of the process with matching PID from the internal process table.
If both WIFEXITED and WIFSIGNALED are false for the process, the return value is undefined.
See also
remove_process()

Variable Documentation

◆ processes

std::map<pid_t, process> processes

The internal process table.

Maps process IDs to process objects.