# Common Process Functions
## Memory Allocation
Page No. 8
```c
#include <stdlib.h>
void * malloc(size_t size)
// allocates a block of size bytes
void * calloc(size_t nb, size_t size)
// allocates a block of nb * size bytes (initially set to 0)
void * realloc(void* ptr, size_t size)
// resizes a block of memory (preserves content)
void free (void * ptr)
// frees the allocated memory
```
## Process Creation: `fork`
Page No. 16
```c
#include <sys/types.h>
#include <unistd.h>
pid_t fork (void)
// Dynamic creation of a new process (child)
```
Return Values
|return value|case|
|-|-|
| 0 | returned to the child |
| child process pid | returned to the parent |
| -1 | system call failed |
## Process Termination: `exit`
Page No. 29
```c
// option 1
exit(int val)
// option 2
return val
```
These two ways have subtle differences. You can refer to [this answer](https://stackoverflow.com/questions/461449/return-statement-vs-exit-in-main) for detailed explanation.
`val` is a value that the parent can acquire by using `wait` or `waitpid` (covered later)
## Basic Parent/Child Synchronization
Page No. 32
### `wait`
Page No. 33
```c
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait (int* status)
```
If the calling process
1. has at least one zombie child
call returns the identity of one of the zombies
recovery of the return code value in variable `status`
2. has a child, but none is in a zombie state
process is blocked until one of its children becomes zombie
notification by a signal `SIGCHLD`
3. does not have any children
the call returns `-1` and the value of errno becomes `ECHILD`
### `waitpid`
Page No. 38
```c
#include <sys/types.h>
#include <sys/wait.h>
pid_t waitpid (pid_t pid, int* status, int opt )
```
Option `opt` of function waitpid allows a parent process to check the termination of a child
- with a specific `pid` value
- or that belongs to a group `|pid|`
| Value of parameter `pid` | waits for |
|-----------------------------------------|----------------------------------------------------------|
| > 0 | PID of child process |
| 0 | any process that belongs to the same group as the caller |
| -1 | any child process |
| < -1 any child process in group \|pid\| | any child process in group `\|pid\|` |
| Value of parameter `opt` | |
|-----------------------------------------|--------------------------------------|
| WNOHANG | non blocking call |
| WUNTRACED | check for stopped processes |
| Return Value | Case |
|-----------------------------------------|----------------------------------------------|
| -1 | error |
| 0 | (non blocking mode) process is still running |
| `pid` | of the zombie process |
## Code Replacement: `exec` family
The `exec` family of functions replaces the current process image with a new process image.
The `exec()` functions return only if an error has occurred. The return value is -1, and errno is set to indicate the error.
### `execl()` v.s.`execv()`
Page No.42
`argv` in list format. THE LAST PARAMETER MUST BE A `NULL` POINTER!
```c
#include <unistd.h>
int execl (const char *path, const char *arg, …);
```
`argv` in array format.
```c
#include <unistd.h>
int execv (const char *path, char * const argv[]);
```
### `execlp()` and `execvp()`
The `execlp()`, `execvp()` functions duplicate the actions of the shell in searching for an executable file if the specified filename does not contain a slash (/) character. The file is sought in the colon-separated list of directory pathnames specified in the `PATH` environment variable.
If the specified filename includes a slash character, then `PATH` is ignored, and the file at the specified pathname is executed. In addition, certain errors are treated specially.
If permission is denied for a file (the attempted `execve`(2) failed with the error `EACCES`), these functions will continue searching the rest of the search path.
If no other file is found, however, they will return with errno set to `EACCES`.
If the header of a file isn't recognized (the attempted `execve`(2) failed with the error `ENOEXEC`), these functions will execute the shell (`/bin/sh`) with the path of the file as its first argument. (If this attempt fails, no further searching is done.)
## Example: Visualization of a Program
```c
// This is parent.c
#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char** argv) {
if (fork()==0) {
execlp("echo", "echo", "Hello World!", NULL);
perror("execlp");
exit(1);
}
wait(0);
printf("Child has exited!");
exit(EXIT_SUCCESS);
}
```
<a href="https://ibb.co/frq0sv2"><img src="https://i.ibb.co/dkjKRgr/transfer.png" alt="transfer" border="0"></a>