A few LINUX system functions

Here are some notes on a few LINUX system functions. In each case, there is more to say that what is mentioned here. A good place to find more information is linux.die.net.

You may also want to read about signals and errno, among other topics.


getpid() and getppid()

Prototypes:

Libraries:

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

Purpose:

Notes:


fork()

Prototype: pid_t fork(void)

Library:

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

Purpose: fork() creates a new process (a child of the calling process) by duplicating the calling process (the parent). The entire address space is duplicated: code, variables, file descriptors, etc., except for a few items (some of which are listed below).

Notes:


pipe()

Prototype: int pipe(int pipefd[2]);

Library:

     #include <unistd.h>

Purpose: pipe() creates a data channel for interprocess communication. The array pipefd is used to return two file descriptors for the two ends of the pipe: pipefd[0] is the read end and pipefd[1] is the write end. Data written to the write end is buffered until it is read from the read end.

Notes:


wait() and waitpid()

Prototypes:

Libraries:

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

Purpose: These are used to cause the calling process to wait until a child process changes state. Such a change might be: the child terminated, the child was stopped by a signal, or the child was resumed by a signal.

waitpid() waits until the child process specified by pid has changed state. The default (options = 0) is to wait until the child terminates. Here options has many values. (We combine various predefined constants with OR.) If pid is -1, the wait is for any child at all. The return value is the PID of the child whose state has changed, or -1 upon error. If options is not null, it is used to return information about the child. For instance, WIFEXITED returns a nonzero value if the child terminated, and WEXITSTATUS returns the exit status of the child.

wait() waits until one child process has terminated. It is equivalent to waitpid(-1, &status, 0). The return value is the PID of the terminated child, or -1 upon error.

Notes:


exit()

Prototype:

     void _exit(int status);  /* Notice the _ in front of the name. */
     void _Exit(int status);  /* synonym */

Library:

     #include <stdlib>

Purpose: exit() terminates the calling process (after any open file descriptors are closed).

Notes:


clone()

Prototype: int clone(int (* fn)(void *), void * child_stack, int flags, void * arg);

Library:

     <sched.h>

Purpose: clone() creates a LINUX thread of the calling process.

Notes:


gettid()

Prototype: pid_t gettid();

Library:

     <sys/types.h>
Purpose: gettid() returns the thread id of the calling process. Notes:


time()

Prototype: time_t time(time_t * t); /* time_t is a typedef for (probably long unsigned) int */

Library:

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

Purpose: time() returns the time as the number of seconds since a reference time called the "Epoch", 1970/01/01 at 00:00:00. If t is not null, then *t also contains the value.

Notes:


sleep()

Prototype: unsigned int sleep(unsigned int seconds);

Library:

     #include <unistd.h>

Purpose: sleep() makes the calling process sleep (idle, waiting, doing nothing) until the requested number of seconds have elapsed and then returns 0.


init()

This refers to the first process started as the system loads. It is the ancestor, one way or another, of all processes. Its pid is typically 1. Obviously, we do not ever need to call it.


system()

Prototype: int system(const char * command);

Library:

     #include <stdlib.h>

Purpose: system() executes a shell command (that is, something we might type at the command line) and and returns to the calling program after the command has been executed. If there is an error, the return code is -1. Otherwise the return code is the return status of the command (which is more or less the value the command uses as an argument of exit). (Here the command is typically a string constant as in "ls -al".)

Notes:


exec() family of functions

Prototypes:

Library:

     #include <unistd.h>

Purpose: the exec() functions transform the current process into a new process by executing the function specified in the argument name (which may also contain a path). The other arguments are passed to the function. This is sometimes called an "overlay".

The differences between the various versions follow patterns:

Notes:


dup()

Prototype: int dup(int oldfd);

Library:

     #include <unistd.h>

Purpose: dup() creates a copy of the file descriptor "oldfd". On error, the return value is -1. On success, the number of the new file descriptor is returned.

Notes:


daemon()

Prototype: int daemon(int nochdir, int noclose);

Library:

     #include <unistd.h>

Purpose: daemon() is used when we want to make a program run in the background as a system daemon. (For instance, there may be daemons to do logging or network connections.)

Notes: