C questions A great programmer codes excellent code in C and Java. The code does video decoding. Java code works faster then C on my computer. how come?

Slides:



Advertisements
Similar presentations
Linux device-driver issues
Advertisements

Threads Relation to processes Threads exist as subsets of processes Threads share memory and state information within a process Switching between threads.
R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
CS 450 Module R4. R4 Overview Due on March 11 th along with R3. R4 is a small yet critical part of the MPX system. In this module, you will add the functionality.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
Operating Systems Course Hebrew University Spring 2007 Signals & User Thread.
Posix Message Queues Courtesy of W. Richard Stevens Unix Network Programming Volume 2: Interprocess Communication.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
chap13 Chapter 13 Programming in the Large.
Recitation 9: Error Handling, I/O, Man Andrew Faulring Section A 4 November 2002.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Lecture 11 Dynamic link libraries. Differences between static libraries and DLLs In static library code is added to the executable. In DLL, the code is.
Ethernet Driver Changes for NET+OS V5.1. Design Changes Resides in bsp\devices\ethernet directory. Source code broken into more C files. Native driver.
Pipes A pipe is a simple, synchronized way of passing information between processes A pipe is a special file/buffer that stores a limited amount of data.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
UNIX Files File organization and a few primitives.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system.
Linux Device Model Part 1
Operating Systems Recitation 4, April th, 2002 Signals.
1 CS503: Operating Systems Spring 2014 Part 0: Program Structure Dongyan Xu Department of Computer Science Purdue University.
Interfacing Device Drivers with the Kernel
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
Laface 2007 File system 2.1 Operating System Design Filesystem system calls buffer allocation algorithms getblk brelse bread breada bwrite iget iput bmap.
An Introduction to MPI (message passing interface)
COMP 3438 – Part I - Lecture 5 Character Device Drivers
17/02/2016S. Ponce / EP-LBC1 Debugging Under Linux Sebastien Ponce Friday, 8 March 2002.
January 7, 2003Serguei Mokhov, 1 File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
Program Libraries 1. What is a program library? A library is a collection of implementations of behavior, written in terms of a language, that has a well-defined.
OS interface: file and I/O system calls File operations in C/C++? –fopen(), fread(), fwrite(), fclose(), fseek() in C f.open(…), f.close(…) in C++ I/O.
CS252: Systems Programming Ninghui Li Based on Slides by Gustavo Rodriguez-Rivera Topic 2: Program Structure and Using GDB.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
File table: a list of opened files Each entry contains: – Index: file descriptors – Pointer to the file in memory – Access mode File descriptor is a positive.
CISC2200 Threads Fall 09. Process  We learn the concept of process  A program in execution  A process owns some resources  A process executes a program.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Files in UNIX u UNIX deals with two different classes of files:  Special Files  Regular Files u Regular files are just ordinary data files on disk -
File System Design David E. Culler CS162 – Operating Systems and Systems Programming Lecture 23 October 22, 2014 Reading: A&D a HW 4 out Proj 2 out.
The Shell What does a shell do? - execute commands, programs - but how? For built in commands run some code to do the command For other commands find program.
Linux Device Model A device model after 2.5
A bit of C programming Lecture 3 Uli Raich.
Program Execution in Linux
Processes in Unix, Linux, and Windows
Threads and Locks.
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
Memory Allocation CS 217.
CSE 333 – Section 3 POSIX I/O Functions.
CSE 333 – Section 3 POSIX I/O Functions.
CSE 333 – Section 3 POSIX I/O Functions.
Program Execution in Linux
CSE 451: Operating Systems Winter 2003 Lecture 4 Processes
Processes in Unix, Linux, and Windows
Appendix F C Programming Environment on UNIX Systems
Processes in Unix and Windows
Presentation transcript:

C questions A great programmer codes excellent code in C and Java. The code does video decoding. Java code works faster then C on my computer. how come? I write a function pointer to file. I later read it as char *. I cast to func pointer and try to run. will it work?

Example for question 2 int hello() { int i=8; return ++i; } typedef int (*hello_t)() ; int main(int argc, char * argv[]) { int fd; char buf[2000]; int size = abs(&(main) - &(hello)); fd=open("./hello.bin", O_RDWR | O_CREAT | O_TRUNC | O_EXCL); fchmod(fd,S_IRWXU); write(fd, (void *)&hello, size); close(fd); fd=open("./hello.bin", O_RDWR); int n = read(fd,buf,2000); hello_t func= buf; int i=(func)(); close(fd); return 0; }

Yet another C question what does the following code do? int main(int argc, char * argv[]) { char a[10]; int i; for (i=-5 ; i < 10 ; ++i ) a[i]=’\0’; return 0; }

More lkmpg

Sysfs (missing chapter) As /proc became a twisty maze of passages, all-alike.... Sysfs was built to represent the kernel module structure (and only the kernel module structure) We can change parameters and communicate with KM using sysfs

sysfs - references eople/mochel/doc/papers/ols- 2005/mochel.pdf p?id=kernel_objects#kernel_object_sysf s_example

sysfs - structure InternalExternal Kernel Objects Directories Object AttributesRegular Files Object Relationships Symbolic Links

sysfs - API int sysfs_create_dir(struct kobject ∗ k); void sysfs_remove_dir(struct kobject ∗ k); int sysfs_rename_dir(struct kobject ∗, const char ∗ new_name) int sysfs_create_file(struct kobject ∗, const struct attribute ∗ ); void sysfs_remove_file(struct kobject ∗, const struct attribute ∗ ); int sysfs_update_file(struct kobject ∗, const struct attribute ∗ ); int device_create_file(struct device ∗ device, struct device_attribute ∗ entry); void device_remove_file(struct device ∗ dev, struct device_attribute ∗ attr); struct device_attribute { struct attributeattr; ssize_t ( ∗ show)(struct device ∗ dev, char ∗ buf); ssize_t ( ∗ store)(struct device ∗ dev, const char ∗ buf, size_t count); }; struct attribute { char ∗ name; struct module ∗ owner; mode_t mode; }

Example review : p?id=kernel_objects#kernel_object_sysf s_example

Working with /proc /proc is not supposed to be used for communication with modules. use /sys for that.

API create_proc_entry(PROC_ENTRY_FILENAME, 0644, NULL); remove_proc_entry(PROC_ENTRY_FILENAME, &proc_root); Our_Proc_File->read_proc = procfile_read ssize_t procfile_read(char *buffer, char **buffer_location, off_t offset, int buffer_length, int *eof, void *data)

Chapter 9 - blocking processes We can cause a process to block by calling wait_event_interruptible

wait_event_interruptible puts the calling process in an event queue until something (specified in the second argument) happens.

blocking process - partial example while (Already_Open) { int i, is_sig = 0; /* * This function puts the current process, including any system * calls, such as us, to sleep. Execution will be resumed right * after the function call, either because somebody called * wake_up(&WaitQ) (only module_close does that, when the file * is closed) or when a signal, such as Ctrl-C, is sent * to the process */ wait_event_interruptible(WaitQ, !Already_Open); /* * If we woke up because we got a signal we're not blocking, * return -EINTR (fail the system call). This allows processes * to be killed or stopped. */

work queues We can scheduale work to be done periodically or within a certain time using queue_delayed_work

API’s init : static struct work_struct Task; static DECLARE_WORK(Task, intrpt_routine, NULL); static void intrpt_routine(void *irrelevant) register : my_workqueue = create_workqueue(MY_WORK_QUEUE_NAME); queue_delayed_work(my_workqueue, &Task, 100); unregister : cancel_delayed_work(&Task);/* no "new ones" */ flush_workqueue(my_workqueue); destroy_workqueue(my_workqueue);

And a little bit about linking Static libs Dynamic libs Linker configuration

Static libs static libraries are created using ar(1) we can create library using ar crv

Exercise create some objects create library using ar(1) ar crl mylib.a *.o ranlib mylib.a Create source that use your objects Link with library with -L and -lmylib.a use nm(1) for names

dylib use libtool -dynamic -o *.o default extension on the mac is.dylib default on linux is so default on windows is.dll

reading names from library We can read the names exported by library using nm(1) We can also see what dynamic libraries are used by using nm -mg Try running nm on libc and on iTunes

Striping binaries we can remove symbols using strip(1) command This will prevent debugging We can also save striped info to file

More info on object files Almost everything you may want is done using nm(1), strip(1), libtool(1), otool(1) and ar(1). You can spy on any system call being used using strace(1) - stack trace. Examine the man page for more information

Dynamic libs Linked at compile time and used at runtime. Loaded on the project startup We can also load a lib completely dynamically using dlopen (same as loadLibrary)

dlopen(2) NAME dlopen -- load and link a dynamic library or bundleSYNOPSIS #include void* dlopen(const char* path, int mode);

dlsym(3) NAME dlsym -- get address of a symbolSYNOPSIS #include void* dlsym(void* handle, const char* symbol);

dlclose(3) NAME dlclose -- close a dynamic library or bundleSYNOPSIS #include int dlclose(void* handle);

dlerror(2) NAME dlerror -- get diagnostic informationSYNOPSIS #include const char* dlerror(void);

Display all Linked Library loaded export DYLD_PRINT_LIBRARIES=1 (or setenv DYLD_PRINT_LIBRARIES 1) start your app Start iTunes (/Applications/iTunes.app/Content/Mac OS/iTunes

Preferring static linking When give the choice (using -L and -l flag) the linker will prefer DYNAMIC linking If you want to force linking with static lib give the library name directly to the linker (give complete path to.a file just like another.o file)

What code runs before main?

What code runs before main() constructors to static/global objects. (PTHREAD_MUTEX_INITIALIZER) dll_main()/XXX_init.