这里我创建了一个进程调用网易云音乐轮和肢,代码如下。CreateProcess是关键
#include <stdio.h>#include <windows.h>
int main(int 棚神argc, char* argv[])
{
//创建进程
char szCommandLine[]="C:\\Program Files (x86)\\Netease\\CloudMusic\\cloudmusic.exe"
STARTUPINFO si={sizeof(si)}
PROCESS_INFORMATION pi
si.dwFlags=STARTF_USESHOWWINDOW
si.wShowWindow=true
bool bRet=::CreateProcess(
NULL,
szCommandLine,
NULL,
NULL,
FALSE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi)
if(bRet)
{
::CloseHandle(pi.hThread)
::CloseHandle(pi.hProcess)
printf("新进程的进程ID号:%d\n",pi.dwProcessId)
printf("新进程的主线程腊世的ID号:%d\n",pi.dwThreadId)
}
}
以下友升代码是以进程方式启动fileName这个程序,氏告橘command是程序的歼团启动参数。public
Process
ProcessStart(string
fileName,
string
command)
{
try
{
Process
process
=
new
Process()
process.StartInfo.FileName
=
fileName
process.StartInfo.Arguments
=
command
process.StartInfo.UseShellExecute
=
false
process.StartInfo.RedirectStandardInput
=
true
process.StartInfo.RedirectStandardOutput
=
true
process.StartInfo.RedirectStandardError
=
true
process.StartInfo.CreateNoWindow
=
true
process.Start()
return
process
}
catch
(Exception
ex)
{
throw
(ex)
}
}
可以分三步来做:
做两个简单的守护进程,并能正常运行
监控进程是否在运行
启动进程
综合起来就可以了,代码如下:
被监控进程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=0i<NOFILEi++)
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\n",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=0i<NOFILEi++)
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\n",asctime(localtime(&t)), count) //转换为本地时间输出
else
{
fprintf(fp,"current time is:%s and the process does not exist, restart it!\n",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 -- 查看日志
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)