mybash 的实现
码云链接
https://gitee.com/bestiisjava2017/laura5332/blob/master/信息安全系统设计/mybash.c
遇到的问题
- 定义 char *arlgist[[3];
解决方案
- 动态分配内存空间后,运行就没有问题了。
#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;
}