Last active
October 8, 2025 01:00
-
-
Save Tribhuwan-Joshi/c3a9130cd779772da5b0578e4ef75ceb to your computer and use it in GitHub Desktop.
Notes for dive into systems book.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| wc -> word count. lines, word and bytes | |
| rm -i -> i flag ask to confirm | |
| Man page Sections | |
| 1 -> command line tool | |
| 2 -> unix system calls that should be called from program -> fork | |
| 3 -> library fn that should be called from program | |
| man section commands | |
| ssh user@remote | |
| scp -> scp location1 location2 | |
| -p port -> default 22 | |
| # copy from a remote subdirectory to host subdirectory -> scp user@host:./sub/ ./sub | |
| - If you wants to transfer many files using tar to zip or unzip and gizip/bzip2 are 2 file compression utilities | |
| - generate ssh key using -> ssh-keygen -t rsa | |
| it will generate 2 files inside ~/.ssh -> id_rsa and id_rsa.pub move id_rsa to remote server | |
| - use ssh-copy-id it will do the task for you | |
| VIM notes | |
| h j k l -> left down up right | |
| escape -> command mode, i-> interactive mode | |
| x -> delete character on cursor | |
| A -> append at the end of line | |
| w-> skip to the next word | |
| e -> skip to end of the word | |
| ^ -> go to start of the current line | |
| b -> went to previous | |
| dw -> delete the current word, cursor on the first letter | |
| O -> enter new line above the cursor | |
| d$ -> delete sentence to the right of the cursor | |
| 0-> move to the start of line | |
| dd -> delete whole line eg 2dd to delete 2 lines | |
| u -> undo | |
| U -> undo to the original state | |
| CTRL + R -> undo the undo | |
| dd -> delete the line, put in the register. | |
| p -> put the line | |
| r -> replace character with specific character | |
| s -> delete current character and enter insert mode | |
| ch4 | |
| ctrl + G -> cursor location and line number | |
| gg -> move to the start of file | |
| / -> search for pattern | |
| n -> search for same phrase again | |
| N -> search in opposite direction | |
| ctrl O -> to go back to previous position | |
| % -> matching parenthesis | |
| a -> skip to the end of the word -> append | |
| R -> continuous replace | |
| y -> copy text | |
| p -> paste | |
| ### GREP & FIND | |
| find ./(starting point) -name "something*"/"*.c" -type f/d | |
| GREP - The grep command finds patterns within a file or within a set of files. It can filter data from a source to a destination based on pattern matching. | |
| grep -n(line number) -H(list the file name) -i(case insenstive)"pattern" file(s) | |
| - pattern eg "[A-Z]ello*o" /b -> starts | |
| eg-> search for main in all file ending in *.c | |
| grep main *.c | |
| grep recursively -> -r | |
| ### FILE PERMISSION | |
| ls -la -> list permissions | |
| user/group/owner +/- | |
| CHMOD | |
| chmod xxx(0-7) file | |
| chmod u+x file / o-r etc | |
| CHGRP | |
| chgrp groupname directory/file | |
| Compress & Archiving | |
| tar -x(unzip)-z( uncompress/gzip) -c(create zip) -f (filename) -v(verbose) | |
| gzip/gunzip | |
| bzip2/bunzip2 | |
| # Process Control | |
| ./a.out & -> process in background | |
| ctrl + z -> suspend | |
| bg -> run in background | |
| fg -> run it in foreground | |
| // kill process | |
| kill -n(signal) pid | |
| pkill -n(signal) process-name | |
| # TIMING | |
| time command -> real, user, sys | |
| # history | |
| !command number or !! (recent command) | |
| # IO DIRECTION | |
| Types - stdin , stdout, stderr - every process begin with three open file streams | |
| 0,1,2 - stdin,stdout,stderr | |
| < redirect stdin | |
| > redirect stdout | |
| 2> redirect stderr | |
| &> redirect stdin and stdout to the same file | |
| >> append stdout to the file | |
| 1> [outfile] 2> &1( redirect stderr to stdout) | |
| cat non-existence 2> error.txt | |
| # Pipes | |
| - allow user to link together a sequence of commands in a single command line. A pipe redirects the stdout of the command listed before the pipe(|) | |
| eg cmd1 | cmd2 -> cmd1 ouput is cmd2 stdin | |
| xargs <cmd> command executes cmd on every value passed to it on stdin. | |
| eg ls | xargs wc | |
| # DOT FILEs AND BASHRC | |
| $env -> list all env variable | |
| which command -> tell where the command is coming from | |
| PATH=$PATH:/home/sarita/mybin or add :/your/path to PATH | |
| alias cmd="big cmd" | |
| # SHELL PROGRAMMING | |
| - shell script -> set of executable text file that consist of a sequence of shell commands | |
| - $@ -> argv, $#-> argc. Script name is not counted | |
| - if [[ cond ]] end with fi, do end with done | |
| umask -> delete the permission from file, default 666 and 777 for directories | |
| # SYSTEM INFORMATION | |
| htop/top -> system usage | |
| /proc & /sys -> psuedo file system contain detailed information about the current state of system | |
| - lscpu, lsmeme, lsgpu -> show information from /proc & /sys | |
| cat /proc/stat /cpuinfo/meminfo etc | |
| --------------------------------------------------------- | |
| # DEEPER DIVE INTO C | |
| Parts | |
| OS | |
| Code | |
| Data : global variable | |
| Heap: Dynamic memory allocation varaible | |
| Stack: local variable and function stack | |
| C program compilation process -> .c -> compiliation .s -> assembly -> .o -> Linker -> executable | |
| ------------------------C NOTES------------------------------------------- | |
| C always pass by value | |
| struct is copied when passed | |
| struct is a lvalue, means it can be assigned | |
| malloc(size of bytes to allocate) eg. malloc(sizeof(int)); | |
| - return memory address(void * ) or NULL if error occurs | |
| typcase before like (int *) | |
| after freeing the memory with free(p); | |
| set p = NULL; to avoid UB; | |
| C save some header to save the size of dynamic memory fragement in the heap. | |
| # 2D array C -> defining cols is good generic design when passing multi-dimensional array to a parameter | |
| option 1 -> malloc the whole 2d array at once - malloc((n*m) * sizeof(int)); | |
| way to send in fn -> (int *arr, int r, int c) -> arr[i*c + j]; | |
| option 2 -> malloc each row one by one. int **2darr = malloc(sizeof(int*) * N); 2darr[i] = malloc(int * M); | |
| way to send in fn -> (int **arr, int r, int c) -> arr[i][j]; | |
| ### STRING | |
| string literal -> save in read-only memory. NON-mutable. char str* = "test"; | |
| if fn return string then return type is char * and can only be assigned to variable with return type as char *; | |
| int strlen(char *s) -> return size excluding \0 | |
| char *strcpy(char *dst, char *src) -> copy src to dst return dst base address copy until \0 | |
| char *strncpy(char *dst, char *src, size_t size) -> copy upto first \0 or size | |
| -> Always add \0 after strncpy/strcpy | |
| -> strlcpy -> add \0 automatically -> safer char * strlcpy(char *dst, char *src, size_t size); | |
| compare 2 strings -> strcmp/strncmp - use ASCII | |
| return 0 -> same, +ve -> str1 > str2 , -ve -> str1<str2 | |
| char *strcat(char *dst, char *src) -> concat from src to dst | |
| char *strncat(char *dst, char *src, size_t size); -> add \0 automatically | |
| // finding substring/character values | |
| char *strstr(const char *string , const char* substr) -> find substr in string return NULL if can't find | |
| char *strchr(const char *s, int c); | |
| // if the return pointer is printed as %s -> it would be a string -> find first position for the match | |
| ### Dividing string into token | |
| char *strtoken(char *str, const char *delim); | |
| for keep moving the static strtok pointer use strtok(NULL,delimiter); | |
| // int sprintf(char *,const char *format,... ); | |
| - Function for individual character value | |
| #include <ctype.h> | |
| islower(c) , isupper(ch), isalpha(ch),isdigit,ispunct,isspace,tolower,toupper | |
| boolean fn return 0 if false otherwise positive value | |
| // convert Types | |
| int atoi(const char *nptr) // convert string to integer | |
| double atof(const char *nptr) // convert string to float | |
| STRUCTs & pointer | |
| sptr->gpa (destruct struct ) same as (*sptr).gpa | |
| struct studentT classroom1[40]; // an array of 40 struct studentT | |
| struct studentT *classroom2; // a pointer to a struct studentT | |
| // (for a dynamically allocated array) | |
| struct studentT *classroom3[40]; // an array of 40 struct studentT * | |
| // (each element stores a (struct studentT *) | |
| ## IO FILE | |
| %5.2f -> 5 character wide, after point only 2 character is allowed | |
| - give space on left if not enough character | |
| - char c = getChar(); //get character | |
| putchar(c); // output char to screen | |
| EOF - end of file | |
| FILE *infile, *outfile; -> can't be derefered | |
| open using fopen | |
| infile = fopen("input.txt", "r"); // w -> write, a->append | |
| if infile==NULL; // wrong path or don't have permission | |
| // close using fclose(infile); | |
| * reset pointer to beginning of file -> rewind(filepointer); | |
| * move to specific location fseek(FILE *f, long offset, int whence); | |
| whence -> SEEK_SET -> from start of the file, SEEK_CUR-> from curr position, SEEK_END -> from end | |
| offset -> 3 -> 3 character ahead. -3 -> 3 character back | |
| // returns the next character in the file stream (EOF is an int value) | |
| int fgetc(FILE *f); | |
| // pushes the character c back onto the file stream | |
| // at most one char (and not EOF) can be pushed back | |
| int ungetc(int c, FILE *f); | |
| // writes the char value c to the file stream f | |
| // returns the char value written | |
| int fputc(int c, FILE *f); | |
| // feof(file) -> check if EOF | |
| // char fgets(char *s, int n , FILE *f) -> read n-1 char to arr -> stopping if \0 is included | |
| // writes the string s (make sure '\0' terminated) to the file stream f | |
| int fputs(char *s, FILE *f); | |
| // writes the contents of the format string to file stream f | |
| // (with placeholders filled in with subsequent argument values) | |
| // returns the number of characters printed | |
| int fprintf(FILE *f, char *format, ...); | |
| eg - // use fprintf to print stderr: | |
| fprintf(stderr, "Error return value: %d\n", ret); | |
| int fscanf(FILE *f, char *format, ...); | |
| -------------------------------------------------------------------------------------------------------- | |
| ADVANCE C TOPICS | |
| - C constants, the switch statement, enumerated types, and typedef | |
| - command line arguments | |
| - the void * type and type re-casting | |
| - pointer arithmetic | |
| - C libraries: using, compiling, and linking | |
| - writing and using your own C libraries (and dividing your program into multiple modules (.c and .h files)) | |
| - compiling C source to assembly code. | |
| switch(Expression){ | |
| case <literal value 1> : | |
| statement; | |
| break; | |
| } | |
| if no break is provided it will run next case statement | |
| enum -> outside of fn-> each constants/literal has value like 0,1,2,3....so on | |
| enum days_of_week { | |
| SUN = 1, // start the sequence at 1 | |
| MON, // this is 2 (next value after 1) | |
| TUES, // this is 3, and so on | |
| WED, | |
| THURS, | |
| FRI, | |
| SAT | |
| }; | |
| typedef existing_type_name new_type_alias_name; | |
| The argv array contains argc + 1 elements. // NULL in the end of argv | |
| ------------------------------------------------------------------------------- | |
| Library | |
| API -> defined in .h files -> prototype | |
| Implementation -> gets linked to binary executable. Could be in .a files or .so(share object file) | |
| link using -l option | |
| The --static option provides one method for requesting static linking: | |
| gcc -o myprog myprog.c --static -pthread -lreadline | |
| compiliation steps :- | |
| 1. Precomplier steps -> expands preprocessor directives -> gcc -E prog.c. Errors -> include directive syntax, library not available | |
| 2. Compilation phase -> convert .c code to assembly -> .s. Errors-> c languge syntax error. gcc -S(aseembly) prog.c | |
| 3. assembly phase -> convert assembly code into relocatable binary object code -> .o -> -c flag to stop at this step | |
| - gcc and LINUX produce ELF (executable and linkable format) -> binary files -> a.out and .o can be viewed using objdump | |
| - objdump -d file | |
| 4. Linkable editing step - create single executable from relocatable binaries(.o) and libraries (.so / .a). Error -> symbol is undefined | |
| ## COMMON ERRORS | |
| #include <stdio.h> | |
| #include <examplelib.h> | |
| int main(int argc, char *argv[]) { | |
| int result; | |
| result = libraryfunc(6, MAX); | |
| printf("result is %d\n", result); | |
| return 0; | |
| } | |
| calling libraryfunc from examplelib.h where we have .so file for examplelib.h | |
| inside .h file | |
| #define MAX 10 // a constant exported by the library | |
| // a function exported by the library | |
| extern int libraryfunc(int x, int y); | |
| The extern prefix to the function prototype means that the function’s definition comes from another file | |
| LD_LIBRARY_PATH environment variable for the runtime linker to find a library’s .so file. | |
| header files stored at - /usr/include | |
| library files stored at /usr/libs | |
| also search in current working directory. If can't find, explicitly provide path using -I(header) -L(library) | |
| eg - gcc -I/home/me/include -o myprog myprog.c -L/home/me/lib -lexamplelib | |
| .h -> Interface -> should be include by files that need it | |
| .c -> Implementation | |
| option 1: | |
| - compile a binary form of the library that could be linked. could be directly build from source | |
| - compile and staticlly links them with executable | |
| options2: | |
| - compiled library in archive(.a) or (.so) shared object - usually don't has access to source code. must be linked using -l | |
| .c has internal fn. | |
| These internal functions are often defined with the keyword static, which scopes their availability to the module (.c file) in which they are defined. | |
| step1 : | |
| - create binary form of the library (.o file), compile with -c option | |
| - .o files can build an archive or .so | |
| - To build static library use archiver | |
| so create mylib.o from mylib.c - gcc -c -I(include fn) mylib.c | |
| then run gcc -o myprog with mylib.o and myprog.c | |
| ### Quick way (not recommended) | |
| gcc f1.c f2.c main.c -o main | |
| ### Recommended | |
| 1. compile each source to an object file (.o). -g -c. | |
| 2. Link all object file to executable -o | |
| 3. run ./a.out | |
| ### Static library (.a) | |
| - compile ur .c files(not main) into .o files | |
| - make static library using ar(Archive) rcs libmylib.a(name) avg.o reverse.o | |
| - gcc main.c -L. -lmylib -o main (link with main.c) | |
| - -I/home/me/myincludes provide -I(header file) if its not present in the current directory | |
| ---------------------------Compling C to assembly and compiling assembly code----------------- | |
| -S -> stop at assembly (Compilation phase ) -m32(compile into IA-32 and x86-64) | |
| to see machine code in .o(object file) using objdump -d x.o ( compile .c to .s) | |
| gcc -m32 -c simpleops.s -> complies .s to .o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment