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

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

If you can't read please download the document

Upload: anthony-shoumikhin

Post on 16-Apr-2017

629 views

Category:

Technology


0 download

TRANSCRIPT

Linux

man !

$ man hello
HELLO(1) User Commands HELLO(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

GCC = + + +

cppcc1, cc1plus, ...ascollect2

$ cat hello.c

#include

int main()
{
printf("Hello World\n");
}


$ gcc -o hello hello.c

$ ./hello

Hello World

$ cat hello.c

#include

int main()
{
printf("Hello World\n")
}


$ gcc -o hello hello.c

hello.c: In function 'main': hello.c:7: error: syntax error before '}' token

$ cat main.c

int main()
{
print_hello();
}

$ cat hello.c

#include

void print_hello()
{
printf("Hello World\n");
}

$ 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

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

$ 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

$ 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

void openlog(char *ident, int option,
int facility);

void syslog(int priority, char *format, );

void closelog();

#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();
};

$ 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]

$ cat hello.h

void h_world();
void g_world();

$ cat hello.c

#include
#include "hello.h"

void h_world()
{
printf("Hello World\n");
}

void g_world()
{
printf ("Goodbye World\n");
}

$ cat main.c

#include "hello.h"

int main()
{
h_world();
g_world();
}

$ gcc -c main.c

$ gcc -c hello.c

$ ar cr libhello.a hello.o

$ gcc -o hello main.o -L. -lhello

$ ar t libhello.a
hello.o

$ ar x libhello.a hello.o

$ gcc -c -fPIC -o hello.o hello.c

$ gcc -shared -o libhello.so hello.o

$ gcc -o hello -L. -lhello main.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
...

$ 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

void *dlopen(char const *filename, int flag);

void *dlsym(void *handle, char *symbol);

dlclose(void *handle);

-dl

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

USER -
HOME -
PATH - , , ""
PWD -
OLDPWD -
SHELL -
HOSTNAME -
QTDIR - QT
LD_LIBRARY_PATH - ""
LANG -
DISPLAY - X11

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

-

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

#include
#include
#include // read(), write(), close() #include // open(), O_RDONLY
#include //S_IRUSR
#include // mode_t

#define BUFFER_SIZE64

int main(int argc, char ** argv)
{
int fd;
ssize_t read_bytes;
ssize_t written_bytes;
char buffer[BUFFER_SIZE];

if (argc < 2)
{
fprintf(stderr, "Too few arguments\n");
exit (1);
}

fd = open(argv[1], O_RDONLY);

if (fd < 0)
{
fprintf(stderr, "Cannot open file\n");
exit (1);
}

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 write\n");
exit (1);
}
}

if (read_bytes < 0)
{
fprintf(stderr, "myread: Cannot read file\n");
exit (1);
}

close (fd);

return 0;
}

pid_t getpid(); //PID

pid_t getppid(); //PID

pid_t fork(); //

int execve(char const *path, char * const argv[], char *const envp[]); //

#include
#include

int main()
{
char *echo_args[] = { "echo", "child", NULL};

if (fork())
printf("parent");
else
execve("/bin/echo", echo_args, environ);

return 0;
}

IPC

int pipe(int pipefd[2]); //

int mkfifo(char const *pathname,
mode_t mode); //

#include
#include
#include

int main (int argc, char * argv[])
{
int pipedes[2];
pid_t pid;

pipe(pipedes);
pid = fork();

if (pid > 0)
{
char *str = "String passed via pipe\n";

close(pipedes[0]);
write(pipedes[1], (void *)str, strlen(str) + 1);
close(pipedes[1]);
}

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

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

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

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


#include

void handler(int i)
{
printf("Terminating\n");
exit(EXIT_FAILURE);
}

int main(int argc, char *argv[])
{
signal(SIGSEGV, handler);
kill(getpid(), SIGSEGV);
}

SIGINT ,
SIGKILL ,
SIGTERM
SIGSEGV
SIGCHLD
SIGSTOP
SIGCONT
SIGUSR1

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

#include
#include

void *thread(void *arg)
{
printf("Thread %i is running\n", *(int *)arg);
}


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

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

IDE


QtCreator
Code::Blocks
Eclipse
Vim
Emacs
NetBeans
MonoDevelop
...

Muokkaa otsikon tekstimuotoa napsauttamalla

Muokkaa jsennyksen tekstimuotoa napsauttamallaToinen jsennystasoKolmas jsennystasoNeljs jsennystasoViides jsennystasoKuudes jsennystasoSeitsems jsennystasoKahdeksas jsennystasoYhdekss jsennystaso

Muokkaa otsikon tekstimuotoa napsauttamalla

Muokkaa jsennyksen tekstimuotoa napsauttamallaToinen jsennystasoKolmas jsennystasoNeljs jsennystasoViides jsennystasoKuudes jsennystasoSeitsems jsennystasoKahdeksas jsennystasoYhdekss jsennystaso

Muokkaa otsikon tekstimuotoa napsauttamalla

Muokkaa jsennyksen tekstimuotoa napsauttamallaToinen jsennystasoKolmas jsennystasoNeljs jsennystasoViides jsennystasoKuudes jsennystasoSeitsems jsennystasoKahdeksas jsennystasoYhdekss jsennystaso

Muokkaa otsikon tekstimuotoa napsauttamalla

Muokkaa jsennyksen tekstimuotoa napsauttamallaToinen jsennystasoKolmas jsennystasoNeljs jsennystasoViides jsennystasoKuudes jsennystasoSeitsems jsennystasoKahdeksas jsennystasoYhdekss jsennystaso

Muokkaa otsikon tekstimuotoa napsauttamalla

Muokkaa jsennyksen tekstimuotoa napsauttamallaToinen jsennystasoKolmas jsennystasoNeljs jsennystasoViides jsennystasoKuudes jsennystasoSeitsems jsennystasoKahdeksas jsennystasoYhdekss jsennystaso

Muokkaa otsikon tekstimuotoa napsauttamalla

Muokkaa jsennyksen tekstimuotoa napsauttamallaToinen jsennystasoKolmas jsennystasoNeljs jsennystasoViides jsennystasoKuudes jsennystasoSeitsems jsennystasoKahdeksas jsennystasoYhdekss jsennystaso