Introduction Systems Programming Overview Systems programming is the programming activity to create...
-
Upload
tabitha-lester -
Category
Documents
-
view
216 -
download
0
Transcript of Introduction Systems Programming Overview Systems programming is the programming activity to create...
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
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
Systems Call Example
Introduction 1-3
#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdlib.h> // for exit(3)#define BUFFSIZE 512int 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 outputif (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;
}
Library Call Example
Introduction 1-4
#include <stdio.h>#include <stdlib.h> // 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;
}
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
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 properlyint size_read = read(f, buf, max);if (size_read < max)
buf[size_read] = 0; // terminate the stringelse
buf[max] = 0; // terminate the stringIntroduction 1-6
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 <stdint.h>uint32_t x; // 32-bit intuint16_t y; // short, 16-bit intuint64_t z; // 64-bit int on 64-bit systems
// example 2.struct x { … }; // structure x is defined properly hereprintf( “size of struct x %d\n”, sizeof(struct x)); //
correct wayprintf( “size of struct x %d\n”, sizeof(struct x *)); //
pointer to struct Introduction 1-7
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
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