Programming: Process and Thread

Process Control

Unix provides a number of system calls for manipulating processes from C programs. This section describes the important functions and gives examples of how they are used.

Obtaining Process IDs

getpid and getppid

#include <sys/types.h>
#include <unistd.h>

pid_t getpid(void);
pid_t getppid(void);

// Returns: PID of either the caller or the parent

Creating and Terminating Processes

From the perspective of programmers, we care the following states:

  • Running

  • Blocked: The execution of this process is suspended and will not be scheduled. A process stops as a result of receiving a SIGSTOP, SIGTSP, SIGTTIN or SIGTTOU signal, and remains stopped until it receives a SIGCONT signal, at which point it becomes running again. It can be further categorized as uninterruptible sleep and interruptible sleep.

  • Terminated: A process become terminated for: Receving a signal whose default action is to terminate the process; Returning from the main routine; Calling the exit function.

Reaping Child Processes

The child process terminated → It becomes a zombie → reaped by its parent → Cease to exist

The init process is the adopted parent of any orphaned children. It has a PID of 1. It is created by the kernel in the system start-up, never terminates. If a parent terminates without reaping its children, the init (``systemdorUpstart` in Linux)process is to reap them.

Even though zombies are not running, they still occupy system memory resources. A process waits for its children to terminate or stop by calling the waitpid function.

By default (options = 0) waitpid temporarily suspends execution of the calling process until a child process in its wait set terminates. After the function returns, the termianted child has been reaped and the kernel removes all traces of it from system.

pid Value:

pid Value

Description

pid > 0

pid = -1

Wait set includes all parent's children.

Options:

Option
Description

WNOHANG

Return immediately if no child in wait set terminated.

WUNTRACED

Wait until child is terminated or stopped. Return the PID of that child.

WCONTINUED

Wait until a running child is terminated or a stopped child resumes (via SIGCONT).

WNOHANG|WUNTRACED

Return immediately unless a child has stopped or terminated.

Exit Status (*statusp):

Function
Description

WIFEXITED(status)

True if child terminated normally.

WEXITSTATUS(status)

Exit status if WIFEXITED is true.

WIFSIGNALED(status)

True if child terminated from an uncaught signal.

WTERMSIG(status)

Signal number causing termination if WIFSIGNALED is true.

WIFSTOPPED(status)

True if the returning child is currently stopped.

WSTOPSIG(status)

Signal number causing stop if WIFSTOPPED is true.

WIFCONTINUED(status)

True if child was restarted by SIGCONT.

A simpler version of waitpid

The order to reap the child processes is nondeterministic behavior that can make reasoning about concurrency so diffcult.

Putting Processes to Sleep

The sleep function suspends a process for a specified period of time.

Returns after the process has been woken up. Returns zero if the requested amount of time has elapsed, and the number of seconds still left to sleep otherwise. The function returns the number of seconds left unslept if the sleep is interrupted by a signal handler.

Loading and Running Programs

The execve function loads and runs the executable object file filename with the argument list argv and the environment variable envp. execve returns to the calling program only there is an error, such as not being able to find filename. It is called once and never returns.

Screenshot 2023-12-16 at 9.57.13 PM

POSIX Thread APIs

  • thread: A pointer to a pthread_t variable that will be filled in with a unique thread ID for the new thread.

  • attr: A pointer to a pthread_attr_t structure that specifies attributes for the new thread.

  • start_routine: A pointer to the function that the new thread will execute.

  • arg: An argument that will be passed to the start_routine function when it is called by the new thread.

Give up the processor to let other threads run.

  • thread: The thread ID of the thread to join.

  • retval: A pointer to a variable that will be filled in with the exit status of the joined thread.

When pthread_join() is called, the calling thread blocks until the specified thread terminates. Once the thread has terminated, pthread_join() returns and the exit status of the thread is stored in the location pointed to by retval.

When pthread_exit() is called, the calling thread is terminated and its resources are freed. The exit status of the thread is returned in the retval parameter.

  • mutex: A pointer to the mutex to initialize.

  • attr: A pointer to a pthread_mutexattr_t structure that specifies attributes for the mutex. If NULL, default attributes are used.

Initializes a mutex object, which may be used to control access to a resource.

  • mutex: A pointer to the mutex to lock.

This function locks the specified mutex. If the mutex is already locked by another thread, the calling thread blocks until the mutex becomes available.

  • mutex: A pointer to the mutex to unlock.

Unlocks the specified mutex, allowing other threads to acquire the mutex if they are blocked waiting for it.

  • cond: A pointer to the condition variable to wait on.

  • mutex: A pointer to the mutex that must be locked by the calling thread.

Blocks the calling thread on the condition variable specified by cond. The thread remains blocked until another thread signals the condition variable.

  • cond: A pointer to the condition variable to signal.

Wakes up at least one thread that is currently blocked on the specified condition variable.

  • attr: A pointer to the thread attributes object.

Initializes a thread attributes object with default values.

  • attr: A pointer to the thread attributes object.

  • detachstate: The detach state to set, which can be either PTHREAD_CREATE_DETACHED or PTHREAD_CREATE_JOINABLE.

Sets the detach state of a thread, determining whether the thread is joinable or detached upon creation.

Last updated