Background Survey Answers Operating Systems CS 550 Spring 2016 Kenneth Chiu.

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

C Tutorial Ross Shaull cs146a Why C Standard systems language – Historical reasons (OS have historically been written in C, so libraries written.
Recitation By yzhuang, sseshadr. Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
CS 536 Spring Run-time organization Lecture 19.
C and Data Structures Baojian Hua
Run-time Environment and Program Organization
Netprog: Buffer Overflow1 Buffer Overflow Exploits Taken shamelessly from: netprog/overflow.ppt.
Memory Layout C and Data Structures Baojian Hua
Buffer overflows.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Agenda Attack Lab C Exercises C Conventions C Debugging
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Hank Childs, University of Oregon April 13 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Memory-Related Perils and Pitfalls in C
CSE 220 – C Programming malloc, calloc, realloc.
YongChul Kwon CSE451 Section 1: Spring 2006 YongChul Kwon
EGR 2261 Unit 11 Pointers and Dynamic Variables
Computer Organization and Design Pointers, Arrays and Strings in C
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Segmentation COMP 755.
A bit of C programming Lecture 3 Uli Raich.
Dynamic Memory Allocation Strings
System Programming and administration
C Programming Tutorial – Part I
CS 537 Section 1 Programming in Unix and C
Pointers and Memory Overview
Algorithm Analysis CSE 2011 Winter September 2018.
Hank Childs, University of Oregon
Arrays in C.
Programming Languages and Paradigms
Hank Childs, University of Oregon
Methods and Parameters
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Introduction to the C Language
C – Scope, Lifetime, and the Stack
Lecture 8b: Strings BJ Furman 15OCT2012.
CS 2308 Exam I Review.
Memory Management III: Perils and pitfalls Mar 13, 2001
Common C Programming Errors, GDB Debugging
Pointers, Dynamic Data, and Reference Types
Dynamic Memory Allocation
Memory Allocation CS 217.
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2015.
Your questions from last session
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
C Programming Getting started Variables Basic C operators Conditionals
Pointers and Arrays Beyond Chapter 16
Homework Reading Programming Assignments Finish K&R Chapter 1
EE 312 Exam I Review.
Dynamic Memory A whole heap of fun….
Homework Continue with K&R Chapter 5 Skipping sections for now
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2016.
EE 312 Exam I Review.
Pointers, Dynamic Data, and Reference Types
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2013.
SPL – PS1 Introduction to C++.
EE 312 Exam I Review.
Introduction to C CS 3410.
Presentation transcript:

Background Survey Answers Operating Systems CS 550 Spring 2016 Kenneth Chiu

Q5: Let’s say you have process A writing a number to a shared memory region. Process B reads the number from the shared memory region. Both processes are started at the same time. How do you make sure that Process B doesn’t read the number until Process A has finished writing the number? A: This is a traditional producer-consumer problem with a queue size of 1.

Q6: Write a C or C++ program to create a file named “data” containing exactly 1024 bytes with each byte having the value 0xff. A: Many people had trouble with this. Mainly just looking for the fact that you know there’s a key difference between ASCII and binary output, and just some ideas about how to do it. (In the real world, if you just have some slight idea, then you can quickly Google it. If you have no idea, then you don’t even know what to Google for in some cases.) – #include #include int main() { int rv; FILE *f = fopen(“data”, “w”); assert(f != 0); char b = 0xff; for (int i = 0; i < 1024; i++) { rv = fwrite(&b, 1, 1, f); assert(rv == 1); } rv = fclose(f); assert(rv == 0); return 0; }

Q7: Finish this function fragment to allocate two arrays using a single malloc() call: – void func(int n1, int n2) { // Allocate enough memory for both arrays. char *tmp = malloc(...); // Should point to beginning of n1 ints. int *int_array =...; // Should point to beginning of n2 doubles. double *double_array =...; for (int i = 0; i < n1; i++) int_array[i] = i; // Initialize the array of ints. for (int i = 0; i < n2; i++) double_array[i] = i; // Initialize the array of doubles.

A: – void func(int n1, int n2) { char *tmp = malloc(n1*sizeof(int) + n2*sizeof(double)); int *int_array = (int *) tmp; double *double_array = (double *) (tmp + n1*sizeof(int)); for (int i = 0; i < n1; i++) int_array[i] = i; for (int i = 0; i < n2; i++) double_array[i] = i;

Q8: Write code to test at run-time whether the machine is little-endian or big-endian? If you were required to determine the same information at compile-time, how would you do it? – int i = 1; if (*(char *) &i == 1) { printf(“little-endian\n”); } else { printf(“big-endian\n”); } – At compile time, use an #ifdef of some type.

Q9: Let’s say that you have 1000 JPEG files in a folder and its subfolders. Each file begins with the letters PIC. You wish to rename them all so that they begin with IMG instead. How would you do this? A: Correct answers: (1) “I would write a script in XYZ.” (2) “I would use the rename command.” (3) “I would create a sequence of commands in ABC.” Some people talked about the algorithm, or write a program in C, etc.

Q10. How do you debug a memory leak? A: What’s hard about it? – Use valgrind, Purify, etc. – If you can’t, you might be able to watch it in ‘top’. – To fix, if you can’t use valgrind, then you it becomes very difficult.

Q11. What causes a segfault? How do you debug it? – A segfault is caused by access to “unmapped” memory. It doesn’t exist, in other words. – Typically, it means you somehow got garbage values into a pointer. – valgrind is the best tool. Otherwise, gdb can be used.

Q12: Is this program correct? What do you think it will print out when you compile and run it? Explain why. – #include #include #include int main() { char *s = (char *) malloc(10); strcpy(s, “hello”); free(s); printf(“%s\n”, s); } A: Some people thought it would give segfault.

Q13: Assume that you have a file named foo in the current directory. The first four bytes contain an integer in binary using two’s complement (in native byte order). Let this integer have the value N. The next N bytes then contain a string. The string is not terminated with a 0 (null) byte. Write a program in any language and OS that will read in the string and print it to the console (standard output). A: First read the length, then read the rest. – #include #include #include int main() { int rv; int fd = open(“foo”, O_RDONLY); assert(fd >= 0); unsigned int n; rv = read(fd, &n, 4); assert(rv == 4); char *str = malloc(n + 1); str[n] = ‘\0’; rv = read(fd, str, n); assert(rv == n); rv = close(fd); assert(rv == 0); printf(“%s\n”, str); }

Q: Sketch a program that will be CPU-bound. A: A program that solves the TSP.

Q: Sketch a program that will be IO-bound. A: Databases are usually I/O bound.

Q14: What does a linker do? A: It resolves symbol references in object code to addresses and/or numbers, and combines the files into one file, usually an executable.

Q15: Why is a shared library called a shared library? A: Because it’s memory image can be shared by multiple processes.

Q16: Which is harder to debug, a deadlock or a race condition? A: Deadlocks are relatively easy. Attach to it with gdb. Check where all the threads are. Next step is harder, but then you need to backtrack to figure out how it got into this state. Usually, you can add a bunch of print statements to trace it. Race conditions can be very hard to debug. That’s because two given concurrent executions can have many different interleavings.