Linux编程常用
来源:互联网 发布:淘宝装修图片上传 编辑:程序博客网 时间:2024/06/07 05:34
gcc命令
警告:
1、-pedantic 选项,那么使用了扩展语法的地方将产生相应的警告信息
2、-Wall 使用它能够使GCC产生尽可能多的警告信息
3、-Werror,它要求GCC将所有的警告当成错误进行处理
eclipse
清除所有断点:打开debug视图,即可清除所有断点。
获取时间
#include <sys/time.h> Linux系统的日期时间头文件。比如:
structtimeval tv;gettimeofday(&tv,NULL);ftime()。
#include <time.h> C/C++中的日期和时间头文件。比如:char strtime[1024] = {0};
time_t now;
struct tm *ptm;
time(&now);
ptm = localtime(&now);
sprintf(strtime, "%d%02d%02d%02d%02d%02d", ptm->tm_year+1900,ptm->tm_mon+1,ptm->tm_mday,ptm->tm_hour,ptm->tm_min,ptm->tm_sec);
获取当前文件夹
#include <stdio.h>
#include <unistd.h>
//头文件在unix下是unistd.h,在TC2.0下是dir.h,在vc6.0定义在direct.h的头文件,VS2008下是direct.h,应该依编程者的环境而定
#define MAXPATH 256
int main(void)
{
char buffer[MAXPATH];
getcwd(buffer,MAXPATH);
printf("The current directoryis:%s\n",buffer);
return 0;
}
开机后台运行
可以通过增加一个(&)符号,将应用程序在后台启动。如:/path/to/yourprogram &
监控狗
如下,每隔5秒检查CardPlatform进程是否存在,不存在,就开启。
#!/bin/sh
echo "enter shell" >> /usr/local/zf/CardPlatform/log
while true;do
count=`ps -e | grep CardPlatform|grep -v grep`
if [ "$?" != "0" ];then
echo "start " >> /usr/local/zf/CardPlatform/log
echo ">>>>CardPlatform isnot runing..."
/usr/local/zf/CardPlatform/CardPlatform
else
echo "running " >> /usr/local/zf/CardPlatform/log
echo ">>>>CardPlatformis runing..."
fi
sleep 5
done
打印堆栈
#include <execinfo.h>
FILE* fp = fopen("/home/3.txt","ab");
if(fp)
{
void *bt[20];
char **strings;
size_t sz;
sz = backtrace(bt, 20);
strings = backtrace_symbols(bt, sz);
for(int i = 0; i < sz; ++i)
fprintf(fp,"%s\n", strings[i]);
fclose(fp);
}
遍历文件夹
#include <sys/types.h>
#include <dirent.h>
void listDir(char *path) {
DIR *pDir;
struct dirent *ent;
char childpath[512];
pDir = opendir(path);
memset(childpath, 0, sizeof(childpath));
while ((ent = readdir(pDir)) != NULL) {
if (ent->d_type & DT_DIR) {
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
continue;
sprintf(childpath, "%s/%s", path, ent->d_name);
printf("path:%s/n", childpath);
listDir(childpath);
} else {
printf("%s\n",ent->d_name);
}
}
}
int main(int argc,char *argv[])
{
listDir(argv[1]);
return 0;
}
获取APP安装路径
int Getappfulldir(const char* papp, char* pfull) {
int nrst = 0;
char filepath[256] = { 0 };
sprintf(filepath, "/proc/%d", getpid());
if (-1 != chdir(filepath)) {
char cmd[256] = { 0 };
snprintf(cmd, 256, "which %s", papp);
FILE* fp = NULL;
if (NULL != (fp = popen(cmd, "r"))) {
char path[256] = { 0 };
if ( NULL != fgets(path, sizeof(path) / sizeof(path[0]), fp)) {
strcpy(pfull, path);
nrst = 1;
}
pclose(fp);
}
}
return nrst;
}
删除文件
#include <dirent.h>
remove(pb);
signal( SIGCHLD, SIG_DFL )
因为system函数中,使用到了fork(),waitpid.如果父进程忽略了SIGCHID信号,waitpid就没有不能得到子进程的SIGCHLD信号,那么,处理的返回值就会有问题。system的返回值也会有问题。通常的做法是:
signal( SIGCHLD, SIG_DFL );
system( command );
signal( SIGCHLD, SIG_IGN );
setsid
当进程是会话的领头进程时setsid()调用失败并返回(-1)。setsid()调用成功后,返回新的会话的ID,调用setsid函数的进程成为新的会话的领头进程,并与其父进程的会话组和进程组脱离。由于会话对控制终端的独占性,进程同时与控制终端脱离。
pid_t pid = fork(); //fork a process
if (pid < 0) exit(0); //fork error
if (pid > 0) exit(0); //father process exit
setsid();[1] //creat a new session for a process
//之前parent和child运行在同一个session里,parent是会话(session)的领头进程,
//parent进程作为会话的领头进程,如果exit结束执行的话,那么子进程会成为孤儿进程,并被init收养。
//执行setsid()之后,child将重新获得一个新的会话(session)id。
//这时parent退出之后,将不会影响到child了。
- linux常用编程命令
- Linux - 编程常用函数
- Linux编程常用
- linux编程常用函数
- Linux网络编程常用函数
- Linux网络编程常用函数
- Linux网络编程常用函数
- Linux网络编程常用函数
- Linux编程常用的函数
- Linux网络编程常用函数
- LINUX编程常用的函数
- Linux编程常用的函数
- linux编程常用包含文件
- linux编程常用函数说明
- Linux编程常用的函数
- linux时间编程常用函数
- linux socket 编程 常用函数
- linux编程常用函数接口
- Java中类的初始化顺序
- PHP 面向对象
- 数据库事务隔离级别
- Android 真机调试 LogCat不显示VERBOSE、DEBUG的日志信息
- 39.和为S的两个数字
- Linux编程常用
- 数据存储之文件存储
- Matrix Factorization: A Simple Tutorial and Implementation in Python
- Android学习笔记-常见报错
- 名字的漂亮度
- 编译Linux/Android 输出很多信息很难找到错误/警告信息怎么办?
- 蓝桥杯:Torry的困惑(基本型)
- 虫洞-引力波
- 生活琐碎笔记