Linux C 学习: 学习笔记,不定期更新。
1、 Linux c main 函数传参
int main(int argc, char * argv[])
{
if(argc > 1)
{
printf("argv = %s\n",argv[1]);
}
}
执行:
./a.out acanoe
结果为:argv = acanoe .
依次类推 后面再添加的参数 将被定义为 argv[2] argv[3] 等等。 /************** c 基础 ****************************/
2、Linux char 型转 int 型函数 atoi( ) 的应用。
紧接上面的例子
int main(int argc, char * argv[])
{
if(argc > 2)
{
printf("argv = %s\n",argv[1]);
printf("atoi test is %d\n",atoi(argv[2]));
}
}
执行 ./a.out acanoe 123
结果为:
argv = acanoe
atoi test is 123
3、 sprintf 应用 / * 使用 在open 一个文件的使用, 传参的目录不是完整的,你就可以用sprintf ,有利于你自己定义一个环境目录 */
紧接上面的例子:
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char * argv[])
{
char dirfile[32];
int fd;
if(argc > 2)
{
memset(dirfile,0,sizeof(dirfile));
printf("argv = %s\n",argv[1]);
printf("atoi test is %d\n",atoi(argv[2]));
sprintf(dirfile,"%s/test.c",argv[3]);
printf("dirfile is %s\n",dirfile);
fd = open(dirfile,O_RDWR,0777);
if(fd < 0){
printf("open %s is erroe\n",dirfile);
}
}
}
执行:./a.out acanoe 123 /work
argv = atoi test is 123dirfile is /work/test.c
4、使用 #if $ 和 #endif 可以更好的注销和使用你的函数。使函数的功能变得更加灵活。
紧接上面的例子:
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char * argv[])
{
char dirfile[32];
int fd;
if(argc > 2)
{
memset(dirfile,0,sizeof(dirfile));
printf("argv = %s\n",argv[1]);
printf("atoi test is %d\n",atoi(argv[2]));
#if 1
sprintf(dirfile,"%s/test.c",argv[3]);
printf("dirfile is %s\n",dirfile);
fd = open(dirfile,O_RDWR,0777);
if(fd < 0){
printf("open %s is erroe\n",dirfile);
}
#endif
}
}
编译后的执行./a.out acanoe 123 /work 结果 和 例3 一致 , 而
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char * argv[])
{
char dirfile[32];
int fd;
if(argc > 2)
{
memset(dirfile,0,sizeof(dirfile));
printf("argv = %s\n",argv[1]);
printf("atoi test is %d\n",atoi(argv[2]));
#if 0
sprintf(dirfile,"%s/test.c",argv[3]);
printf("dirfile is %s\n",dirfile);
fd = open(dirfile,O_RDWR,0777);
if(fd < 0){
printf("open %s is erroe\n",dirfile);
}
#endif
}
}
的执行./a.out acanoe 123 /work 结果为
argv = acanoe
atoi test is 123
5、 Linux 下多线程任务, 线程锁 mutex 线程条件 cond 的使用
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t mutex;
pthread_cond_t c1cond;
pthread_cond_t c2cond;
int times = 0;
void * child1(void *arg)
{
// pthread_cleanup_push(pthread_mutex_unlock,&mutex); /* comment 1 */
printf("thread 1 get running \n");
while(1){
// printf("thread 1 pthread_mutex_lock returns %d\n",
pthread_mutex_lock(&mutex);
pthread_cond_wait(&c1cond,&mutex);
printf("\n");
printf("thread 1 condition applied thread1 times is %d\n",times++);
printf("\n");
pthread_mutex_unlock(&mutex);
//sleep(2);
}
// pthread_cleanup_pop(0); /* comment 2 */
}
void *child2(void *arg)
{
printf("thread 2 get running.\n");
while(1){
//printf("thread 2 pthread_mutex_lock returns %d\n",
pthread_mutex_lock(&mutex);
pthread_cond_wait(&c2cond,&mutex);
printf("\n");
printf("thread 2 condition applied thread2 times is %d\n",times++);
printf("\n");
pthread_mutex_unlock(&mutex);
//sleep(2);
}
}
int main(void)
{
pthread_t tid1,tid2;
printf("hello, condition variable test\n");
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(&c1cond,NULL);
pthread_cond_init(&c2cond,NULL);
pthread_create(&tid1,NULL,child1,NULL);
pthread_create(&tid2,NULL,child2,NULL);
while(1){
pthread_cond_signal(&c1cond);
sleep(2); /* comment 6 */
pthread_cond_signal(&c2cond);
}
pthread_exit(0);
}
编译使用 gcc pthread.c -lpthread
执行 ./a.out 可线程一线程 二 交替执行!
6、 将一块数据使用对齐的方式强制转换成 一个结构体
#include <stdio.h>
#include <string.h>
typedef struct __blogsinfo{
char name[6];
char sex[3];
char age[2];
char hobby[10];
}blogsinfo_t;
typedef struct __personinfo {
char name[6];
char sex[3];
char age[2];
char hobby[10];
}personinfo_t;
int main()
{
char data[1024] = "acanoe";
char *sex = "man";
char *age = "22";
char *hobby = "Linux c";
strcat(data,sex);
strcat(data,age);
strcat(data,hobby);
personinfo_t personinfo;
printf("%s \n",data);
memset(&personinfo,0,sizeof(personinfo_t));
blogsinfo_t *blogsinfo=(blogsinfo_t *)data;
memcpy(personinfo.name, blogsinfo->name, 6);
printf("personinfo_name is %s\n",personinfo.name);
memcpy(personinfo.sex, blogsinfo->sex, 3);
printf("personinfo_sex is %s\n",personinfo.sex);
memcpy(personinfo.age , blogsinfo->age,2);
printf("personinfo_age is %s\n",personinfo.age);
memcpy(personinfo.hobby, blogsinfo->hobby, 10);
printf("personinfo_hobby is %s\n",personinfo.hobby);
}
将数据组包优化以后 : 程序为:
#include <stdio.h>
#include <string.h>
typedef struct senddate{
char name[6];
char sex[3];
int age;
char hobby[10];
char birthday[10];
}senddate_t;
typedef struct __blogsinfo{
char name[6];
char sex[3];
int age;
char hobby[10];
char birthday[10];
}blogsinfo_t;
typedef struct __personinfo {
char name[6];
char sex[3];
int age;
char hobby[10];
char birthday[10];
}personinfo_t;
int main()
{
char data[1024];
struct senddate date;
memcpy(date.name,"acanoe",strlen("acanoe"));
memcpy(date.sex,"man",strlen("man"));
date.age = 22;
memcpy(date.hobby,"Linux_c",strlen("Linux_c"));
memcpy(date.birthday ,"19900305",strlen("19900305"));
memcpy(data,&date,sizeof(senddate_t));
personinfo_t personinfo;
memset(&personinfo,0,sizeof(personinfo_t));
blogsinfo_t *blogsinfo=(blogsinfo_t *)data;
memcpy(personinfo.name, blogsinfo->name, 6);
printf("personinfo_name is %s\n",personinfo.name);
memcpy(personinfo.sex, blogsinfo->sex, 3);
printf("personinfo_sex is %s\n",personinfo.sex);
personinfo.age = blogsinfo->age;
printf("personinfo_age is %d\n",personinfo.age);
memcpy(personinfo.hobby, blogsinfo->hobby, 10);
printf("personinfo_hobby is %s\n",personinfo.hobby);
memcpy(personinfo.birthday, blogsinfo->birthday, 10);
printf("personinfo_birthday is %s\n",personinfo.birthday);
}
7、 定义一个固定时间执行一次的函数,可以巧用 gettimeofday( ) 函数。
#include <stdio.h>
#include <sys/time.h>
static struct timeval newtimeof = {-1, -1};
static struct timeval oldtimeof = {-1, -1};
void main(int argc,char *argv[])
{
int limit;
limit = atoi(argv[1]);
while(1)
{
gettimeofday(&newtimeof, NULL);
if ((newtimeof.tv_sec * 1000 + newtimeof.tv_usec/1000)-(oldtimeof.tv_sec * 1000 + oldtimeof.tv_usec/1000) > limit * 1000) {
printf("\n");
printf("the time is up\n");
printf("\n");
oldtimeof.tv_sec= newtimeof.tv_sec;
oldtimeof.tv_usec= newtimeof.tv_usec;
}
printf("not in time\n");
sleep(1);
}
}
执行 ./a.out 6
就会每隔6 s 执行一次 printf( " the time is up \n");
这种调用比 sleep(6); 语句相比最大的好处是,占用系统资源少,gettimeofday() 函数仅仅是取了一个系统时间戳, 对比两次的时间戳,来达到延时的目的。
第二个优点是可以延时相当长的时间,1分钟,或者一个小时等。 缺点是 延时时间不够精准。