c questions a great programmer codes excellent code in c and java. the code does video decoding....

32
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?

Upload: beverly-norman

Post on 04-Jan-2016

217 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 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?

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?

Page 2: 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?

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;

}

Page 3: 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?

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;

• }

Page 4: 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?

More lkmpg

Page 5: 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?

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

Page 7: 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?

sysfs - structure

Internal External

Kernel ObjectsDirectories

Object Attributes Regular Files

Object Relationships

Symbolic Links

Page 8: 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?

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 attribute attr; 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;}

Page 9: 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?

Example

review : http://docs.blackfin.uclinux.org/doku.php?id=kernel_objects#kernel_object_sysfs_example

Page 10: 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?

Working with /proc

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

Page 11: 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?

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)

Page 12: 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?

Chapter 9 - blocking processes

We can cause a process to block by calling wait_event_interruptible

Page 13: 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?

wait_event_interruptible

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

Page 14: 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?

blocking process - partial examplewhile (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. */

Page 15: 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?

work queues

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

Page 16: 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?

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);

Page 17: 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?

And a little bit about linking

Static libs

Dynamic libs

Linker configuration

Page 18: 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?

Static libs

static libraries are created using ar(1)

we can create library using ar crv

Page 19: 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?

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<path> and -lmylib.a

use nm(1) for names

Page 20: 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?

dylib

use libtool -dynamic -o <output> *.o

default extension on the mac is .dylib

default on linux is so

default on windows is .dll

Page 21: 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?

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

Page 22: 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?

Striping binaries

we can remove symbols using strip(1) command

This will prevent debugging

We can also save striped info to file

Page 23: 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?

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

Page 24: 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?

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)

Page 25: 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?

dlopen(2)

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

Page 26: 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?

dlsym(3)

NAME dlsym -- get address of a symbolSYNOPSIS #include <dlfcn.h> void* dlsym(void* handle, const char* symbol);

Page 27: 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?

dlclose(3)

NAME dlclose -- close a dynamic library or bundleSYNOPSIS #include <dlfcn.h> int dlclose(void* handle);

Page 28: 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?

dlerror(2)

NAME dlerror -- get diagnostic informationSYNOPSIS #include <dlfcn.h> const char* dlerror(void);

Page 29: 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?

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/MacOS/iTunes

Page 30: 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?

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)

Page 31: 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?

What code runs before main?

Page 32: 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?

What code runs before main()

constructors to static/global objects. (PTHREAD_MUTEX_INITIALIZER)

dll_main()/XXX_init.