0) { buf[read_size] = 0; // terminate the buffer write(stdout, buf, read_size); // print it to the screen read_size = read(f, buf, BUFFSIZE); } // end of while } // end of if-else close(f); return 0; }"> 0) { buf[read_size] = 0; // terminate the buffer write(stdout, buf, read_size); // print it to the screen read_size = read(f, buf, BUFFSIZE); } // end of while } // end of if-else close(f); return 0; }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction Systems Programming Overview  Systems programming is the programming activity to create software used by other programmers and users. System.

Similar presentations


Presentation on theme: "Introduction Systems Programming Overview  Systems programming is the programming activity to create software used by other programmers and users. System."— Presentation transcript:

1 Introduction Systems Programming Overview  Systems programming is the programming activity to create software used by other programmers and users. System programming examples include the creation of a compiler, a database, an operating system, a game, or a word processor.  System programming is in contrast with application programming which processes data for end-users.  Systems programming often involves extensive use of resources from operating systems and programming libraries. 1-1

2 Introduction Systems Calls and Library Calls  System calls on Linux are function calls to the functions provided by the Linux operating systems. These calls are described in Section 2 of the Linux manual pages.  E.g., open is a system call to open a file handler  Library calls on Linux are function calls to various C libraries. These calls are described in Section 3 of the Linux manual pages.  E.g., fopen is a C library call to open a file (stream) handler. 1-2

3 Systems Call Example Introduction 1-3 #include #include // for exit(3) #define BUFFSIZE 512 int main(int argc, char* argv[]) { int f = open("syscall.c", O_RDONLY); char buf[BUFFSIZE + 1]; int read_size = 0; int stdout = 1; // number one is the standard output if (f < 0) { perror("Open failed -- "); exit(1); // abnormal exit } else { read_size = read(f, buf, BUFFSIZE); while (read_size > 0) { buf[read_size] = 0; // terminate the buffer write(stdout, buf, read_size); // print it to the screen read_size = read(f, buf, BUFFSIZE); } // end of while } // end of if-else close(f); return 0; }

4 Library Call Example Introduction 1-4 #include #include // for exit(3) int main(int argc, char* argv[]) { char c; FILE * f = fopen("libcall.c", "r"); if (f == NULL) { fprintf(stderr, "open failed.\n"); exit(1); } else { fscanf(f, "%c", &c); while (!feof(f)) { fprintf(stdout, "%c", c); fscanf(f, "%c", &c); } // end of while } // end of if-else fclose(f); return 0; }

5 Library and System Call Facts  Where do the manual pages reside?  /usr/share/man  What is the format of text of the manual pages?  nroff (take a look at an example, e.g., write.2.tgz)  How many system calls are there?  Roughly 430+  How many library calls (c, X, …) are there?  Roughly 8,000 Introduction 1-5

6 C Program Common Mistakes(1)  Off-by-one // example 1. char * src = “hello”; char * dst = (char*)malloc(strlen(src)); strcpy(dst, src); // or strncpy(dst, src, strlen(src)); // example 2. int max = 16; char buf[max]; … // file opened properly int size_read = read(f, buf, max); if (size_read < max) buf[size_read] = 0; // terminate the string else buf[max] = 0; // terminate the string Introduction 1-6

7 C Program Common Mistakes(2)  Forgot size differences // example 1. long int x; // in some old programs, long == int, 32 bits /* new use in gnu c compiler */ #include uint32_t x; // 32-bit int uint16_t y; // short, 16-bit int uint64_t z; // 64-bit int on 64-bit systems // example 2. struct x { … }; // structure x is defined properly here printf( “size of struct x %d\n”, sizeof(struct x)); // correct way printf( “size of struct x %d\n”, sizeof(struct x *)); // pointer to struct Introduction 1-7

8 C Program Common Mistakes(3)  Forgot to initialize variables before using them // example 1. char * a; strcpy(a, “hello”); // example 2. struct x { … }; struct x *y; y-> member = 5; Introduction 1-8

9 C Program Common Mistakes(4)  Deallocate variable memory when still in use char * list_remove(struct list_t * listptr) { struct node_t * toremove = listptr->head; char * retval = listptr->head->name; listptr->head = listptr->head->next; free(toremove); return retval; } Introduction 1-9


Download ppt "Introduction Systems Programming Overview  Systems programming is the programming activity to create software used by other programmers and users. System."

Similar presentations


Ads by Google