unix_c++学习笔记

内容: 系统标识 时间与日期 多进程编程 1. 系统标识符 a.获得有关的系统信息 #include int uname( struct utsname * name);// 用 man uname 查看 struct utsname b.获得系统的名称 #include int gethostname( char* name, int namelen ); // 成功返回 0, 否则返回 1 例子: #include #include #include using namespace std; int main(){ cout<<"-------------- hostname ------------"< struct tm, localtime(),gmtime() // man localtime to see the struct tm cout<<"-----------struct tm-----------"< tm_year+1900 < tm_mon+1 < tm_mday < time_t cout<<"-----------mktime-----------"< char * cout<<"-----------ctime-----------"< char * cout<<"-----------asctime-----------"< formated char* cout<<"-----------strftime-----------"< int system( const char * string ); 执行string 所表示的命令,将产生一个新的进程,system为阻塞函数, 新的进程结束后才继续 例子: #include #include using namespace std; int main(){ cout<<"----------begin---------"< #include using namespace std; int main(){ char cmd[ 100 ]; memset( cmd, 0x00, sizeof( cmd ) ); while( 1 ){ cout<<"[irini@localhost]#"; cin.getline( cmd,sizeof( cmd ) ); if( strcmp( cmd,"bye" )==0 ) break; system( cmd ); } return 0; } 4. atexit() 函数 #include int atexit( void (*func) (void) ); 登记exit handler,最多可登记32个,在进程退出时最后登记的先调用,最先登记的最后调用 5. exit 与 _exit * 进程的退出过程: 进程做的事: exit handler( atexit注册的), 关闭IO流,如果申请了堆空间就释放 ------------------------------------------------------------------ kernel做的事: 销毁进程空间, 删除进程表中的相应项 * exit 是正常退出,想做进程的,然后进入kernel处理 _exit 是异常退出,直接进入kernel 例子: #include #include using namespace std; void fn1(){ cout<<"in fn1()..."< #include pid_t getpid(); // 当前进程号 pid_t getppid(); // 得到父进程号 例子: #include #include #include using namespace std; int main(){ cout<<"pid="< < #include #include using namespace std; void fn(){ cout<<"in fn()... "< #include pid_t wait( int * statloc ); pid_t waitpid( pid_t pid, int *static, int option ); 返回值为子进程的pid statloc 用于接受终止的子进程的返回状态 option 通常设为0 例子: #include #include #include #include using namespace std; int main(){ pid_t cid=fork(); if( cid==0 ){ sleep( 3 ); cout<<"[child] pid="< <<" ppid="< < 0 ){ cout<<"[father] pid="< <<" cid="< < #include using namespace std; int main(){ cout<<"-------------begin-----------"<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值