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.

Slides:



Advertisements
Similar presentations
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Advertisements

15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
The Process Model.
Processes CSCI 444/544 Operating Systems Fall 2008.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
Unix Processes.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
CSSE Operating Systems
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Unix Processes operating systems. The Process ID Unix identifies each process with a unique integer called a process ID. The process that executes the.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
Process Description and Control Chapter 3. Major Requirements of an OS Interleave the execution of several processes to maximize processor utilization.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes Tarek Abdelzaher Vikram Adve.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Process. Processes A process is an abstraction for sequence of operations that implement a computation/program. A process may be manipulated, suspended,
Operating Systems Chapter 2
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-1 Process Concepts Department of Computer Science and Software.
Processes: program + execution state
Creating and Executing Processes
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
CS 241 Section Week #2 9/9/10. 2 Topics This Section MP1 issues MP2 overview Process creation using fork()‏ Debugging tools: valgrind, gdb.
1 C Basics. 2 The C Language Spirit Made by professional programmers for professional programmers Very flexible, very efficient, very liberal Does not.
System calls for Process management
Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section.
Processes Dr. Yingwu Zhu. Process Concept Process – a program in execution – What is not a process? -- program on a disk - a process is an active object,
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads.
Operating Systems Process Creation
CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6.
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
System calls for Process management Process creation, termination, waiting.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Slide 1 COMP 3438 System Programming UNIX Processes UNIX Processes (Chapters 2 & 3)
1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
CSCI 4061 Recitation 2 1.
Chapter 3: Processes.
Chapter 3: Process Concept
Topic 3 (Textbook - Chapter 3) Processes
Unix Process Management
Chapter 3: Process Concept
Chapter 3: Processes.
Processes A process is a running program.
Processes in Unix, Linux, and Windows
Example questions… Can a shell kill itself? Can a shell within a shell kill the parent shell? What happens to background processes when you exit from.
Tarek Abdelzaher Vikram Adve Marco Caccamo
Chapter 3: Processes.
Processes in Unix, Linux, and Windows
CGS 3763 Operating Systems Concepts Spring 2013
Lecture 5: Process Creation
System Structure B. Ramamurthy.
LINUX System Programming with Processes (additional)
Processes in Unix, Linux, and Windows
System Structure and Process Model
Lecture 6: Multiprogramming and Context Switching
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
CS510 Operating System Foundations
CSE 451: Operating Systems Autumn 2004 Module 4 Processes
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Presentation transcript:

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 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.

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

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.

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.

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.

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)

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?

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

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

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.

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);

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

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; }

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; }

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

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

Process

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

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

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

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

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; }

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!

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?

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); }

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); }

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

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

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

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

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

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

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);

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

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

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

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

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

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.

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”);

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; }

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)

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

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.

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

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