當前位置:首頁 » 編程語言 » linuxc語言進程是否存在

linuxc語言進程是否存在

發布時間: 2023-06-12 07:09:17

『壹』 在linux系統中,如何運行一個c語言程序

1、打開kali linux的終端。創建一個文件並命名為test.c。在終端輸入:touch test.c。

『貳』 如何在Linux下用c語言創建守護進程並監控系統運行期間的所有進程

可以分三步來做:


  1. 做兩個簡單的守護進程,並能正常運行

  2. 監控進程是否在運行

  3. 啟動進程


綜合起來就可以了,代碼如下:
被監控進程thisisatest.c(來自):
#include<unistd.h>
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/param.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<time.h>


void init_daemon()
{
int pid;
int i;
pid=fork();
if(pid<0)
exit(1); //創建錯誤,退出
else if(pid>0) //父進程退出
exit(0);

setsid(); //使子進程成為組長
pid=fork();
if(pid>0)
exit(0); //再次退出,使進程不是組長,這樣進程就不會打開控制終端
else if(pid<0)
exit(1);


//關閉進程打開的文件句柄
for(i=0;i<NOFILE;i++)
close(i);
chdir("/root/test"); //改變目錄
umask(0);//重設文件創建的掩碼
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 ",asctime(localtime(&t))); //轉換為本地時間輸出
fclose(fp);
}
}
return;
}


監控進程monitor.c:
#include<unistd.h>
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/param.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<time.h>


#include<sys/wait.h>
#include<fcntl.h>
#include<limits.h>


#define BUFSZ 150


void init_daemon()
{
int pid;
int i;
pid=fork();
if(pid<0)
exit(1); //創建錯誤,退出
else if(pid>0) //父進程退出
exit(0);


setsid(); //使子進程成為組長
pid=fork();
if(pid>0)
exit(0); //再次退出,使進程不是組長,這樣進程就不會打開控制終端
else if(pid<0)
exit(1);


//關閉進程打開的文件句柄
for(i=0;i<NOFILE;i++)
close(i);
chdir("/root/test"); //改變目錄
umask(0);//重設文件創建的掩碼
return;
}


void err_quit(char *msg)
{
perror(msg);
exit(EXIT_FAILURE);
}


// 判斷程序是否在運行
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(count>0)
fprintf(fp,"current time is:%s and the process exists, the count is %d ",asctime(localtime(&t)), count); //轉換為本地時間輸出
else
{
fprintf(fp,"current time is:%s and the process does not exist, restart it! ",asctime(localtime(&t))); //轉換為本地時間輸出
system("/home/user/daemon/thisisatest"); //啟動服務
}


fclose(fp);
}
}
return;
}


具體CMD命令:


cc thisisatest.c -o thisisatest
./thisisatest
cc monitor.c -o monitor
./monitor


tail -f testfork3.log -- 查看日誌

『叄』 linux C語言實現意外退出後自動重啟程序

腳本里 可以用

jobs | grep 進程號 || a.out的絕對路徑

『肆』 linux簡單的C語言程序,多進程,為什麼存在死循環的時候不列印字元死循環明明在列印語句的後面呀

流來不及刷新,不是你列印就立刻出現再終端的,它需要一個刷新過程,而你的死循環讓它沒有時間刷新

熱點內容
空調壓縮機種類 發布:2025-07-04 18:13:58 瀏覽:241
中國有ip6伺服器嗎 發布:2025-07-04 17:58:56 瀏覽:724
第六章編譯原理答案 發布:2025-07-04 17:37:55 瀏覽:39
php內存優化 發布:2025-07-04 17:25:54 瀏覽:663
威綸觸摸屏如何設置時間限制密碼 發布:2025-07-04 17:25:50 瀏覽:418
python列表的遍歷 發布:2025-07-04 17:24:20 瀏覽:23
編譯基本塊 發布:2025-07-04 17:23:06 瀏覽:750
scl語言編程 發布:2025-07-04 17:23:05 瀏覽:993
oracle用戶連接資料庫連接 發布:2025-07-04 17:20:20 瀏覽:939
我的世界純生存伺服器推薦死亡不掉落 發布:2025-07-04 17:06:14 瀏覽:348