
What is a UNIX Process
A process is an instance of a computer program that is being sequentially executed by a computer system that has the ability to run several computer programs concurrently.
Unix Process
An entity that executes a given piece of code, has its own execution stack, its own set of memory pages, its own file descriptors table, and a unique process ID.
The fork() system call
The fork() system call is the basic way to create a new process. It is also a very unique system call, since it returns twice(!) to the caller.
Example:
#include <unistd.h>
pid_t child_pid;
int child_status;
/* lets fork off a child process... */
child_pid = fork();
/* check what the fork() call actually did */
switch (child_pid) {
case -1: /* fork() failed */
perror("fork"); /* print a system-defined error message */
exit(1);
case 0: /* fork() succeeded, we're inside the child process */
printf("Hello, World\n");
exit(0); /* here the CHILD process exits, not the parent. */
default: /* fork() succeeded, we're inside the parent process */
wait(&child_status); /* wait till the child process exits */
}
/* parent's process code may continue here... */
Notes:
* The perror() function prints an error message based on the value of the errno variable, to stderr.
* The wait() system call waits until any child process exits, and stores its exit status in the variable supplied. There are a set of macros to check this status, that will be explained in the next section.
0 comments:
Post a Comment