Программирование linux

Download Программирование Linux

If you can't read please download the document

Upload: anthony-shoumikhin

Post on 22-Jun-2015

403 views

Category:

Technology


2 download

TRANSCRIPT

  • 1. Linux

2. man ! 3. $ man hello HELLO(1)User CommandsHELLO(1) NAME hello - friendly greeting program SYNOPSIS hello [OPTION]... DESCRIPTION Print a friendly, customizable greeting. -h, --help display this help and exit -v, --version display version information and exit -t, --traditional use traditional greeting format -n, --next-generation use next-generation greeting format -g, --greeting=TEXT use TEXT as the greeting message 4. 5. GCC = + + + cpp cc1, cc1plus, ... as collect2 6. $ cat hello.c #include int main() { printf("Hello Worldn"); } $ gcc -o hello hello.c $ ./hello Hello World 7. $ cat hello.c #include int main() { printf("Hello Worldn") } $ gcc -o hello hello.c hello.c: In function 'main': hello.c:7: error: syntax error before '}' token 8. $ cat main.c int main() { print_hello(); } $ cat hello.c #include void print_hello() { printf("Hello Worldn"); } 9. $ gcc -c main.c $ gcc -c hello.c $ ls hello.c hello.o main.c main.o $ gcc -o hello main.o hello.o $ ls hello* hello.c hello.o main.c main.o$ ./hello Hello World 10. $ gcc -o hello hello.c main.c -Wall main.c: In function main: main.c:1:1: warning: implicit declaration of function print_hello main.c:1:1: warning: control reaches end of non-void function$ nm hello.o 00000000 T print_hello U puts $ ldd hello linux-vdso.so.1 =>(0x00110000) libc.so.6 => /lib/libc.so.6 (0x00a57000) /lib/ld-linux.so.2 (0x00a38000) 11. $ cat Makefile hello: main.o hello.o gcc -o hello main.o hello.o main.o: main.c gcc -c main.c hello.o: hello.c gcc -c hello.c clean: rm -f *.o hello http://habrahabr.ru/blogs/development/111691 12. $ make gcc -c main.c gcc -c hello.c gcc -o hello main.o hello.o $ ls hello hello.c hello.o main.c main.o Makefile 13. 14. void openlog(char *ident, int option, int facility); void syslog(int priority, char *format, ); void closelog(); 15. #include int main() { int i; openlog("test", LOG_PID, LOG_USER); syslog(LOG_DEBUG,"try to sending 5 messages"); for (i = 0; i < 5; ++i) syslog(LOG_INFO,"info message [i = %i]",i); closelog(); }; 16. $ tail /var/log/debug Dec 20 11:25:04 linux test[6222]: try to sending 5 messages $ tail /var/log/messages Dec 20 11:25:04 linux test[6222]: info message [i = 0] Dec 20 11:25:04 linux test[6222]: info message [i = 1] Dec 20 11:25:04 linux test[6222]: info message [i = 2] Dec 20 11:25:04 linux test[6222]: info message [i = 3] Dec 20 11:25:04 linux test[6222]: info message [i = 4] 17. 18. $ cat hello.h void h_world(); void g_world(); $ cat hello.c #include #include "hello.h" void h_world() { printf("Hello Worldn"); } void g_world() { printf ("Goodbye Worldn"); } 19. $ cat main.c #include "hello.h" int main() { h_world(); g_world(); } 20. $ gcc -c main.c $ gcc -c hello.c $ ar cr libhello.a hello.o $ gcc -o hello main.o -L. -lhello $ ar t libhello.ahello.o $ ar x libhello.a hello.o 21. $ gcc -c -fPIC -o hello.o hello.c $ gcc -shared -o libhello.so hello.o $ gcc -o hello -L. -lhellomain.c $ ./hello./hello: error while loading shared libraries: libhello.so: cannot open shared object file: No such file or directory $ ldd hello ... libhello.so => not found ... 22. $ gcc -o hello -L. -lhello -Wl,-rpath,. main.o $ ./hello Hello World Goodbye World $ ldd hello libhello.so => ./libhello.so (0x00a57000) $ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PWD 23. void *dlopen(char const *filename, int flag); void *dlsym(void *handle, char *symbol); dlclose(void *handle); -dl 24. #include double pow(double x, double y); int main() { void *library;double (*power)(double, double);library = dlopen("/lib/libmath.so",RTLD_LAZY); if (!library) return dlerror(); power = dlsym(library, pow); dlclose(library); }; 25. 26. USER - HOME - PATH - , , "" PWD - OLDPWD - SHELL - HOSTNAME - QTDIR - QT LD_LIBRARY_PATH - "" LANG - DISPLAY - X11 27. extern char ** environ;//declared in unistd.h char *getenv(char const *name); int setenv(char const *name, char const *value,int overwrite); int unsetenv(char const *name); 28. - 29. int open(char const* filename, int flags); int close(int fd); ssize_t read(int fd, void *buffer, size_t count); ssize_t write(int fd, const void * buffer, size_t count); off_t lseek(int fd, ott_t offset, int against); 30. #include #include #include // read(), write(), close() #include // open(), O_RDONLY #include //S_IRUSR #include // mode_t #define BUFFER_SIZE 64 int main(int argc, char ** argv) { int fd; ssize_t read_bytes; ssize_t written_bytes; char buffer[BUFFER_SIZE]; 31. if (argc < 2) { fprintf(stderr, "Too few argumentsn"); exit (1); } fd = open(argv[1], O_RDONLY); if (fd < 0) { fprintf(stderr, "Cannot open filen"); exit (1); } 32. while ((read_bytes = read(fd, buffer,BUFFER_SIZE)) > 0) { written_bytes = write(FILENO(stdout),buffer, read_bytes); if (written_bytes != read_bytes) { fprintf (stderr, "Cannot writen"); exit (1); } } 33. if (read_bytes < 0) { fprintf(stderr, "myread: Cannot readfilen"); exit (1); } close (fd); return 0; } 34. 35. pid_t getpid();//PID pid_t getppid();//PID pid_t fork();// int execve(char const *path, char * const argv[],char *const envp[]);// 36. #include #include int main() { char *echo_args[] = { "echo", "child", NULL}; if (fork()) printf("parent"); else execve("/bin/echo", echo_args, environ); return 0; } 37. IPC 38. int pipe(int pipefd[2]);// int mkfifo(char const *pathname, mode_t mode);// 39. #include #include #include int main (int argc, char * argv[]) { int pipedes[2]; pid_t pid; pipe(pipedes); pid = fork(); 40. if (pid > 0) { char *str = "String passed via pipen"; close(pipedes[0]); write(pipedes[1], (void *)str,strlen(str) + 1); close(pipedes[1]); } 41. else { char buf[1024]; int len; close(pipedes[1]); while ((len = read(pipedes[0], buf,1024)) != 0) write(2, buf, len); close(pipedes[0]); } return 0; } 42. 43. int socket(int domain, int type, int protocol); int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); int listen(int sockfd, int backlog); int accept(int sockfd, struct sockaddr *addr,socklen_t *addrlen); struct hostent *gethostbyname(char const *name); 44. sock = socket(AF_INET, SOCK_STREAM, 0); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(port); bind(sock, (struct sockaddr *)&serv_addr,sizeof(serv_addr); listen(sock, 1); new_sock = accept(sock, (struct sockaddr *)&new_addr, &new_len); 45. server = gethostbyname(hostname); serv_addr.sin_family = AF_INET; memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length); serv_addr.sin_port = htons(port); connect(sock, &serv_addr, sizeof(serv_addr); 46. 47. #include void handler(int i) { printf("Terminatingn"); exit(EXIT_FAILURE); } int main(int argc, char *argv[]) { signal(SIGSEGV, handler); kill(getpid(), SIGSEGV); } 48. SIGINT , SIGKILL , SIGTERM SIGSEGV SIGCHLD SIGSTOP SIGCONT SIGUSR1 49. 50. int pthread_create(pthread_t *thread, pthread_attr_t const *attr, void *(*start_routine) (void *), void *arg); int pthread_join(pthread_t thread, void **retval); void pthread_exit(void *retval); int pthread_cancel(pthread_t thread); -pthread 51. #include #include void *thread(void *arg) { printf("Thread %i is runningn", *(int *)arg); } 52. int main(int argc, char * argv[]) { int id1 = 1, id2 = 2; pthread_t thread1, thread2; pthread_create(&thread1, NULL, thread, &id1); pthread_create(&thread2, NULL, thread, &id2); pthread_join(thread1, NULL); pthread_join(thread2, NULL); return errno; } 53. 54. pthread_mutex_t pthread_mutex_lock(mutex) pthread_mutex_trylock(mutex) pthread_mutex_unlock(mutex) pthread_cond_t pthread_cond_wait(condition, mutex) pthread_cond_signal(condition) pthread_cond_broadcast(condition) pthread_rwlock_t pthread_barrier_t 55. IDE 56. QtCreator Code::Blocks Eclipse Vim Emacs NetBeans MonoDevelop ...