Linux命令(例如ls,ipconfig等)的菜单,它分叉并使用execl将命令作为子进程运行

问题描述 投票:0回答:1

我得到了一些代码,我需要调整它以显示Linux命令菜单,当选择一个命令时,程序应该fork并使用execl作为子进程运行命令并显示其PID。我是C的新手所以我到目前为止一直在努力:/

#include <unistd.h>
#include <stdio.h>
#include "sys/types.h"
#include <sys/wait.h>
int main(){

pid_t pid;
    int status = 0;
    int i;
    pid= fork() ;
        if(pid!=0) {
                wait(&status);
                printf ( " I am the parent my PID is %d, myPPID is %d, \n ",getpid(),getppid());
                printf( "Mychild process has finished. \n ");
        }else {
        printf ( " I am the child , my PID is %d , my PPID is %d \n",getpid(),getppid());
        sleep(2);
        execl ( "/bin/ls",".",(char*)0);
        printf( "Can you read this ?\n " ) ;
        }
return 0;
}
c menu fork pid ls
1个回答
0
投票

在打电话之前

fork() 

代码应显示菜单,可能是通过对printf()的一些调用,

然后输入用户选择

然后使用该选择,可能通过switch()语句,将适当的文本放入

char *args[] variable 

然后在调用args[]时使用该execl()变量

© www.soinside.com 2019 - 2024. All rights reserved.