标签:
一, GNU工具链简介:
(1)编译代码步骤:
预处理 -> 编译 -> 汇编 -> 链接;
预处理:去掉注释,进行宏替换,头文件包含等工作;
gcc -E test.c -o test.i
编译: 不同平台使用汇编语言不同,汇编将高级语言编译成汇编语言;
gcc -S test.c -o test.s
汇编: 将汇编语言翻译成二进制代码;
gcc -c test.c -o test.o
链接: 包含各函数库的入口,得到可执行文件;
gcc -o test test.c
(2)gcc编译:
.c文件编译:gcc -o DESFILE SRCFILE ,将源c文件SRCTILE编译为可执行文件DESFILE ;
gcc -o DESFILE SRCFILE ,加上-g选项,生成的可执行文件DESFILE可进行调试;
(2)gdb调试:
调试前提:gcc编译时加入-g参数;
调试选项:gdb DESFILE 进入调试,
l 列出代码信息,后面可带整数参数;
b 设断点,后面跟要设断点的函数名或者行数;
r 运行;
n 下一步;
c 继续;
q 退出,退出调试;
二,Linux下简单编程实例:
(1)进程创建:
函数说明:
#include <sys/types.h>
#include <unistd.h>
pid_t fork();
调用fork()创建进程,若创建成功,父进程返回子进程ID(>0),子进程返回0,出错返回-1;
实例:
1 #include <stdio.h> 2 #include <sys/types.h> 3 #include <unistd.h> 4 5 int main(void) 6 { 7 pid_t child_pid; 8 printf("the main program process ID is:%d",(int)getpid); 9 child_pid = fork(); 10 if(child_pid >0) 11 { 12 printf("This is parent process:%d\n",(int)getpid); 13 } 14 else if(pid == 0) 15 { 16 printf("This is child process:%d\n",(int)getpid); 17 } 18 else 19 { 20 printf("fork() occurr erro!\n"); 21 } 22 return 0; 23 }
1 #include <stdio.h> 2 #include <sys/types.h> 3 #include <unistd.h> 4 5 int spawn(char* progam,char** arg_list) 6 { 7 pid_t child_pid; 8 child_pid = fork(); 9 if(child_pid!=0) 10 { 11 return child_pid; 12 } 13 else 14 { 15 execvp(program,arg_list); 16 fprintf(stderr,"Erro occured!\n"); 17 abort(); 18 } 19 return 0; 20 } 21 22 int main(void) 23 { 24 char* arg_list[] = {"ls","-l","/",NULL}; 25 spawn("ls",arg_list); 26 return 0; 27 }
(2)线程创建:
函数说明:
int pthread_create(pthread_t *thread,const pthread_attr_t *attr,
void *(start_routine)(void *),void *arg);
第一个参数为线程ID;
第二个参数为线程属性,可默认为NULL;
第三个参数为线程执行的函数;
第四个为函数的参数;
实例:
线程创建:
1 #include <stdio.h> 2 #include <sys/types.h> 3 #include <pthread.h> 4 5 void* print_x(void* unused) 6 { 7 while(1) 8 { 9 printf("x"); 10 } 11 return NULL; 12 } 13 14 int main(void) 15 { 16 pthread_t thread_id; 17 pthread_create(&thread_id,NULL,&print_x,NULL); 18 while(1) 19 { 20 printf("s"); 21 } 22 return 0; 23 }
(3)信号:
signal:软件中断,传递给进程的异步消息;
当进程收到信号后,将立即对信号进行处理;
信号可以在任何代码位置中断;
实例:
1 #include <stdio.h> 2 #include <signal.h> 3 #include <sys/types.h> 4 #include <unistd.h> 5 6 void sig_usr(int signo) 7 { 8 printf("Receive signal !\n"); 9 } 10 11 int main(void) 12 { 13 printf("%d\n",(int)getpid()); 14 if(signal(SIGUSR1,sig_usr)==SIG_ERR) 15 { 16 printf("Can‘t catch signal"); 17 } 18 for(;;) 19 { 20 pause(); 21 } 22 return 0; 23 }
(4)线程,信号量
实例:
1 void* thread_fun(void* arg) 2 { 3 while(1) 4 { 5 pthread_mutex_lock(&thread_flag_mutex); 6 while(!thread_flag) 7 { 8 thread_cond_wait(&thread_flag_cv,&thread_flag_mutex); 9 } 10 pthread_mutex_unlock(); 11 do_work(); 12 } 13 return NULL; 14 } 15 16 void set_thread_flag(int flag_vlaue) 17 { 18 pthread_mutex_lock(&thread_flag_mutex); 19 thread_flag_mutex = flag_vlaue; 20 pthread_cond_signal(&thread_flag_cv); 21 pthread_mutex_unlock(&thread_flag_mutex); 22 }
(5)无名管道pipe:
函数原型: int pipe(int fd[2]);
生成两个文件描述符,一个用于读f[0],一个用于写f[1];
无名管道的使用条件:父子进程;
实例:
1 #include <unistd> 2 3 int main(void) 4 { 5 int fd[2]; 6 char buffer[80]; 7 pipe(fd); 8 if(fork()>0) 9 { 10 char s[] = "Hello!\n"; 11 write(fd[1],s,sizeof(s)); 12 } 13 else 14 { 15 read(fd[1],buffer,80); 16 printf("buffer: %s",buffer); 17 } 18 return 0; 19 }
(6)基于文件描述符的文件简单操作:
文件打开与关闭函数:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname,int flags);
int open(const char *pathname,int flags,mode_t mode);
int close(int fd);
flags类型:flags决定文件的打开方式,有以下常用类型:
O_RDONLY:只读; O_RDWR:读写;
O_WRONLY:只写; O_CREAT:若文件不存在则创建;
文件读写函数:
#include <unistd.h>
ssize_t read(int fd,void *buf,size_t count);
ssize_t write(int fdvoid *buf,size_t count);
文件的定位:
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd,off_t offset,int whence);
fd:文件注释符;
offset:偏移量;
whence:取值范围:SEEK_SET(将文件位移量设置为据文件开始出offset字节);
SEEK_CUR(将文件位移量设置为当前值+offset字节);
SEEK_END(将文件位移量设置为文件长度+offset字节);
实例:
1 #include <unistd.h> 2 #include <sys/types.h> 3 #include <sys/stat.h> 4 #include <fcntl.h> 5 6 int main(void) 7 { 8 char s[] = "Linux c program!\n" 9 char buffer[80]; 10 int fd,count; 11 fd = open("./test.txt",O_WRONLY|O_CREAT); 12 write(fd,s,sizeof(s)); 13 close(fd); 14 fd = open("./test.txt",O_RDONLY); 15 count = read(fd,buffer,80); 16 close(fd); 17 printf("buffer:%s",buffer); 18 return 0; 19 }
(7)socket客户端与服务器端:
socket相关函数:
客户端:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <sys/types.h> 5 #include <sys/socket.h> 6 #include <netinet/in.h> 7 #include <arpa/inet.h> 8 #include <unistd.h> 9 10 int main(void) 11 { 12 int sockfd; 13 char buffer[1024]; 14 struct sockaddr_in server_addr; 15 struct hostent *host; 16 int portnumber,nbytes; 17 if((host = gethostbyname("127.0.0.1"))==NULL) 18 { 19 fprintf(stderr,"Gethostname error!\n"); 20 exit(1); 21 } 22 sockfd = socket(AF_INET,SOCK_STREAM,0); 23 bzero(&server_addr,sizeof(server_addr)); 24 server_addr.sin_famiy = AF_INET; 25 server_addr.sin_port = htons(1180); 26 server_addr.sin_addr = ((struct in_addr*)host->h_addr); 27 nbytes = read(sockfd,buffer,1024); 28 buffer[nbytes] = ‘\0‘; 29 printf("I have received:%s\n",buffer); 30 close(sockfd); 31 return 0; 32 }
服务器端:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <sys/types.h> 4 #include <string.h> 5 #include <sys/socket.h> 6 #include <netinet.h> 7 #include <arpa.h> 8 #include <unistd.h> 9 10 int main(void) 11 { 12 int sockfd,new_fd; 13 struct sockaddr_in server_addr; 14 struct sockaddr_in client_addr; 15 in sin_size,portnumber; 16 char hello[] = "Hello !Are you Fine?\n"; 17 sockfd = socker(AF_INET,SOCK_STREAM,0); 18 bzero(&server_addr,sizeof(struct sockaddr_in)); 19 server_addr.sin_family = AF_INET; 20 server_addr.sin_addrs_addr = htonl(INADDR_ANY); 21 server_addr.sin_port = htons(1180); 22 bind(sockfd,(struct sockaddr*)(&server_addr),sizeof(struct sockaddr)); 23 listen(sockfd,5); 24 while(1) 25 { 26 sin_size = sizeof(struct sockaddr_in); 27 new_fd = accept(sockfd,(struct sockaddr*)(&clien_addr),&sin_size); 28 fprintf(stderr,"server get connection from %s\n",inet_ntoa(client_addr,sin_addr)); 29 write(new_fd,hello,strlen(hello)); 30 close(new_fd); 31 } 32 close(sockfd); 33 return 0; 34 }
Linux简单程序实例(GNU工具链,进程,线程,无名管道pipe,基于fd的文件操作,信号,scoket)
标签:
原文地址:http://www.cnblogs.com/weiyikang/p/5011715.html