progsys(1) — process and fork

Process


Process = program in execution

  • One or more threads.
  • Can communicate through pipes, signals, networks, files.

Process descriptor

  • Each process has its Process Descriptor, which is used to keep track of the process in memory
  • Stores PID, state, parent process, children, registers, address space information, open files
  • Process information is usually stored in the so called Process Table, i.e. an array of PD

Three states model

  • Running : execution, process is using the CPU.
  • Ready : process can be executed but is temporarily waiting, CPU is used by another process.
  • Waiting : process can’t be executed; it is either blocked because waiting for an external event (e.g. I/O, interrupt from another process) or because scheduler assigned CPU to others.

Code in C

1
2
3
4
5
6
#include <sys/types.h>
gid_t getgid (void);
pid_t getpid (void);
pid_t getppid (void);
uid_t getuid (void);
  • getpid(): returns the process ID of the calling process.
  • getppid() : returns the process ID of the parent of the calling process.
  • getgid() : returns the group ID of the calling process
  • getuid() : returns the user ID of the calling process

Creat

1
pid_t p = fork();

Remarks:

  • p == 0 (Child process)
  • p == -1 (Error)
  • p > 0 (Parent process)

Get ID


1
2
3
4
int my_PID = getpid();
int my_parent_PID = getppid();
int my_UID = getuid();
int my_GID = getgid();