Presentation is loading. Please wait.

Presentation is loading. Please wait.

Correct C Programs. The C compiler cc [filename.c] Options include: -o [output file name] -lm to include the maths library -E to get output from preprocessor.

Similar presentations


Presentation on theme: "Correct C Programs. The C compiler cc [filename.c] Options include: -o [output file name] -lm to include the maths library -E to get output from preprocessor."— Presentation transcript:

1 Correct C Programs

2 The C compiler cc [filename.c] Options include: -o [output file name] -lm to include the maths library -E to get output from preprocessor only (no compilation). For a large program you can compile a small piece at a time, and link it to larger, pre-compiled parts. To compile only, use: cc -c [filename.c] This creates object files (whose names end in.o), which can then be linked later by using them instead of the source files. The compiler works in three stages: 1. it preprocesses (replacing the #include, #define etc); 2. it compiles the preprocessed files; 3. it links all of the pieces of code together into the executable program.

3 C Compiler: Libraries To make the functions available to other programs, add the library to the list of compiled files: cc -o pgm main.c file1.c g_lib.a Follow this by ranlib g_lib.a which organizes the file in a form that is useful for the linker. It is possible to create libraries, which are (generally large) collections of useful object code ready for linking, so they don’t need to be compiled over and over again for different programs. To do this with a set of.o files, ar ruv g_lib.a gfopen.o gfclose.o gcalloc.o....... This makes a library file called g_lib.a. ar = archive ruv = replace, update, verbose g_lib.a = output archive file name object files to go in the library

4 Conditional compilation There is also an #ifndef, which includes lines if a name is not defined. There is also an #undef function to remove previous definitions, in order to prevent clashes. To match the #if, there is an #else; there is also #elif, which is short for else if. The preprocessor can be used to exclude code from compilation: #define DEBUG 1 /*set debug to true at top of file*/...... #if DEBUG printf(“debug: a = %d\n”, a); #endif Because DEBUG has value 1, the printf statements will be compiled. The statements can be turned off by setting DEBUG = 0 at the start. An alternative is #ifdef DEBUG.... #endif which will include the lines if DEBUG is defined at all at the top.

5 Preprocessor macros with arguments #define SQ(x)((x) * (x)) will replace, e.g., SQ(a + b) by ((a + b) * (a + b)). Note here that all of the parentheses are needed! #define min(x, y) (((x) < (y)) ? (x) : (y)) will replace an expression such as m = min(u, v) by m = (((u) < (v)) ? (u) : (v)) The arguments of m can be arbitrary expressions of comparable type.

6 Writing large programs A large program will normally be stored as a collection of.h and.c files in its own directory. If we are developing a program called pgm, we make a header file pgm.h, which contains #includes, #defines and a list of function prototypes. All.c files should then have #include “pgm.h” at the top. Note the use of quotes rather than in, when you include your own header files. The compiler then looks in the current directory rather than in the directory for standard C header files.

7 Header files in large programs Often the same header files are included in several different source code files. How to avoid multiple definitions when the header file is compiled repeatedly? Let us assume we have a header file called MyHeaderFile1.h.: #ifndef MYHEADERFILE1 #define MYHEADERFILE1.... function prototypes;.... definitions; #endif Thus, the material in the file is only included if it hasn’t been included already. this is only compiled if MYHEADERFILE1 is not defined (i.e. if the header file has not been processed before)

8 Program Correctness You thought that you implement the program correct, but it crashes. Why? A common problem: memory Do you allocate any buffer? Do you allocate enough space? Do you free it only once?

9 Segmentation Violations and Bus Errors #include main() { char *s; s = NULL; printf("%d\n", s[0]); } (1)What will it happen? (2) Why?

10 Importance of Writing Correct Programs char buf[80]; printf(“Enter your first name”); scanf(“%s”, buf); Any problem? Problem: if the user enters more than 79 bytes, the resulting string and the string terminator \0 do not fit in the allocated variable!! Possible Fix of the Problem? char buf[80]; printf(“Enter your first name”); scanf(“%79s”, buf);. Buffer Overflow

11 Library Function Calls Traditional UNIX – returns 0 successful, -1 unsuccessful, sets errno POSIX Standard Committee decides that no ‘errno’ be used. Instead: all new functions return error code Good coders handle ALL errors, not just mandatory (in the standard) ones.

12 Error reporting perror function outputs to standard error a message corresponding to the current value of errno No return values of errors are defined by perror #include void perror(const char * s); strerror function returns a pointer to the system error message corresponding to the error code errnum If successful, strerror returns a pointer to the error string #include char *strerror(int errnum);

13 Handling Errors Always handle all errors Either: Print error message and exit program (only in main) Return -1 or NULL and set an error indicator such as errno Return an error code All functions should report errors to calling program Use conditional compilation to enclose debugging print statements cc -DDEBUG

14 Debug Your program Discussion: It crashes, what can I do? Tip1: Code review by yourself and others  Do you know the problem is? char w[16]; char document[4096]; char docFname[128]; File *fp; int main (int argc, char **argv ){ //parsing parameters … //reading document … for(i=0; i<=docLength; i++){ if(!strcmp(&(document[i]), w)){ printf(“found!\n”); return; }

15 Tip2: Print Out More Debugging Info … int main (int argc, char **argv ){ //parsing parameters … //reading document … printf(“w=%s, docLength=%d”, w, docLength); for(i=0; i<=docLength; i++){ printf(“i=%d, document[i]=%c\n”, I, document[i]); if(!strcmp(&(document[i]), w)){ printf(“found!\n”); return; }

16 Tip3: Try Different Inputs Test case 1: the document contains the word Test case 1a: the document contains the word in the very beginning Test case 1b: the document contains the word in the very end Test case 2: the document does not contain the word Further reduce the document size to see if it changes

17 Tip4: Interactive Debuggers Professional engineers use interactive debuggers (gdb, Microsoft visual studio, etc) gdb  You can stop the program in the middle, examine the state (variable values, etc)  You can step one statement by one statement  You can change the variable values

18 Process

19 Processes A process is an abstraction for sequence of operations that implement a computation/program. A process may be manipulated, suspended, scheduled and terminated Process Types: OS processes executing system code program User processes executing user code program Processes are executed concurrently with CPU multiplexing among them

20 Process States Possible process states Running (occupy CPU) Blocked Ready (does not occupy CPU) Other states: suspended, terminated Why not the following transitions? Ready to blocked Blocked to running

21 1 CPU can run Multiple Processes Multiple CPUs can run Multiple Processes 13000 program counter of CPU2

22 Linux 5 State Process Model Add states for creating and deleting process Add transitions Timeout, Dispatch, Event Occurs Events are: I/O Synchronization

23 Process Identification UNIX identifies processes via unique value Process ID Each process has also parent process ID since each process is created from a parent process. Root process is the ‘init’ process ‘getpid’ and ‘getppid’ – functions to return process ID (PID) and parent process ID (PPID) #include int main (void) { printf(“I am process %ld\n”, (long)getpid()); printf(“My parent id %ld\n”, (long)getppid()); return 0; }

24 Creating a Process - Fork Creating a process and executing a program are two different things in UNIX Fork duplicates a process so that instead on one process you get two--- But the code being executed doesn’t change!!! Fork returns 0 if child -1 if fork fails Child’s PID if parent process Child gets new program counter, stack, file descriptors, heap, globals, pid!

25 Creating a Process in Unix #include int main(void) { int x; x = 0; fork(); x = 1; printf("I am process %ld and my x is %d\n", (long)getpid(), x); return 0; } What does this print?

26 UNIX Example #include int main(void) { pid_t parentpid; pid_t childpid; if ((childpid = fork()) == -1) { perror(can’t create a new process); exit(1); } else if (childpid == 0) {/* child process executes */ printf(“child: childpid = %d, parentpid = %d \n”, getpid(), getppid()); exit(0); } else { /*parent process executes */ printf(“parent: childpid = %d, parentpid = %d \n”, childpid, getpid()); exit(0); }

27 UNIX Example childpid = fork() if (childpid == 0) { printf(“child: childpid = %d, parentpid = %d \n”, getpid(), getppid()); exit(0); } else { printf(“parent: childpid = %d, parentpid = %d \n”, childpid, getpid()); exit(0); }

28 Chain and Fan Child Parent Child … … pid_t childpid = 0; for (i=1;i<n;i++) if (childpid = fork()) break; pid_t childpid = 0; for (i=1;i<n;i++) if ((childpid = fork()) <=0) break; ChainFan

29 Process Operations (Creation) When creating a process, we need resources such as CPU, memory files, I/O devices Process can get resources from the OS or from the parent process Child process is restricted to a subset of parent resources  Prevents many processes from overloading system Execution possibilities are  Parent continues concurrently with child  Parent waits until child has terminated Address space possibilities are:  Child process is duplicate of parent process  Child process has a new program loaded into it

30 Process Termination Normal exit (voluntary) End of main() Error exit (voluntary) exit(2) Fatal error (involuntary) Divide by 0, core dump / seg fault Killed by another process (involuntary) Kill procID, end task

31 Process Operations (Termination) When a process finishes last statement, it automatically asks OS to delete it Child process may return output to parent process, and all child’s resources are de- allocated. Other termination possibilities Abort by parent process invoked  Child has exceeded its usage of some resources  Task assigned to child is no longer required  Parent is exiting and OS does not allow child to continue without parent

32 Process Hierarchies Parent creates a child process, a child process can create its own processes Forms a hierarchy UNIX calls this a "process group" Windows has no concept of process hierarchy all processes are created equal

33 wait() Function wait function allows parent process to wait (block) until child finishes wait function causes the caller to suspend execution until child’s status is available waitpid function allows a parent to wait for a particular child errnocause ECHILDCaller has no unwaited-for children EINTRFunction was interrupted by signal EINVALOptions parameter of waitpid was invalid

34 Waiting for a child to finish – C Manual #include pid_t childpid; childpid = wait(NULL); if (childpid != -1) printf(“waited for child with pid %ld\n”, childpid);

35 Waiting for a child to finish: RR:72 #include pid_t r_wait(int *stat_loc) { int retval; while (((retval = wait(stat_loc)) == -1) && (errno == EINTR)) ; return retval; } Restarts wait if interrupted by a signal Part of Restart library in RR, Appendix B

36 Scheduling vs Dispatcher? Scheduler decides which process to run next Dispatcher gives control to the next process selected by the scheduler Switching process context Switching to user mode Jumping to the proper location in the user program to restart that program

37 Wait for all children that have finished pid_t childpid; while (childpid = waitpid(-1, NULL, WNOHANG)) { if ((childpid == -1) && (errno != EINTR)) break; } // keep waiting Wait for any child of current process Waitpid returns 0 to report there are possible unwaited-for children but their status is not available Restart waitpid if function interrupted by signal or successfully waited for child

38 Dead children may become zombies When a process terminates but its parent does not wait for it? Zombie (pid entry remains in system process table) Zombies unlike orphans do not consume many resources. Professional code installs signal handler for signal SIGCHLD … which issues a wait() call

39 What happens When the parent process terminates first? “Child is orphaned” Child is “re-parented” to init process Child continues to consume resources init process (id=1) waits for children

40 Status Values for wait(int *stat_loc) The following macros are available for checking the return status of a child: #include WIFEXITED(int stat_val) WEXITSTATUS(int stat_val) WIFSIGNALED(int stat_val) WTERMSIG(int stat_val) WIFSTOPPED(int stat_val) WSTOPSIG(int stat_val) They are used in pairs. If WIFEXITED returns true, the child executed normally and the return status (at most 8 bits) can be gotten with WEXITSTATUS.

41 Simple example (without checking EINTR) int status; childpid = wait(&status); if (childpid == -1) perror("Failed to wait for child"); else if (WIFEXITED(status) && 0==WEXITSTATUS(status)) printf(“Child terminated normally”);

42 Typical process creation code (RR:74) #include int main (void) { pid_t childpid; /* set up signal handlers here... */ childpid = fork(); if (childpid == -1) { perror("Failed to fork");return 1;} if (childpid == 0) fprintf(stderr, "I am child %ld\n", (long)getpid()); else if (wait(NULL) != childpid) fprintf(stderr, "A signal must have interrupted the wait!\n"); else fprintf(stderr, "I am parent %ld with child %ld\n", (long)getpid(), (long)childpid); return 0; }

43 Results from running program 6 Possible forms of output!!! Fork fails (1) Parent catches signal after child executes fprintf but before child returns (2,3) Parent catches signal after child terminates and wait returns successfully(2,4) Parent catches signal between time child terminates and wait returns (2,3) or (2,4) Parent catches signal before child executes fprintf and parent fprintf happens first (3,2) Parent catches signal before child executes fprintf and child executes fprintf first (2,3)

44 exec() Function Exec function allows child process to execute code that is different from that of parent Exec family of functions provides a facility for overlaying the process image of the calling process with a new image. Exec functions return -1 and sets errno if unsuccessful

45 Exec Example #include int main (void) { pid_t childpid; childpid = fork(); if (childpid == -1) { perror("Failed to fork"); return 1; } if (childpid == 0) { /*child code*/ execl(“/bin/ls”, “ls”, “-l”, NULL); perror(“Child failed to exec ls”); return 1; } if (wait(NULL) != childpid) { /* parent code */ perror(“Parent failed to wait due to signal or error”); return 1; } return 0; } Argv[0] is by convention program name so need ls in parameters The array of pointers must be terminated by a NULL pointer.

46 Background Processes and Daemons Shell – command interpreter creates background processes if line ends with & When shell creates a background process, it does not wait for the process to complete before issuing a prompt and accepting another command Ctrl-C does not terminate background process Daemon is a background process that normally runs indefinitely

47 Daemons Not connected to a terminal (tty) Create logs in /var/log/ Parent process “forks, disconnects tty and dies” so they are orphans Event driven Examples: ftpd, sshd, pageout daemon, httpd,mysqld Special privileges & access rights to hardware Execute as root or with a specific daemon userid A target for buffer overflow/Denial of Service attacks


Download ppt "Correct C Programs. The C compiler cc [filename.c] Options include: -o [output file name] -lm to include the maths library -E to get output from preprocessor."

Similar presentations


Ads by Google