• 第五周 加分题-mybash的实现


    第五周 加分题-mybash的实现

    使用fork,exec,wait实现mybash

    • 产品代码
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <string.h>
    #define DELAY 2
    void child_code(int delay,char *arglist[3])
    {
    	printf("child %d here will sleep for %d seconds
    ",getpid(),delay);
    	sleep(delay);
    	execvp("who",arglist);
    	printf("child done. about to exit
    ");
    	exit(17);
    }
    void parent_code (int childpid)
    {
    	int wait_rv=0;
    	wait_rv = wait(NULL);
    	printf("done waiting %d .wait returned :%d
    ",childpid,wait_rv);
    }
    int main()
    {
    	char s1[5],s2[5];
    	scanf("%s%s",s1,s2);
    	char *arglist[3];
    //	strcpy(arglist[0],s1);
    //	strcpy(arglist[1],s2);
    	arglist[0]="who";
    	arglist[1]="-a";
    	arglist[2]=0;
    	printf("%s",arglist[2]);
    	int newpid;
    	printf("before:mypid is %d 
    ",getpid());
    	if((newpid = fork())==-1)
    		perror("fork");
    	else if(newpid==0)
    		child_code(DELAY,arglist);
    	else parent_code(newpid);
    	return 0;
    		
    }
    
    
    • 测试代码
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <string.h>
    #define DELAY 2
    void child_code(int delay,char *arglist[3])
    {
    	printf("child %d here will sleep for %d seconds
    ",getpid(),delay);
    	sleep(delay);
    	execvp("ls",arglist);
    	printf("child done. about to exit
    ");
    	exit(17);
    }
    void parent_code (int childpid)
    {
    	int wait_rv=0;
    	wait_rv = wait(NULL);
    	printf("done waiting %d .wait returned :%d
    ",childpid,wait_rv);
    }
    int main()
    {
    	char s1[5],s2[5];
    	scanf("%s%s",s1,s2);
    	char *arglist[3];
    //	strcpy(arglist[0],s1);
    //	strcpy(arglist[1],s2);
    	arglist[0]="ls";
    	arglist[1]="-a";
    	arglist[2]=0;
    	printf("%s",arglist[2]);
    	int newpid;
    	printf("before:mypid is %d 
    ",getpid());
    	if((newpid = fork())==-1)
    		perror("fork");
    	else if(newpid==0)
    		child_code(DELAY,arglist);
    	else parent_code(newpid);
    	return 0;
    		
    }
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <string.h>
    #define DELAY 2
    void child_code(int delay,char *arglist[3])
    {
    	printf("child %d here will sleep for %d seconds
    ",getpid(),delay);
    	sleep(delay);
    	execvp("ls",arglist);
    	printf("child done. about to exit
    ");
    	exit(17);
    }
    void parent_code (int childpid)
    {
    	int wait_rv=0;
    	wait_rv = wait(NULL);
    	printf("done waiting %d .wait returned :%d
    ",childpid,wait_rv);
    }
    int main()
    {
    	char s1[5],s2[5];
    	scanf("%s%s",s1,s2);
    	char *arglist[3];
    //	strcpy(arglist[0],s1);
    //	strcpy(arglist[1],s2);
    	arglist[0]="ls";
    	arglist[1]="-l";
    	arglist[2]=0;
    	printf("%s",arglist[2]);
    	int newpid;
    	printf("before:mypid is %d 
    ",getpid());
    	if((newpid = fork())==-1)
    		perror("fork");
    	else if(newpid==0)
    		child_code(DELAY,arglist);
    	else parent_code(newpid);
    	return 0;
    		
    }
    
    

    实现结果截图

    image

  • 相关阅读:
    03.《架构漫谈》阅读笔记
    02.《架构漫谈》阅读笔记
    03.《架构之美》阅读笔记
    02.《架构之美》阅读笔记
    01.《架构之美》阅读笔记
    软件架构中的质量属性--以淘宝网为例(小论文)
    MVC框架介绍分析
    论面向服务架构设计及其应用
    1.26学习进度总结
    1.24学习进度总结
  • 原文地址:https://www.cnblogs.com/tyn5304/p/7700166.html
Copyright © 2020-2023  润新知