ºìÁªLinuxÃÅ»§
Linux°ïÖú

ÔÚLinux²Ù×÷ϵͳÖбà³ÌÉùÒôÉ豸ʵÀý

·¢²¼Ê±¼ä:2006-11-21 09:38:06À´Ô´:ºìÁª×÷Õß:flycocoon
¡¡¡¡LinuxϵÄÉùÒôÉ豸±à³Ì±È´ó¶àÊýÈËÏëÏóµÄÒª¼òµ¥µÃ¶à¡£Ò»°ã˵À´£¬ÎÒÃdz£ÓõÄÉùÒôÉ豸ÊÇÄÚ²¿ÑïÉùÆ÷ºÍÉù¿¨£¬ËüÃǶ¼¶ÔÓ¦/devĿ¼ÏµÄÒ»¸ö»ò¶à¸öÉ豸Îļþ£¬ÎÒÃÇÏó´ò¿ªÆÕͨÎļþÒ»Ñù´ò¿ªËüÃÇ£¬ÓÃioctl£¨£©º¯ÊýÉèÖÃһЩ²ÎÊý£¬È»ºó¶ÔÕâЩ´ò¿ªµÄÌØÊâÎļþ½øд²Ù×÷¡£

¡¡¡¡ÓÉÓÚÕâЩÎļþ²»ÊÇÆÕͨµÄÎļþ£¬ËùÒÔÎÒÃDz»ÄÜÓÃANSI C£¨±ê×¼C£©µÄfopen¡¢fcloseµÈÀ´²Ù×÷Îļþ£¬¶øÓ¦¸ÃʹÓÃϵͳÎļþI/O´¦Àíº¯Êý£¨open¡¢read¡¢write¡¢lseekºÍclose£©À´´¦ÀíÕâЩÉ豸Îļþ¡£ioctl£¨£©»òÐíÊÇLinuxÏÂ×îÅÓÔӵĺ¯Êý£¬Ëü¿ÉÒÔ¿ØÖƸ÷ÖÖÎļþµÄÊôÐÔ£¬ÔÚLinuxÉùÒôÉ豸±à³ÌÖУ¬×îÖØÒªµÄ¾ÍÊÇʹÓô˺¯ÊýÕýÈ·ÉèÖñØÒªµÄ²ÎÊý¡£

¡¡¡¡ÏÂÃæÎÒÃǾÙÁ½¸öʵ¼ÊµÄÀý×ÓÀ´ËµÃ÷ÈçºÎʵÏÖLinuxϵÄÉùÒô±à³Ì¡£ÓÉÓÚ´ËÀà±à³ÌÉæ¼°µ½ÏµÍ³É豸µÄ¶Áд£¬ËùÒÔ£¬ºÜ¶àʱºòÐèÒªÄãÓÐrootȨÏÞ£¬Èç¹ûÄ㽫ÏÂÃæµÄÀý×Ó±àÒëºó²»ÄÜÕýÈ·Ö´ÐУ¬ÄÇô£¬Ê×ÏÈÇëÄã¼ì²éÊÇ·ñÊÇÒòΪûÓвÙ×Ýij¸öÉ豸µÄȨÏÞ¡£

¡¡¡¡¶ÔÄÚ²¿ÑïÉùÆ÷±à³ÌÄÚ²¿ÑïÉùÆ÷ÊÇ¿ØÖÆ̨µÄÒ»²¿·Ö£¬ËùÒÔËü¶ÔÓ¦µÄÉ豸ÎļþΪ/dev/console¡£±äÁ¿KIOCSOUNDÔÚÍ·Îļþ /usr /include /linux /kd.hÖÐÉùÃ÷£¬ioctlº¯ÊýʹÓÃËü¿ÉÒÔÀ´¿ØÖÆÑïÉùÆ÷µÄ·¢Éù£¬Ê¹ÓùæÔòΪ£º

ÒýÓÃ:
¡¡¡¡ioctl ( fd, KIOCSOUND, (int) tone);


¡¡¡¡fdΪÎļþÉ豸ºÅ£¬tone ÊÇÒôƵֵ¡£µ±toneΪ0ʱ£¬ÖÕÖ¹·¢Éù¡£±ØÐëÒ»ÌáµÄÊÇËüËùÀí½âµÄÒôƵºÍÎÒÃÇƽ³£ÒÔΪµÄÒôƵÊDz»Í¬µÄ£¬ÓÉÓÚ¼ÆËã»úÖ÷°å¶¨Ê±Æ÷µÄʱÖÓƵÂÊΪ1.19MHZ£¬ËùÒÔÒª½øÐÐÕýÈ·µÄ·¢Éù£¬±ØÐë½øÐÐÈçϵÄת»»£ºÑïÉùÆ÷ÒôƵֵ=1190000/ÎÒÃÇÆÚÍûµÄÒôƵֵ¡£

¡¡¡¡ÑïÉùÆ÷·¢Éùʱ¼äµÄ³¤¶ÌÎÒÃÇͨ¹ýº¯Êýusleep£¨unsigned long usec£©À´¿ØÖÆ¡£ËüÊÇÔÚÍ·Îļþ/usr /include /unistd.hÖж¨ÒåµÄ£¬ÈóÌÐò˯Ãßusec΢Ãë¡£ÏÂÃæ¼´ÊÇÈÃÑïÉùÆ÷°´Ö¸¶¨µÄ³¤¶ÈºÍÒôƵ·¢ÉùµÄ³ÌÐòµÄÍêÕûÇåµ¥£º

ÒýÓÃ:
#include < fcntl.h >
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
#include < unistd.h >
#include < sys/ioctl.h >
#include < sys/types.h >
#include < linux/kd.h >

/* É趨ĬÈÏÖµ */
#define DEFAULT_FREQ 440 /* É趨һ¸öºÏÊʵÄƵÂÊ */
#define DEFAULT_LENGTH 200 /* 200 ΢Ã룬·¢ÉùµÄ³¤¶ÈÊÇÒÔ΢ÃëΪµ¥Î»µÄ*/
#define DEFAULT_REPS 1 /* ĬÈϲ»Öظ´·¢Éù */
#define DEFAULT_DELAY 100 /* ͬÑùÒÔ΢ÃëΪµ¥Î»*/

/* ¶¨ÒåÒ»¸ö½á¹¹£¬´æ´¢ËùÐèµÄÊý¾Ý*/
typedef struct {
int freq; /* ÎÒÃÇÆÚÍûÊä³öµÄƵÂÊ£¬µ¥Î»ÎªHz */
int length; /* ·¢Éù³¤¶È£¬ÒÔ΢ÃëΪµ¥Î»*/
int reps; /* Öظ´µÄ´ÎÊý*/
int delay; /* Á½´Î·¢Éù¼ä¸ô£¬ÒÔ΢ÃëΪµ¥Î»*/
} beep_parms_t;


/* ´òÓ¡°ïÖúÐÅÏ¢²¢Í˳ö*/
void usage_bail ( const char *executable_name ) {
printf ( "Usage: \n \t%s [-f frequency] [-l length] [-r reps] [-d delay] \n ",
executable_name );
exit(1);
}

/ * ·ÖÎöÔËÐвÎÊý£¬¸÷ÏîÒâÒåÈçÏ£º
* "-f <ÒÔHZΪµ¥Î»µÄƵÂÊÖµ >"
* "-l <ÒÔºÁÃëΪµ¥Î»µÄ·¢Éùʱ³¤ >"
* "-r <Öظ´´ÎÊý >"
* "-d <ÒÔºÁÃëΪµ¥Î»µÄ¼äЪʱ³¤ >"
*/
void parse_command_line(char **argv, beep_parms_t *result) {
char *arg0 = *(argv++);
while ( *argv ) {
if ( !strcmp( *argv,"-f" )) { /*ƵÂÊ*/
int freq = atoi ( *( ++argv ) );
if ( ( freq <= 0 ) | | ( freq > 10000 ) ) {
fprintf ( stderr, "Bad parameter: frequency must be from 1..10000\n" );
exit (1) ;
} else {
result->freq = freq;
argv++;
}
} else if ( ! strcmp ( *argv, "-l" ) ) { /*ʱ³¤*/
int length = atoi ( *(++argv ) );
if (length < 0) {
fprintf(stderr, "Bad parameter: length must be >= 0\n");
exit(1);
} else {
result->length = length;
argv++;
}
} else if (!strcmp(*argv, "-r")) { /*Öظ´´ÎÊý*/
int reps = atoi(*(++argv));
if (reps < 0) {
fprintf(stderr, "Bad parameter: reps must be >= 0\n");
exit(1);
} else {
result->reps = reps;
argv++;
}
} else if (!strcmp(*argv, "-d")) { /* ÑÓʱ */
int delay = atoi(*(++argv));
if (delay < 0) {
fprintf(stderr, "Bad parameter: delay must be >= 0\n");
exit(1);
} else {
result->delay = delay;
argv++;
}
} else {
fprintf(stderr, "Bad parameter: %s\n", *argv);
usage_bail(arg0);
}
}
}

int main(int argc, char **argv) {
int console_fd;
int i; /* Ñ­»·¼ÆÊýÆ÷ */
/* Éè·¢Éù²ÎÊýΪĬÈÏÖµ*/
beep_parms_t parms = {DEFAULT_FREQ, DEFAULT_LENGTH, DEFAULT_REPS,
DEFAULT_DELAY};
/* ·ÖÎö²ÎÊý£¬¿ÉÄܵĻ°¸üз¢Éù²ÎÊý*/
parse_command_line(argv, &parms);

/* ´ò¿ª¿ØÖÆ̨£¬Ê§°ÜÔò½áÊø³ÌÐò*/
if ( ( console_fd = open ( "/dev/console", O_WRONLY ) ) == -1 ) {
fprintf(stderr, "Failed to open console.\n");
perror("open");
exit(1);
}

/* ÕæÕý¿ªÊ¼ÈÃÑïÉùÆ÷·¢Éù*/
for (i = 0; i < parms.reps; i++) {
/* Êý×Ö1190000´ÓºÎ¶øÀ´£¬²»µÃ¶øÖª*/
int magical_fairy_number = 1190000/parms.freq;

ioctl(console_fd, KIOCSOUND, magical_fairy_number); /* ¿ªÊ¼·¢Éù */
usleep(1000*parms.length); /*µÈ´ý... */
ioctl(console_fd, KIOCSOUND, 0); /* Í£Ö¹·¢Éù*/
usleep(1000*parms.delay); /* µÈ´ý... */
} /* Öظ´²¥·Å*/
return EXIT_SUCCESS;
}


¡¡¡¡½«ÉÏÃæµÄÀý×ÓÉÔ×÷À©Õ¹£¬Óû§¼´¿ÉÒÔÈÃÑïÉùÆ÷³ª¸è¡£Ö»ÒªÕÒµ½ÎåÏßÆ×»ò¼òÆ×µÄÒô½×¡¢Òô³¤¡¢½ÚÅĺÍƵÂÊ¡¢·¢Éùʱ³¤¡¢¼ä¸ôµÄ¶ÔÓ¦¹Øϵ¾Í¿ÉÒÔÁË¡£ÎÒÏÖÔÚ»¹¼ÇµÃÒÔÇ°ÔÚDOSϱàд³ö¡¶ÊÀÉÏÖ»ÓÐÂèÂèºÃ¡·Ê±µÄÐË·Ü¡£×îºó£¬ËµÒ»Ð©ÌáÍâ»°£¬ÕâÆäʵÊÇÒ»¸öºÜ¼òµ¥µÄ³ÌÐò£¬µ«ÊÇÎÒÃÇÈ´ÓÃÁ˺ܳ¤µÄƪ·ù£¬Ï£Íû¶ÁÕß´ÓÒÔÉϵĴúÂëÀïÄÜÌå»áµ½Ð´ºÃµÄ³ÌÐòµÄһЩ·½·¨£¬»òÐí×îÖØÒªµÄÊÇÌí¼Ó×¢ÊÍ°É¡£Ò»¸ö³ÌÐòµÄ×¢ÊÍÓÀÔ¶²»»áÏӶ࣬¼´±ãÄãдµÄʱºò¾õµÃËü¸ù±¾ÊǶàÓ࣬µ«ÏàÐÅÎÒ£¬ÏàÐÅÔøÕâÑù¸æËßÎÒÃǵÄÐí¶àÓÅÐãµÄ³ÌÐòÔ±£ºÑø³ÉдºÜ¶à×¢Ê͵ÄÏ°¹ß¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 2236 ÌõÆÀÂÛ

  1. 72.232.63.* ÓÚ 2007-02-21 19:29:33·¢±í:

    Great work!
    mtv ringtones | composer motorola free ringtones