新聞中心
怎么用C語(yǔ)言實(shí)現(xiàn)linux的命令
命令是查詢當(dāng)前登錄的每個(gè)用戶,它的輸出包括用戶名、終端類型、登錄日期及遠(yuǎn)程主機(jī),在Linux系統(tǒng)中輸入who命令輸出如下:
創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),湖口網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:湖口等地區(qū)。湖口做網(wǎng)站價(jià)格咨詢:18982081108
我們先man一下who,在幫助文檔里可以看到,who命令是讀取/var/run/utmp文件來得到以上信息的。
我們?cè)賛an一下utmp,知道utmp這個(gè)文件,是二進(jìn)制文件,里面保存的是結(jié)構(gòu)體數(shù)組,這些數(shù)組是struct utmp結(jié)構(gòu)體的。
struct utmp {
short ut_type;
pid_t ut_pid;
char ut_line[UT_LINESIZE];
char ut_id[4];
char ut_user[UT_NAMESIZE];
char ut_host[UT_HOSTSIZE];
struct {
int32_t tv_sec;
int32_t tv_usec;
} ut_tv;
/***等等***/
};
要實(shí)現(xiàn)who只需要把utmp文件的所有結(jié)構(gòu)體掃描過一遍,把需要的信息顯示出來就可以了,我們需要的信息有ut_user、ut_line、ut_tv、ut_host。
老師給的初始代碼:who1.c運(yùn)行結(jié)果如下:
需要注意的是utmp中所保存的時(shí)間是以秒和微妙來計(jì)算的,所以我們需要把這個(gè)時(shí)間轉(zhuǎn)換為我們能看懂的時(shí)間,利用命令man -k time | grep 3搜索C語(yǔ)言中和時(shí)間相關(guān)的函數(shù):
經(jīng)過搜索發(fā)現(xiàn)了一個(gè)ctime()函數(shù),似乎可以滿足我們的需求,于是對(duì)代碼中關(guān)于時(shí)間的printf進(jìn)行修改:
printf("%s",ctime(utbufp-ut_time));
編譯運(yùn)行發(fā)現(xiàn)出來的結(jié)果雖然已經(jīng)轉(zhuǎn)換成了我們能看懂的時(shí)間格式,但是很明顯這個(gè)時(shí)間是錯(cuò)的:
搜索一下ut_time這個(gè)宏,發(fā)現(xiàn)它被定義為int32_t類型:
但是ctime()函數(shù)中要求參數(shù)的類型是time_t類型,所以重新定義一下類型,編譯運(yùn)行之后,發(fā)現(xiàn)時(shí)間已經(jīng)改成了正確的,但是發(fā)現(xiàn)()中的內(nèi)容被換行了,猜想ctime()函數(shù)的返回值可能自動(dòng)在最后補(bǔ)了一個(gè)字符\n:
一開始想通過\r\b來實(shí)現(xiàn)“退行”,但實(shí)踐后發(fā)現(xiàn)并不可取,最后考慮到直接修改字符串中最后一個(gè)字符為\0,讓其字符串結(jié)束,使輸出達(dá)到與系統(tǒng)who命令一樣的效果,即在輸出語(yǔ)句前添加如下代碼:
cp[strlen(cp)-1] = '\0'
最后編譯執(zhí)行效果,發(fā)現(xiàn)解決了該問題:
雖然能看出基本上和who指令的執(zhí)行結(jié)果一致,但是并非完全一樣,主要在兩點(diǎn),第一是時(shí)間格式不一樣,第二個(gè)是比who執(zhí)行的結(jié)果多了幾條,需要注意的是utmp中保存的用戶,不僅僅是已經(jīng)登陸的用戶,還有系統(tǒng)的其他服務(wù)所需要的“用戶”,所以在顯出所有登陸用戶的時(shí)候,應(yīng)該過濾掉其他用戶,只保留登陸用戶。我們可以通過ut_type來區(qū)別,登陸用戶的ut_type是USER_PROCESS。
先用if語(yǔ)句對(duì)執(zhí)行結(jié)果進(jìn)行過濾,效果如下:
接著解決時(shí)間格式問題,利用man命令收到了兩個(gè)非常有用的函數(shù):localtime()和strftime(),localtime()是把從1970-1-1零點(diǎn)零分到當(dāng)前時(shí)間系統(tǒng)所偏移的秒數(shù)時(shí)間轉(zhuǎn)換為本地時(shí)間,strftime()則是用來定義時(shí)間格式的,如:年-月-日,利用這兩個(gè)函數(shù)對(duì)時(shí)間進(jìn)行修改后,結(jié)果顯示終于和系統(tǒng)中who命令一模一樣:
最終完整的代碼如下:
#include stdio.h
#include stdlib.h
#include utmp.h
#include fcntl.h
#include unistd.h
#include time.h
#define SHOWHOST
void show_time(long timeval){
char format_time[40];
struct tm *cp;
cp = localtime(timeval);
strftime(format_time,40,"%F %R",cp);
printf("%s",format_time);
}
int show_info( struct utmp *utbufp )
{
if(utbufp-ut_type == USER_PROCESS){
printf("%-8.8s", utbufp-ut_name);
printf(" ");
printf("%-8.8s", utbufp-ut_line);
printf(" ");
show_time(utbufp-ut_time);
printf(" ");
#ifdef SHOWHOST
printf("(%s)", utbufp-ut_host);
#endif
printf("\n");
}
return 0;
}
int main()
{
struct utmp current_record;
int utmpfd;
int reclen = sizeof(current_record);
if ( (utmpfd = open(UTMP_FILE, O_RDONLY)) == -1 ){
perror( UTMP_FILE );
exit(1);
}
while ( read(utmpfd, current_record, reclen) == reclen )
show_info(current_record);
close(utmpfd);
return 0;
}
怎樣用c語(yǔ)言使linux系統(tǒng)關(guān)機(jī)
把system里邊的命令改成"shutdown -r now"看看....
沒Linux系統(tǒng),無法測(cè)試
寫一個(gè)linux下寫個(gè)關(guān)于c語(yǔ)言的雙守護(hù)進(jìn)程,就是監(jiān)視一個(gè)進(jìn)程,當(dāng)其死掉,馬上將其重啟
可以分三步來做:
做兩個(gè)簡(jiǎn)單的守護(hù)進(jìn)程,并能正常運(yùn)行
監(jiān)控進(jìn)程是否在運(yùn)行
啟動(dòng)進(jìn)程
綜合起來就可以了,代碼如下:
被監(jiān)控進(jìn)程thisisatest.c(來自):
#includeunistd.h
#includesignal.h
#includestdio.h
#includestdlib.h
#includesys/param.h
#includesys/types.h
#includesys/stat.h
#includetime.h
void init_daemon()
{
int pid;
int i;
pid=fork();
if(pid0) ?
exit(1); ?//創(chuàng)建錯(cuò)誤,退出
else if(pid0) //父進(jìn)程退出
exit(0);
setsid(); //使子進(jìn)程成為組長(zhǎng)
pid=fork();
if(pid0)
exit(0); //再次退出,使進(jìn)程不是組長(zhǎng),這樣進(jìn)程就不會(huì)打開控制終端
else if(pid0) ?
exit(1);
//關(guān)閉進(jìn)程打開的文件句柄
for(i=0;iNOFILE;i++)
close(i);
chdir("/root/test"); ?//改變目錄
umask(0);//重設(shè)文件創(chuàng)建的掩碼
return;
}
void main()
{
FILE *fp;
time_t t;
init_daemon();
while(1)
{
? sleep(60); //等待一分鐘再寫入
? fp=fopen("testfork2.log","a");
? if(fp=0)
? {
? ? ? time(t);
? ? ? fprintf(fp,"current time is:%s\n",asctime(localtime(t))); ?//轉(zhuǎn)換為本地時(shí)間輸出
? ? ? fclose(fp);
? }
}
return;
}
監(jiān)控進(jìn)程monitor.c:
#includeunistd.h
#includesignal.h
#includestdio.h
#includestdlib.h
#includesys/param.h
#includesys/types.h
#includesys/stat.h
#includetime.h
#includesys/wait.h
#includefcntl.h
#includelimits.h
#define BUFSZ 150
void init_daemon()
{
int pid;
int i;
pid=fork();
if(pid0)
exit(1); ?//創(chuàng)建錯(cuò)誤,退出
else if(pid0) //父進(jìn)程退出
exit(0);
setsid(); //使子進(jìn)程成為組長(zhǎng)
pid=fork();
if(pid0)
exit(0); //再次退出,使進(jìn)程不是組長(zhǎng),這樣進(jìn)程就不會(huì)打開控制終端
else if(pid0)
exit(1);
//關(guān)閉進(jìn)程打開的文件句柄
for(i=0;iNOFILE;i++)
close(i);
chdir("/root/test"); ?//改變目錄
umask(0);//重設(shè)文件創(chuàng)建的掩碼
return;
}
void err_quit(char *msg)
{
perror(msg);
exit(EXIT_FAILURE);
}
// 判斷程序是否在運(yùn)行
int does_service_work()
{
FILE* fp;
int count;
char buf[BUFSZ];
char command[150];
sprintf(command, "ps -ef | grep thisisatest | grep -v grep | wc -l" );
if((fp = popen(command,"r")) == NULL)
err_quit("popen");
if( (fgets(buf,BUFSZ,fp))!= NULL )
{
count = atoi(buf);
}
pclose(fp);
return count;
// exit(EXIT_SUCCESS);
}
void main()
{
FILE *fp;
time_t t;
int count;
init_daemon();
while(1)
{
? sleep(10); //等待一分鐘再寫入
? fp=fopen("testfork3.log","a");
? if(fp=0)
? {
? ? ? count = does_service_work();
? ? ? time(t);
? ? ? if(count0)
? ? ? ? ? fprintf(fp,"current time is:%s and the process exists, the count is %d\n",asctime(localtime(t)), count); ?//轉(zhuǎn)換為本地時(shí)間輸出
? ? ? else
? ? ? {
? ? ? ? ? fprintf(fp,"current time is:%s and the process does not exist, restart it!\n",asctime(localtime(t))); ?//轉(zhuǎn)換為本地時(shí)間輸出
? ? ? ? ? system("/home/user/daemon/thisisatest"); //啟動(dòng)服務(wù)
? ? ? }
? ? ? fclose(fp);
? }
}
return;
}
具體CMD命令:
cc thisisatest.c -o thisisatest
./thisisatest
cc monitor.c -o monitor
./monitor
tail -f testfork3.log ? -- 查看日志
c語(yǔ)言中 重啟電腦的命令是什么
重啟:shutdown -r
關(guān)機(jī):shutdown -h
其他命令參數(shù)如下。
Usage: shutdown [-i | -l | -s | -r | -a] [-f] [-m \\computername] [-t xx] [-c "c
omment"] [-d up:xx:yy]
No args Display this message (same as -?)
-i Display GUI interface, must be the first option
-l Log off (cannot be used with -m option)
-s Shutdown the computer
-r Shutdown and restart the computer
-a Abort a system shutdown
-m \\computername Remote computer to shutdown/restart/abort
-t xx Set timeout for shutdown to xx seconds
-c "comment" Shutdown comment (maximum of 127 characters)
-f Forces running applications to close without war
ning
-d [u][p]:xx:yy The reason code for the shutdown
u is the user code
p is a planned shutdown code
xx is the major reason code (positive integer le
ss than 256)
yy is the minor reason code (positive integer le
ss than 65536)
標(biāo)題名稱:c語(yǔ)言重啟命令linux c++重啟程序
網(wǎng)頁(yè)地址:http://www.dlmjj.cn/article/hhsdch.html