linux下使用系统调用实现进程后台运行
2004-09-24 14:00
941 查看
其实很简单,就是把
if(fork()==0)
{
execve(...);
}
else
{
wait(state);
}
结构中的else去掉就可以了
下面是一个示例
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <wait.h>
#include <errno.h>
#include <sys/stat.h>
#include <string.h>
int main(int argc,char** argv)
{
int * status;
int gc;
char command[64]="./clu";
char comarg[64]="";
argv[1]=comarg;
while(1)
{
if((gc=getchar())=='g')
{
if(fork()==0)
{
if(execve(command,argv,0)==-1)
{
printf("process error is %s/n",strerror(errno));
}
printf("process is ok/n");
}
}
if(gc=='k')
{
printf("to be continue/n");
}
if(gc=='e')
{
return 1;
}
}
}
功能是这样的,启动以后会从键盘接受字符,如果是g就运行预先指定好的程序(在这里是一个叫clu的程序),如果是k就打印to be continue,如果是e就退出
下面是这个叫clu的程序的代码
#include <stdio.h>
#include <time.h>
int main()
{
long begin=0;
long end=24000;
long finish=0;
long loop1,loop2;
time_t flag;
time_t nowtime1,nowtime2;
if((flag=time(&nowtime1))==-1)exit(1);
for(loop1=begin;loop1<end;loop1++)
{
for(loop2=begin;loop2<end;loop2++)
{
if((loop1%2)==0)
{
if((loop2%2)==0)
{
if(finish>65535)
finish=0;
finish+=((loop1/2+loop2/2)%2)%2;
}
}
}
}
if((flag=time(&nowtime2))==-1)exit(1);
printf("%ld/n",finish);
printf("proccess time %ldsec/n",nowtime2-nowtime1);
}
这个程序本身没有任何意义,只是纯粹的延误一下时间,输出一下运行的时间,便于测试,在我的AMD2500+上,大概是5秒,你可以试试哦^_^
要测试这个程序,可以在程序运行时输入g,但是这个计算不会马上结束,在屏幕还没有输出的时候再输入k,就会先输出k的内容,后台clu的结果出来后才输出到屏幕,也就是clu在后台运行了
if(fork()==0)
{
execve(...);
}
else
{
wait(state);
}
结构中的else去掉就可以了
下面是一个示例
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <wait.h>
#include <errno.h>
#include <sys/stat.h>
#include <string.h>
int main(int argc,char** argv)
{
int * status;
int gc;
char command[64]="./clu";
char comarg[64]="";
argv[1]=comarg;
while(1)
{
if((gc=getchar())=='g')
{
if(fork()==0)
{
if(execve(command,argv,0)==-1)
{
printf("process error is %s/n",strerror(errno));
}
printf("process is ok/n");
}
}
if(gc=='k')
{
printf("to be continue/n");
}
if(gc=='e')
{
return 1;
}
}
}
功能是这样的,启动以后会从键盘接受字符,如果是g就运行预先指定好的程序(在这里是一个叫clu的程序),如果是k就打印to be continue,如果是e就退出
下面是这个叫clu的程序的代码
#include <stdio.h>
#include <time.h>
int main()
{
long begin=0;
long end=24000;
long finish=0;
long loop1,loop2;
time_t flag;
time_t nowtime1,nowtime2;
if((flag=time(&nowtime1))==-1)exit(1);
for(loop1=begin;loop1<end;loop1++)
{
for(loop2=begin;loop2<end;loop2++)
{
if((loop1%2)==0)
{
if((loop2%2)==0)
{
if(finish>65535)
finish=0;
finish+=((loop1/2+loop2/2)%2)%2;
}
}
}
}
if((flag=time(&nowtime2))==-1)exit(1);
printf("%ld/n",finish);
printf("proccess time %ldsec/n",nowtime2-nowtime1);
}
这个程序本身没有任何意义,只是纯粹的延误一下时间,输出一下运行的时间,便于测试,在我的AMD2500+上,大概是5秒,你可以试试哦^_^
要测试这个程序,可以在程序运行时输入g,但是这个计算不会马上结束,在屏幕还没有输出的时候再输入k,就会先输出k的内容,后台clu的结果出来后才输出到屏幕,也就是clu在后台运行了
相关文章推荐
- 基于Linux系统调用使用php实现一个在线编译运行C语言程序的系统
- Linux 技巧:让进程在后台可靠运行的几种方法 用 cron 和 at 调度作业 使用 screen 管理你的远程会话
- Linux使用screen实现关闭ssh连接的情况下,让程序继续在后台运行
- Python实现在Linux系统下更改当前进程运行用户
- 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)
- 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)
- linux下使用系统调用编程实现copy命令功能
- linux下使用系统调用编程实现dir命令功能
- 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)
- linux实现系统调用打印进程信息
- Linux进程间通讯所使用的系统调用函数
- 如何实现Linux进程在后台稳定运行?
- 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)
- Linux:使用nohup让进程在后台可靠运行
- 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)
- 在linux系统下让进程在后台运行的方法
- Python实现在Linux系统下更改当前进程运行用户
- 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)
- Linux下使用nohup实现在后台运行程序(转)
- linux下使用top命令查看系统运行状态和进程运行状态