c c review bits bytes same bits different interpretation
play

C/C++ review bits & bytes Same bits, different interpretation - PowerPoint PPT Presentation

CSC209 Fall 2001 C/C++ review bits & bytes Same bits, different interpretation What is the value of 11111111 ? it depends on the type (unsigned) char 1 byte = 8 bits, (unsigned) Unsigned: 255 short int 2 bytes =


  1. CSC209 Fall 2001 C/C++ review – bits & bytes Same bits, different interpretation � What is the value of 11111111 ? – it depends on the type � (unsigned) char – 1 byte = 8 bits, (unsigned) � Unsigned: 255 short int – 2 bytes = 16 bits, (unsigned) int – � Signed: -1 4 bytes = 32 bits, float – 4 bytes, double – 8 � Interpretation of signed & unsigned is the same as bytes, pointer = 32 bits long as most significant bit (msb) not set. � No matter what the type, each is just a � If msb (the sign bit) is set, then the value is sequence of bits (1s and 0s) in memory: negative. The value is given by: � -(value of msb) + (value of rest of bits) � Eg: 01100001 = 97 = ‘a’ � So for 11111111: -(2^7) + (2^6+2^5+…+2^0) = -1 � The left most bit is the “most significant bit”, the � This is the two’s complement representation right most the “least significant � unsigned char uc = 255; char c = (char) uc; printf(“%d\n”,c) A chart of values for a 4 bit integer Manipulate bits Bit value unsigned signed � Show 4 ways of turning on the 8 th bit, using a 0000 0 (0x0, 00) 0 0001 1 (0x1, 01) 1 decimal integer, a hex value, an octal value, and a 0010 2 (0x2, 02) 2 negative number: 0011 3 (0x3, 03) 3 0100 4 (0x4, 04) 4 Unsigned char uc=0; 0101 5 (0x5, 05) 5 uc = uc | 0110 6 (0x6, 06) 6 0111 7 (0x7, 07) 7 1000 8 (0x8, 010) -8 1001 9 (0x9, 011) -7 1010 10 (0xA, 012) -6 1011 11 (0xB, 013) -5 � Questions like: do I give chmod() a decimal number or an 1100 12 (0xC, 014) -4 octal number – are irrelevant. You give chmod() the right 1101 13 (0xD, 015) -3 1110 14 (0xE, 016) -2 bits. 1111 15 (0xf, 017) -1 Karen Reid 1

  2. CSC209 Fall 2001 Assignment strings � Those that contain less bits can be assigned � Is an array of characters. to those that contain more bits. � The string “777” is 3 bytes (not counting null terminator) � Eg. Assign char to int, short to int � ----------------------- | ‘7’ | ‘7’ | ‘7’| 0 |… � Assign more bits � less bits (eg. int to char) ----------------------- leads to data loss. Takes least significant � unsigned int uint=“777” is clearly nonsense. bits: � strtol(“777”,&stopstr,8) gives 511 � unsigned short ushort=0xeeff; � Does it make sense? unsigned char c = (char)ushort; � Is it ok to turn off the user read permission by: c=0xff mode= mode – 0400; Layers of System Software Systems Programming cat less vi date gcc nedit grep ddd csh (or bash or ksh) libc – C Interface to Unix system services How user programs interact with the Unix system services Operating System. Unix kernel (in C) computer Karen Reid 2

  3. CSC209 Fall 2001 Compiler vs. Interpreter C/C++ compiler � Somehow we need to convert a program into Preprocessor does text replacement 1. machine code (object code). #include replaced by the text of the included file. � #define macros replaced throughout each file. � � A compiler passes over a whole program Compiler parses the program, performs 2. before translating it into object code. optimization, and produces assembly code. � An interpreter reads one line of code at a Assembler translates assembly code into machine 3. time. code. � An interpreter is a compiled program (often Linker combines object files and libraries into an 4. executable file. It resolves any remaining symbol written in C). references. Java Compiler/Interpreter Shell Interpreter � Compiler translates program to byte code. � The interpreter is a C program! � The JVM is a byte code interpreter that � The shell interpreter is the program executed translates byte code to machine code. when you write #!/bin/sh � Byte codes implement fine grain primitives. They are generic enough that other languages may be compiled to Java byte � Each line of a shell script is input to a C code. program that parses the line, and determines how to execute it. Karen Reid 3

  4. CSC209 Fall 2001 Standard Libraries Shared Libraries � System calls are not part of the C language � .a libraries are not shared. The functions used are definition. copied into the executable of your program. � size bloat when lots of processes use the same libraries � System calls are defined in libraries (.a .so) � performance and portability are the wins � Libraries typically contain many .o object � .so libraries are shared. One copy exists in files. memory, and all programs using that library link to it � To create your own library archive file: to access library functions. ar crv mylib.a *.o � reduces total memory usage when multiple processes use � Look in /usr/lib and /usr/local/lib the shared library. for system libraries. � small performance hit as extra work must be done either when a library function is called, or at the beginning. Shared vs. Non-Shared Libraries System calls Non-shared Shared � Perform a subroutine call directly to the Unix kernel. link printf � libc provides the C interface to system calls printf copy � 3 main categories printf � File management � Process management main(){ main(){ � Error handling printf() libc.a printf() libc.so } } myprog myprog Karen Reid 4

  5. CSC209 Fall 2001 Error Handling perror() Ch 2.4 � All system calls return -1 if an error occurs. � Library routine: � errno – global variable that holds the numeric code � void perror( char *str ) of the last system call. � perror displays str , then a colon(:), then an � Every process has errno assigned to zero at process creation time. English description of the last system call � When a system call error occurs, errno is set. error as defined in errno.h. � A successful system call never affects the current � Protocol value of errno . � check system calls for a return value of -1 � An unsuccessful system call always overwrites the current value of errno . � call perror() for an error description. � Always check the return value of system calls! Process vs. program Effective user and group ids � Real user-id (ruid), group id (rgid) = uid and gid of � A program is a set of machine instruction that user who initiated process when executed, does something useful � Sometimes (very rarely) a process can acquire the � A process is a running instance of a program. privilege of another user: get effective user id (euid) and effective group id. � There can be many processes running the � S_ISUID bit in 12 th bit of st_mode (regular same program permissions occupy lower 9 bits) � Eg. 2 instances of netscape � If set, then process assumes euid taken from the file owner � Example: passwd program � Normally ruid=euid, rgid=egid Karen Reid 5

  6. CSC209 Fall 2001 Process State Create new processes: fork Only one process can be � The ONLY way to create a new process running on a uniprocessor � The fork system call creates a duplicate of the currently running program. running The scheduler decides � The duplicate (child process) and the original (parent which of the ready process) both proceed from the point of the fork with processes to run. exactly the same data. � The only difference is the return value from the fork call. ready blocked/sleeping Process fork Process A A1 A process is ready if it A process is blocked if it could use the CPU immediately. waiting for an event (I/O, signal) Ch 5.2 Fork: PIDs and PPIDs Welcome to the INCREDIBLE matter replicator! � System call: int fork() � If fork() succeeds it returns the child PID to the parent and returns 0 to the child; � If fork() fails, it returns -1 to the parent (no child is created) � Related system calls: � int getpid() – returns the PID of current process � int getppid() – returns the PID of parent process (ppid of 1 is 1) Karen Reid 6

  7. CSC209 Fall 2001 When fork() fails fork() properties � Properties of parent inherited by child: � There is limit to the maximum number of � UID, GID processes a user can create. � controlling terminal � Once this limit is reached, subsequent calls � CWD, root directory to fork() return -1. � signal mask, environment, resource limits � Memory � File descriptors � Differences between parent and child � PID, PPID, return value from fork() � pending alarms cleared for child � pending signals are cleared for child Fork example Fork example int i, pid; Original process (parent) child process i = 5; int i, pid; int i, pid; i = 5; i = 5; printf(“%d\n”, i); printf(“%d\n”, i); printf(“%d\n”, i); pid = fork(); /* prints 5 */ pid = fork(); pid = fork(); /* pid == 677 */ /*pid == 0 */ if(pid != 0) if(pid != 0) if(pid != 0) i = 6; /* only parent gets here */ i = 6; i = 6; else else else i = 4; i = 4; i = 4; /* only child gets here */ printf(“%d\n”, i); printf(“%d\n”, i); printf(“%d\n”, i); /* prints 6 */ /* prints 4 */ Karen Reid 7

Recommend


More recommend