mybash 的实现

码云链接

https://gitee.com/bestiisjava2017/laura5332/blob/master/信息安全系统设计/mybash.c

遇到的问题

  • 定义 char *arlgist[[3];

scanf("%s",arlgist[0]);

scanf("%s",arlgist[1]);

编译可以通过,但是运行后内存出错。

解决方案

arglist[0]=(char)malloc(sizeof(char)10);

arglist[1]=(char)malloc(sizeof(char)10);

  • 动态分配内存空间后,运行就没有问题了。
#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\n",getpid(),delay);
//sleep(delay);
execvp("who",arglist);
//printf("child done. about to exit\n");
exit(17);
}
void parent_code (int childpid)
{
int wait_rv=0;
wait_rv = wait(NULL);
//printf("done waiting %d .wait returned :%d\n",childpid,wait_rv);
}
int main()
{
fork();
char *arglist[3]; arglist[0]=(char*)malloc(sizeof(char)*10);
arglist[1]=(char*)malloc(sizeof(char)*10);
scanf("%s",arglist[0]);
scanf("%s",arglist[1]);
//arglist[0]="who";
//arglist[1]="-a";
arglist[2]=0;
//printf("%s",arglist[2]);
int newpid;
//printf("before:mypid is %d \n",getpid());
if((newpid = fork())==-1)
perror("fork");
else if(newpid==0)
child_code(DELAY,arglist);
else parent_code(newpid);
return 0;
}

20155332 mybash的实现的更多相关文章

  1. Mybash的实现

    Mybash的实现 要求: 使用fork,exec,wait实现mybash 写出伪代码,产品代码和测试代码 发表知识理解,实现过程和问题解决的博客(包含代码托管链接) 背景知识 1. fork 使用 ...

  2. 2017-2018-1 20155215 第五周 mybash的实现

    题目要求 使用fork,exec,wait实现mybash 写出伪代码,产品代码和测试代码 发表知识理解,实现过程和问题解决的博客(包含代码托管链接) 学习fork,exec,wait fork ma ...

  3. 2017-2018-1 20155306 《信息安全系统设计基础》Mybash的实现

    2017-2018-1 20155306 <信息安全系统设计基础>Mybash的实现 要求: 使用fork,exec,wait实现mybash 写出伪代码,产品代码和测试代码 发表知识理解 ...

  4. 20165223 《信息安全系统设计基础》 实现mybash

    一.了解 mybash 1. 简介   bash 是 Bourne Again Shell 的缩写,是linux默认的标准shell(也是大家常说的系统内核),bash也是Unix/Linux上常见的 ...

  5. 20165220 mybash

    使用fork,exec,wait实现mybash - 写出伪代码,产品代码和测试代码 - 发表知识理解,实现过程和问题解决的博客(包含代码托管链接) 1.fork 功能:创建一个新的进程 一个现存进程 ...

  6. 实现mypwd和mybash

    一.pwd 1.学习pwd命令 man pwd查看pwd功能 可以得知pwd功能是打印当前目录 2.研究pwd实现需要的系统调用(man -k; grep),写出伪代码 (1)man -k direc ...

  7. 实现mybash

    任务内容 1.使用fork,exec,wait实现mybash 查找资料: fork函数 通过fork()系统调用我们可以创建一个和当前进程印象一样的新进程.我们通常将新进程称为子进程,而当前进程称为 ...

  8. 实现mypwd&mybash&myod&读者写者

    目录: 一.mypwd 二.mybash 三.myod 四.读者.写者 一.实现mypwd 学习pwd命令 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 实现mypwd 测试m ...

  9. 20155219 mybash的实现

    第五周加分题--mybash的实现 题目要求 1.使用fork,exec,wait实现mybash 2.写出伪代码,产品代码和测试代码 3.发表知识理解,实现过程和问题解决的博客(包含代码托管链接) ...

随机推荐

  1. python的sys和os模块

    一.sys sys.argv:实现从程序外部向程序传递参数.  其中sys.argv[0]为脚本的名称,所以要判断是否有参数传入可以:if len(sys.argv) > 1.  sys.exi ...

  2. es6面试题--Promise相关

    1. const promise = new Promise((resolve, reject) => { console.log(); resolve(); console.log(); }) ...

  3. iOS UI(绘图)的几张原理图

    Core Animation是对OpenGL ES的Objective-C封装,具有与OpenGL ES几乎等价的高性能,却隐藏了OpenGL ES的复杂性. https://www.cnblogs. ...

  4. gluoncv faster_rcnn 参数修改

    https://github.com/dmlc/gluon-cv/blob/master/gluoncv/model_zoo/faster_rcnn/faster_rcnn.py 对你选用的模块,修改 ...

  5. 面经:C++篇(持续更新)

    一. 左值和右值 L-value中的L指的是Location,表示可寻址.Avalue (computer science)that has an address. R-value中的R指的是Read ...

  6. TensorFlow函数(三)tf.variable_scope() 和 tf.name_scope()

    tf.name_scope() 此函数作用是共享变量.在一个作用域scope内共享一些变量,简单来说,就是给变量名前面加个变量空间名,只限于tf.Variable()的变量 tf.variable_s ...

  7. Presentation 常用的承接句——技术分享、学术报告串联全场不尴尬

    前言 现在即使是搞技术,做科研的,也需要在不同的场合,用ppt来做分享,做汇报,做总结. 如果国际会议,研讨会,或者在外企,国外工作,英文的presentation就更加必不可少.英语的提升需要大家从 ...

  8. [Zedboard Linux系统移植]-从MACHINE_START開始

    改动自:http://www.cnblogs.com/lknlfy/archive/2012/05/06/2486479.html 内核的启动过程? 3)内核的启动过程? arch/arm/kerne ...

  9. Linux Shell常用技巧(九)

    十九.  和系统运行进程相关的Shell命令:       1.  进程监控命令(ps):    要对进程进行监测和控制,首先必须要了解当前进程的情况,也就是需要查看当前进程,而ps命令就是最基本同时 ...

  10. Python 学习笔记(十三)Python函数(二)

    参数和变量 >>> def foo(a,b): #函数是一个对象 return a+b >>> p =foo #对象赋值语句.将foo函数赋值给p这个变量 > ...