Linux进程学习心得

EG1 : system函数:

NAME
       system - execute a shell command

SYNOPSIS
       #include <stdlib.h>

       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<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
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<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>
#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 ;
	}
	else	state = -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<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
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

运行结果:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
1. 概述.............................................................................................................................................1 1.1. 自然语言&计算机语言................................................................................................1 1.2. 计算机语言 & C/C++语言..........................................................................................2 1.3. 简单的C/C++程序及其运行方法(环境的使用)................................................2 1.3.1. C/C++程序开发运行环境....................................................................................2 1.3.2. 格式输出函数printf()和格式输入函数scanf()....................................................3 1.4. 习题..............................................................................................................................5 2. 基本的C语言................................................................................................................................6 2.1. C语言中的名词——类型、量值(常量和变量)....................................................6 2.1.1. 整型和整型量值...................................................................................................6 2.1.2. 浮点型和浮点量(常量和变量).......................................................................8 2.1.3. 字符型和字符量(常量和变量).......................................................................9 2.1.4. 字符串常量.........................................................................................................10 2.2. C语言中的动词—运算符,短语-表达式 和和特殊动词性关键字....................11 2.2.1. 赋值运算符和赋值表达式.................................................................................11 2.2.2. 算术运算符和算术表达式.................................................................................12 2.2.3. 逻辑运算符和逻辑表达式.................................................................................13 2.2.4. 关系运算符和关系表达式.................................................................................14 2.2.5. 其它运算符和表达式.........................................................................................15 2.2.6. 不同类型量值的不同运算的混合.....................................................................17 2.3. C语言中的连词及句子——分支、循环和顺序语句..............................................19 2.3.1. if-else:如果-那么...........................................................................................19 2.3.2. switch-case语句..................................................................................................21 2.3.3. ?-:语句................................................................................................................23 2.3.4. for........................................................................................................................23 2.3.5. while....................................................................................................................24 2.3.6. do-while..............................................................................................................25 2.3.7. goto-label-if.........................................................................................................26 2.3.8. break....................................................................................................................27 2.3.9. continue...............................................................................................................28 2.4. C语言中的句子小结..................................................................................................28 2.5. 用C语言写一段话——程序段..................................................................................29 2.6. 用C语言写复杂段落——语句嵌套..........................................................................29 2.6.1. 复杂段落——语句嵌套的含义.........................................................................29 2.6.2. 嵌套语句的示例.................................................................................................30 2.6.3. 复杂段落——语句嵌套的小结.........................................................................33 2.7. 总结............................................................................................................................33 习题...........................................................................................................................................34 3. C语言的横向名词性扩充..........................................................................................................34 3.1. C语言的横向名词性扩充(1):同类变量的集合——数组..................................34

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值