linux 进程心得,Linux进程学习心得

EG1 : system函数:

NAME

system - execute a shell command

SYNOPSIS

#include

int system(const char *command);

DESCRIPTION

system() executes a command specified in command by calling /bin/sh -c

command, and returns after the command has been completed. During exe‐

cution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT

will be ignored.将一个系统命令(比如是"gvim new_file")以字符串的形式传给system()中的command,system函数执行该命令,就像我们在命令行行中执行:gvim new_file的效果是一样的。以下是一个简单的程序样例:

#include

#include

#include

#include

#include

int main(int argc , char **agrv){

pid_t pid ;

int state ;

pid = fork() ;

if(pid < 0){

printf("Erorr!\n");

return -1 ;

}

if(pid == 0){

//子进程

system("gvim new_file");

exit(0) ;

}

else{

//父进程

if( waitpid(pid, &state , 0) !=pid )state = -1 ;

//父进程等待子进程执行完之后再继续执行,waitpid函数成功是返回state值改变的在子进程的ID ,错误返回-1

printf("The state is : %d\n",state);

}

return 0;

}

以下写的这个my_system函数的功能和上面的system是类似的,不过就是自己写的而已:

#include

#include

#include

#include

#include

#define SHELL "/bin/sh"

int my_system(const char *command){

int state ;

pid_t pid ;

pid = fork() ;

if(pid == 0){

//子进程

execl(SHELL,SHELL,"-c",command ,NULL);

exit(EXIT_FAILURE);

}

else if(pid > 0){

//父进程

if( waitpid(pid , &state , 0) != pid) state = -1 ;

}

elsestate = -1 ;

return state ;

}

int main(){

printf("This is the return value : %d\n",my_system("gvim creat_new_file"));

return 0;

}

EG2 :多进程和sleep函数

/*

Linux进程学习,多进程和sleep函数。

2011-12-24

Ivan_zjj

*/

#include

#include

#include

#include

int main(){

int status = 0;

pid_t child_1_pid,child_2_pid;

child_1_pid = fork(); /* 这是第一个子进程 */

if (child_1_pid == 0){

/* 这是第一个子进程的代码 */

pid_t child_child_pid;

child_child_pid = fork(); /* 这是第一个子进程的子进程 */

if( child_child_pid == 0){

/* 这是第一个子进程的子进程的代码 */

int child_child_i;

for ( child_child_i = 0; child_child_i < 5; child_child_i++){

printf("1st child's child process. This is %2d seconds\n ", child_child_i);

sleep(1); /* 注意:修改等待时间,会得到什么结果呢? */

}

_exit(0);

}

else if( child_child_pid > 0 ){

/* 这是第一个子进程的子进程的第一个子进程的代码 */

int child_1_i;

for( child_1_i = 0; child_1_i < 5; child_1_i++){

printf("1st child's child's parent process. This is %2d seconds\n ", child_1_i);

sleep(1);

}

_exit(0);

}

else{

/* 创建第一个子进程的子进程失败了 */

status = -1;

_exit(0);

}

}

else if( child_1_pid > 0 ){

/* 这是第一个子进程的父进程(即最高的那个父进程) */

int child_1_parent_i;

for( child_1_parent_i = 0; child_1_parent_i < 5; child_1_parent_i++){

printf("1st child's parent process. this is %2d seconds\n ", child_1_parent_i);

sleep(1);

}

//_exit(0); /* 要是不将这行注释掉, 下面的第二个进程就不能创建了。因为父进程运行到这里就返回了。*/

}

else{

/* 创建第一个子进程失败 */

printf(" fork 1st child process failed\n ");

status = -1;

_exit(1);

}

/* 创建第一级的第二个子进程 */

child_2_pid = fork();

if( child_2_pid == 0){

/* 这是第一级的第二个的子进程的代码 */

int child_2_i;

for( child_2_i = 0; child_2_i < 5; child_2_i++)

{

printf("2nd child process. this is %2d seconds\n ",child_2_i);

sleep(1);

}

_exit(0);

}

else if( child_2_pid > 0)

{

/* 这是第一级的第二个子进程的父进程(最高级进程)的代码 */

int child_2_parent_i;

for( child_2_parent_i = 0; child_2_parent_i < 5; child_2_parent_i++)

{

printf("2nd child's parent process, This is %2d seconds\n ",child_2_parent_i);

sleep(1);

}

_exit(0);

}

else

{

/* 创建第一级第二个子进程失败 */

printf(" fork 2nd child process failed\n");

status = -1;

_exit(1);

}

return status;

}void sleep( int seconds  ) : Sleep for the specified number of seconds

运行结果:

fd202c6f7fc82e3ec9f8e573f90dac13.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值