Linux 应用编程
生活随笔
收集整理的這篇文章主要介紹了
Linux 应用编程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
hello world
#include <stdio.h> int main(int argc, char **argv) { printf("Hello world, this is my first app!\r\n"); return 0; }命令行參數處理函數
http://blog.chinaunix.net/uid-26833883-id-3215592.html
#include <stdio.h> #include <unistd.h> #include <getopt.h>int main(int argc,char *argv[]) {int flag_value = 100;while(1){int option_index = 0;int rvalue = 0;static struct option long_option[] = {{"help0",no_argument,0,0},{"help1",required_argument,0,0},{"help2",optional_argument,0,0},{"help3",no_argument,0,10},{0,0,0,0},};long_option[3].flag = &flag_value;rvalue = getopt_long(argc,argv,"a:bc::",long_option,&option_index);if(rvalue == -1){printf("No more argument.\n");return -1;}switch(option_index){case 0:printf("Long option is : %s\n",long_option[option_index].name);break;case 1:printf("Long option is : %s ",long_option[option_index].name);if(optarg){printf("with parm '%s'",optarg);}printf("\n");break;case 2:printf("Long option is : %s ",long_option[option_index].name);if(optarg){printf("with parm '%s'",optarg);}printf("\n");break;case 3:printf("Long option is : %s\n",long_option[option_index].name);break;}printf("flag_value = %d\n",flag_value);}return 0; }
fork與vfork的區別
#include <sys/types.h> #include <sys/types.h> #include <unistd.h> #include <stdio.h> int main() {pid_t pid;pid=fork();if(pid<0)printf("error in fork!");else if(pid==0)printf("I am the child process,ID is %d\n",getpid());elseprintf("I am the parent process,ID is %d\n",getpid()); }
#include <unistd.h> #include <stdio.h> #include <sys/types.h> int main(void) {pid_t pid;int count=0;pid=vfork();if(pid==0){count++;_exit(0);} else {count++; } printf("count= %d\n",count); return 0; }
信號處理 #include <stdio.h> #include <stdlib.h> #include <signal.h> int value = 0; void func(int sig) { printf("I get a signal!\n"); value = 1; } int main() { signal(SIGINT, func); while(0 == value) sleep(1); return 0; }
文件映射 /*** @file mmap_file.c*/#include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <signal.h> #include <unistd.h> #include <sys/mman.h>#define MMAP_FILE_NAME "a.txt" #define MMAP_FILE_SIZE 10void err_exit(const char *err_msg) {printf("error:%s\n", err_msg);exit(1); }/* 信號處理器 */ void signal_handler(int signum) {if (signum == SIGSEGV)printf("\nSIGSEGV handler!!!\n");else if (signum == SIGBUS)printf("\nSIGBUS handler!!!\n");exit(1); }int main(int argc, const char *argv[]) {if (argc < 2){printf("usage:%s text\n", argv[0]);exit(1);}char *addr;int file_fd, text_len;long int sys_pagesize;/* 設置信號處理器 */if (signal(SIGSEGV, signal_handler) == SIG_ERR)err_exit("signal()");if (signal(SIGBUS, signal_handler) == SIG_ERR)err_exit("signal()");if ((file_fd = open(MMAP_FILE_NAME, O_RDWR)) == -1)err_exit("open()");/* 系統分頁大小 */sys_pagesize = sysconf(_SC_PAGESIZE);printf("sys_pagesize:%ld\n", sys_pagesize);/* 內存只讀 *///addr = (char *)mmap(NULL, MMAP_FILE_SIZE, PROT_READ, MAP_SHARED, file_fd, 0);/* 映射大于文件長度,且大于該文件分頁大小 *///addr = (char *)mmap(NULL, sys_pagesize + 1, PROT_READ | PROT_WRITE, MAP_SHARED, file_fd, 0);/* 正常分配 */addr = (char *)mmap(NULL, MMAP_FILE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, file_fd, 0);if (addr == MAP_FAILED)err_exit("mmap()");/* 原始數據 */printf("old text:%s\n", addr);/* 越界訪問 *///addr += sys_pagesize + 1;//printf("out of range:%s\n", addr);/* 拷貝新數據 */text_len = strlen(argv[1]);memcpy(addr, argv[1], text_len);/* 同步映射區數據 *///if (msync(addr, text_len, MS_SYNC) == -1)// err_exit("msync()");/* 打印新數據 */printf("new text:%s\n", addr);/* 解除映射區域 */if (munmap(addr, MMAP_FILE_SIZE) == -1)err_exit("munmap()");return 0; }
時間編程
http://www.ibm.com/developerworks/cn/linux/1307_liuming_linuxtime1/
time的使用
#include <time.h> int main () {time_t time_raw_format;time ( &time_raw_format ); //獲取當前時間printf (" time is [%d]\n", time_raw_format);//用 ctime 將時間轉換為字符串輸出printf ( "The current local time: %s", ctime(&time_raw_format));return 0; }
gettimeofday void function() { unsigned int i,j; double y; for(i=0;i<1000;i++) for(j=0;j<1000;j++) y=sin((double)i); //耗時操作 } main() { struct timeval tpstart,tpend; float timeuse; gettimeofday(&tpstart,NULL); //記錄開始時間戳function(); gettimeofday(&tpend,NULL); //記錄結束時間戳timeuse = 1000000*(tpend.tv_sec-tpstart.tv_sec)+ tpend.tv_usec-tpstart.tv_usec; //計算差值timeuse /= 1000000; printf("Used Time:%f\n",timeuse); exit(0); }
RDTSC unsigned long long get_cycles() {unsigned low, high;unsigned long long val;rdtsc(low,high);val = high; val = (val << 32) | low; //將 low 和 high 合成一個 64 位值return val; }double get_cpu_mhz(void) {FILE* f;char buf[256];double mhz = 0.0;f = fopen("/proc/cpuinfo","r"); //打開 proc/cpuinfo 文件if (!f)return 0.0;while(fgets(buf, sizeof(buf), f)) {double m;int rc; rc = sscanf(buf, "cpu MHz : %lf", &m); //讀取 cpu MHzif (mhz == 0.0) {mhz = m;break;}}fclose(f); return mhz; //返回 HZ 值 }int main() {double mhz;mhz = get_cpu_mhz();cycles_t c1, c2;for(;;){c1 = get_cycles(); sleep(1);c2 = get_cycles();//c2 和 c1 的差值應該為 1000000us,即 1 秒printf("1 sec = %g usec\n", (c2 - c1) / mhz); } }
定時器 void print_info(int signo) { printf(“timer fired\n”); //簡單的打印,表示 timer 到期 } void init_sigaction(void) { struct sigaction act; act.sa_handler= print_info; act.sa_flags=0; sigemptyset(&act.sa_mask); sigaction(SIGPROF,&act,NULL); //設置信號 SIGPROF 的處理函數為 print_info } void init_time() { struct itimerval value; value.it_value.tv_sec=2; value.it_value.tv_usec=0; value.it_interval=value.it_value; setitimer(ITIMER_PROF,&value,NULL); //初始化 timer,到期發送 SIGPROF 信號 } int main() { len=strlen(prompt); init_sigaction(); init_time(); while(1); exit(0); }
音頻編程
http://www.hzlitai.com.cn/article/ARM9-article/example/1518.html
一個利用聲卡上的DSP設備進行聲音錄制和回放的基本框架,它的功能是先錄制幾秒種音頻數據,將其存放在內存緩沖區中,然后再進行回放,其所有的功能都是通過讀寫/dev/dsp設備文件來完成的
一個對混音器進行編程的基本框架,利用它可以對各種混音通道的增益進行調節,其所有的功能都是通過讀寫/dev/mixer設備文件來完成的
一個測試通過的音頻播放程序
#include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <sys/ioctl.h> #include <stdlib.h> #include <stdio.h> #include <linux/soundcard.h>#define LENGTH 3 /* 存儲秒數 */ #define RATE 8000 /* 采樣頻率 */ #define SIZE 8 /* 量化位數 */ #define CHANNELS 1 /* 聲道數目 *//* 用于保存數字音頻數據的內存緩沖區 */ unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];int main() {int fd;/* 聲音設備的文件描述符 */ int id; /*聲音輸出文件描述符*/ int arg;/* 用于ioctl調用的參數 */ int status; /* 系統調用的返回值 */ int i; int j;/* 打開聲音設備 */ fd = open("/dev/dsp", O_RDWR);//arm下的音頻文件是"/dev/sound/dsp"; if (fd < 0) { perror("open of /dev/dsp failed"); exit(1); } /*打開輸出文件*/ id=open("Music.wav",O_RDWR); if(id < 0){ perror("open of sound file failed"); exit(1); } /* 設置采樣時的量化位數 */ arg = SIZE; status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg); if (status == -1) perror("SOUND_PCM_WRITE_BITS ioctl failed"); if (arg != SIZE) perror("unable to set sample size");/* 設置采樣時的聲道數目 */ arg = CHANNELS; status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg); if (status == -1) perror("SOUND_PCM_WRITE_CHANNELS ioctl failed"); if (arg != CHANNELS) perror("unable to set number of channels");/* 設置采樣時的采樣頻率 */ arg = RATE; status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg); if (status == -1) perror("SOUND_PCM_WRITE_WRITE ioctl failed");/* 讀取一定數量的音頻數據,并將之寫到輸出文件中去 */ for(i=0;i<10;i ){ j=read(id, buf, sizeof(buf)); if (j > 0){ write(fd, buf, j); /* 放音 */ } }/* 關閉輸入、輸出文件 */ close(fd); close(id); return 1; }
總結
以上是生活随笔為你收集整理的Linux 应用编程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux内核技术文章
- 下一篇: 一些常用黑客工具的初步使用