linux 系统调用分析 使用strace工具

test.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

//using namespace std;

#define MAX_PARA_NUMS      10
#define MAX_CHAR_EACH_PARA 256

/**
把带空格的input字符串 分割成几个不带空格的字符串
例如:
input = "ls /homg/ding"
那么
output[0] = "ls" 
output[1] = "/home/ding"

函数返回output的数量
*/
int split(char* input, char output[MAX_PARA_NUMS][MAX_CHAR_EACH_PARA])
{
    int counter = 0;

    int len = strlen(input);
    int flag = 0 ;
    int i, j = 0 ;
    for(i = 0; i < len; i++)
    {
        if(input[i] == ' ') //当前字符是空格时,flag设为0 保存当前的字符串
        {
            if(j != 0 && counter > 0)
            {
                output[counter - 1][j] = '\0';
            }
            flag = 0;
        }
        else
        {
            if(flag == 0) // 新的字符串开始 bTemp设为 1
            {
                j = 0;
                counter++;
                flag = 1;
            }
		
            output[counter - 1][j] = input[i]; // 仍然是上一个str
            j++;
        }
    }   
    return counter; 
}


char cmd[MAX_CHAR_EACH_PARA] ; //输入的命令
int counter = 0; // 参数个数 最大设为了10
char params[MAX_PARA_NUMS][MAX_CHAR_EACH_PARA]; // 接收一个一个的参数

int main()
{
  	int ret = 0 ;

	printf("Hello world, this is Linux!") ;
	
	while(1)
	{
		printf(">");
		fgets(cmd,256,stdin) ;
		if (cmd[strlen(cmd) - 1] == '\n')
        	{
            		cmd[strlen(cmd) - 1] = '\0';
        	}
		
		if(fork() == 0)
		{
			//execlp(cmd,NULL);
 			counter = split(cmd, params);
            		if(counter != 0)
           		{
		               	//printf("counter = %d\n" , counter);
				switch(counter)
				{
					case 0: break;
					case 1: 
					{
						execlp(params[0], params[0], NULL);
					}    
					break;
					case 2:
					{
						//printf("%s %s\n" ,params[0] ,params[1] );
						execlp(params[0], params[0] , params[1], NULL);
					}
					break;
					case 3:
					{
						execlp(params[0], params[0], params[1], params[2],
							(char*)0);
					}
					break;
					case 4:
					{
						execlp(params[0], params[0], params[1], params[2], 
							params[3], (char*)0);
					}
					break;
					case 5:
					{
						execlp(params[0], params[0], params[1], params[2], 
					   	 params[3], params[4], (char*)0);
					}
					break;
					case 6:
					{
						execlp(params[0], params[0], params[1], params[2], 
						    params[3], params[4], params[5], (char*)0);
					}
					break;
					case 7:
					{    
						execlp(params[0], params[0], params[1], params[2], 
						    params[3], params[4], params[5], params[6], (char*)0);
					}
					break;
					case 8:
					{    
						execlp(params[0], params[0], params[1], params[2], 
						    params[3], params[4], params[5], params[6],
					    params[7], (char*)0);
					}
					break;
					case 9:
					{    
						execlp(params[0], params[0], params[1], params[2], 
						    params[3], params[4], params[5], params[6],
						    params[7], params[8], (char*)0);
					}
					break;
					case 10:
					{    
						execlp(params[0], params[0], params[1], params[2], 
						    params[3], params[4], params[5], params[6],
						    params[7], params[8], params[9], (char*)0);
					}
					break;
					default:
					{
						printf("Illegal Input!\n");
					}
				} // end switch
			}
		}else{
			wait(&ret);
			printf("child process return %d\n",ret);
		} 
	} // end while
	return 0;
}
编译运行:


直接使用strace 命令


程序运行后 会停在read处 等待输入

在输入 ls  /home/ding 后执行相应的命令


使用strace -f ./test 可以跟踪由fork调用所产生的子进程 



可以看到整个程序的运行过程 以及

open read access write等系统函数的调用


学习参考:

strace的使用  http://blog.csdn.net/qq_26437925/article/details/49532921

execlp实例讲解:http://bubuko.com/infodetail-376404.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值