Section 3.1. Introducing Our Program


3.1. Introducing Our Program

This section introduces the sample program called create_process. This example C program illustrates the various states a process can go through, the system calls (which generate the transitions between these states), and the manipulation of the kernel objects that support the execution of processes. The idea is to reach an understanding of how a program is instantiated into a process and how an operating system handles a process.

 ----------------------------------------------------------------------- create_process.c  1   #include <stdio.h> 2   #include <sys/types.h> 3   #include <sys/stat.h> 4   #include <fcntl.h> 5 6   int main(int argc, char *argv[]) 7   { 8     int fd; 9     int pid; 11 12     pid = fork(); 13     if (pid == 0) 14     { 15        execle("/bin/ls", NULL); 16        exit(2); 17     }  18 19     if(waitpid(pid) < 0) 20        printf("wait error\n"); 21 22     pid = fork();     23     if (pid == 0){ 24        fd=open("Chapter_03.txt", O_RDONLY); 25        close(fd); 26     } 27 28     if(waitpid(pid)<0)   29        printf("wait error\n"); 30 31 32     exit(0); 33   } -------------------------------------------------------------------- 

This program defines a context of execution, which includes information regarding resources needed to fulfill the requirements that the program defines. For example, at any moment, a CPU executes exactly one instruction that it has just fetched from memory.[1] However, this instruction would not make sense if a context did not surround it to keep track of how the instruction referenced relates to the logic of the program. A process has a context that is composed of values held in the program counter, registers, memory, and files (or hardware accessed).

[1] Recall the text segment that was previously mentioned.

This program is compiled and linked to create an executable file that holds all the information required to execute this program. Chapter 9, "Building the Linux Kernel," details the partitioning of the address space of the program and how it relates to the information referred to by the program when we discuss process images and binary formats.

A process contains a number of characteristics that can describe the process as being unique from other processes. The characteristics necessary for process management are kept in a single data type, which is referred to as a process descriptor. We need to look at the process descriptor before we delve into the details of process management.




The Linux Kernel Primer. A Top-Down Approach for x86 and PowerPC Architectures
The Linux Kernel Primer. A Top-Down Approach for x86 and PowerPC Architectures
ISBN: 131181637
EAN: N/A
Year: 2005
Pages: 134

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net