1.6. Programs and ProcessesProgramA program is an executable file residing on disk in a directory. A program is read into memory and is executed by the kernel as a result of one of the six exec functions. We'll cover these functions in Section 8.10. Processes and Process IDAn executing instance of a program is called a process , a term used on almost every page of this text. Some operating systems use the term task to refer to a program that is being executed.The UNIX System guarantees that every process has a unique numeric identifier called the process ID . The process ID is always a non-negative integer. ExampleThe program in Figure 1.6 prints its process ID.If we compile this program into the file a.out and execute it, we have$ ./a.out hello world from process ID 851 $ ./a.out hello world from process ID 854 When this program runs, it calls the function getpid to obtain its process ID. Figure 1.6. Print the process ID#include "apue.h" int main(void) { printf("hello world from process ID %d\n", getpid()); exit(0); } Process ControlThere are three primary functions for process control: fork, exec, and waitpid. (The exec function has six variants, but we often refer to them collectively as simply the exec function.) ExampleThe process control features of the UNIX System are demonstrated using a simple program (Chapter 18, we describe all the special terminal charactersend of file, backspace one character, erase entire line, and so onand how to change them. If we run this program, we get the following results. Note that our program has a different promptthe percent signto distinguish it from the shell's prompt.$ ./a.out % date Sun Aug 1 03:04:47 EDT 2004 programmers work late % who sar :0 Jul 26 22:54 sar pts/0 Jul 26 22:54 (:0) sar pts/1 Jul 26 22:54 (:0) sar pts/2 Jul 26 22:54 (:0) % pwd /home/sar/bk/apue/2e % ls Makefile a.out shell1.c % ^D type the end-of-file character $ the regular shell prompt Figure 1.7. Read commands from standard input and execute them#include "apue.h" #include <sys/wait.h> int main(void) { char buf[MAXLINE]; /* from apue.h */ pid_t pid; int status; printf("%% "); /* print prompt (printf requires %% to print %) */ while (fgets(buf, MAXLINE, stdin) != NULL) { if (buf[strlen(buf) - 1] == "\n") buf[strlen(buf) - 1] = 0; /* replace newline with null */ if ((pid = fork()) < 0) { err_sys("fork error"); } else if (pid == 0) { /* child */ execlp(buf, buf, (char *)0); err_ret("couldn't execute: %s", buf); exit(127); } /* parent */ if ((pid = waitpid(pid, &status, 0)) < 0) err_sys("waitpid error"); printf("%% "); } exit(0); } The notation ^D is used to indicate a control character. Control characters are special characters formed by holding down the control keyoften labeled Control or Ctrlon your keyboard and then pressing another key at the same time. Control-D, or ^D, is the default end-of-file character. We'll see many more control characters when we discuss terminal I/O in Chapter 18. Threads and Thread IDsUsually, a process has only one thread of controlone set of machine instructions executing at a time. Some problems are easier to solve when more than one thread of control can operate on different parts of the problem. Additionally, multiple threads of control can exploit the parallelism possible on multiprocessor systems.Chapter 12. ![]() |