Linuxアプリケーションプログラミング
ハローワールド
http://blog.chinaunix.net/uid-26833883-id-3215592.html
http://www.ibm.com/developerworks/cn/linux/1307_liuming_linuxtime 1/
タイムの使用
http://www.hzlitai.com.cn/article/ARM9-article/example/1518.html
サウンドカードのDSPデバイスを利用して音声の録音と再生の基本的なフレームワークを行い、その機能はまず数秒のオーディオデータを記録し、メモリバッファに保存してから再生します。そのすべての機能は読み書き/dev/dspデバイスファイルを通して完成されます。
#include <stdio.h>
int main(int argc, char **argv)
{
printf("Hello world, this is my first app!\r
");
return 0;
}
コマンドラインパラメータ処理関数http://blog.chinaunix.net/uid-26833883-id-3215592.html
#include <stdio.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
int ch;
while((ch = getopt(argc,argv,"cyg")) != -1)
{
switch(ch)
{
case 'c':
printf("optind = %d
",optind);
printf("Option character \'c\'.
");
break;
case 'y':
printf("optind = %d
",optind);
printf("Option character \'y\'.
");
break;
case 'g':
printf("optind = %d
",optind);
printf("Option character \'g\'.
");
break;
}
}
printf("Ending...
");
printf("optind = %d
",optind);
return 0;
}
#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.
");
return -1;
}
switch(option_index)
{
case 0:
printf("Long option is : %s
",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("
");
break;
case 2:
printf("Long option is : %s ",long_option[option_index].name);
if(optarg)
{
printf("with parm '%s'",optarg);
}
printf("
");
break;
case 3:
printf("Long option is : %s
",long_option[option_index].name);
break;
}
printf("flag_value = %d
",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
",getpid());
else
printf("I am the parent process,ID is %d
",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
",count);
return 0;
}
シグナル処理#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
int value = 0;
void func(int sig)
{
printf("I get a signal!
");
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 10
void err_exit(const char *err_msg)
{
printf("error:%s
", err_msg);
exit(1);
}
/* */
void signal_handler(int signum)
{
if (signum == SIGSEGV)
printf("
SIGSEGV handler!!!
");
else if (signum == SIGBUS)
printf("
SIGBUS handler!!!
");
exit(1);
}
int main(int argc, const char *argv[])
{
if (argc < 2)
{
printf("usage:%s text
", 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
", 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
", addr);
/* */
//addr += sys_pagesize + 1;
//printf("out of range:%s
", 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
", addr);
/* */
if (munmap(addr, MMAP_FILE_SIZE) == -1)
err_exit("munmap()");
return 0;
}
時間プログラミングhttp://www.ibm.com/developerworks/cn/linux/1307_liuming_linuxtime 1/
タイムの使用
#include <time.h>
int main ()
{
time_t time_raw_format;
time ( &time_raw_format ); //
printf (" time is [%d]
", time_raw_format);
// ctime
printf ( "The current local time: %s", ctime(&time_raw_format));
return 0;
}
時間表示切り替えint main ()
{
time_t time_raw_format;
struct tm * time_struct;
char buf [100];
time ( &time_raw_format );
time_struct = localtime ( &time_raw_format );
strftime (buf,100,"It is now: %I:%M%p.",time_struct);
puts (buf);
return 0;
}
gettimeoff dayvoid 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
",timeuse);
exit(0);
}
RDT SCunsigned 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 MHz
if (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
", (c2 - c1) / mhz);
}
}
タイマーvoid print_info(int signo)
{
printf(“timer fired
”); // , 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デバイスファイルを通して完成されます。
/*
* sound.c
*/
#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 arg; /* ioctl */
int status; /* */
/* */
fd = open("/dev/dsp", O_RDWR);
if (fd < 0) {
perror("open of /dev/dsp 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");
/* , Control-C */
while (1) {
printf("Say something:
");
status = read(fd, buf, sizeof(buf)); /* */
if (status != sizeof(buf))
perror("read wrong number of bytes");
printf("You said:
");
status = write(fd, buf, sizeof(buf)); /* */
if (status != sizeof(buf))
perror("wrote wrong number of bytes");
/* */
status = ioctl(fd, SOUND_PCM_SYNC, 0);
if (status == -1)
perror("SOUND_PCM_SYNC ioctl failed");
}
}
ミキサーをプログラミングする基本的なフレームワークは、様々なミキサーのゲインを調整することができます。そのすべての機能は読み書き/dev/mixerデバイスファイルを通して完成されます。/*
* mixer.c
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/soundcard.h>
/* */
const char *sound_device_names[] = SOUND_DEVICE_NAMES;
int fd; /* */
int devmask, stereodevs; /* */
char *name;
/* */
void usage()
{
int i;
fprintf(stderr, "usage: %s <device> <left-gain%%> <right-gain%%>
"
" %s <device> <gain%%>
"
"Where <device> is one of:
", name, name);
for (i = 0 ; i < SOUND_MIXER_NRDEVICES ; i )
if ((1 << i) & devmask) /* */
fprintf(stderr, "%s ", sound_device_names[i]);
fprintf(stderr, "
");
exit(1);
}
int main(int argc, char *argv[])
{
int left, right, level; /* */
int status; /* */
int device; /* */
char *dev; /* */
int i;
name = argv[0];
/* */
fd = open("/dev/mixer", O_RDONLY);
if (fd == -1) {
perror("unable to open /dev/mixer");
exit(1);
}
/* */
status = ioctl(fd, SOUND_MIXER_READ_DEVMASK, &devmask);
if (status == -1)
perror("SOUND_MIXER_READ_DEVMASK ioctl failed");
status = ioctl(fd, SOUND_MIXER_READ_STEREODEVS, &stereodevs);
if (status == -1)
perror("SOUND_MIXER_READ_STEREODEVS ioctl failed");
/* */
if (argc != 3 && argc != 4)
usage();
/* */
dev = argv[1];
/* */
for (i = 0 ; i < SOUND_MIXER_NRDEVICES ; i )
if (((1 << i) & devmask) && !strcmp(dev, sound_device_names[i]))
break;
if (i == SOUND_MIXER_NRDEVICES) { /* */
fprintf(stderr, "%s is not a valid mixer device
", dev);
usage();
}
/* */
device = i;
/* */
if (argc == 4) {
/* 、 */
left = atoi(argv[2]);
right = atoi(argv[3]);
} else {
/* 、 */
left = atoi(argv[2]);
right = atoi(argv[2]);
}
/* */
if ((left != right) && !((1 << i) & stereodevs)) {
fprintf(stderr, "warning: %s is not a stereo device
", dev);
}
/* */
level = (right << 8) left;
/* */
status = ioctl(fd, MIXER_WRITE(device), &level);
if (status == -1) {
perror("MIXER_WRITE ioctl failed");
exit(1);
}
/* */
left = level & 0xff;
right = (level & 0xff00) >> 8;
/* */
fprintf(stderr, "%s gain set to %d%% / %d%%
", dev, left, right);
/* */
close(fd);
return 0;
}
テストに合格したオーディオ再生プログラム#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;
}