简体   繁体   中英

A menu of Linux commands (e.g ls, ipconfig etc) that forks and uses execl to run the commands as a child process

I have been given some code and I need to adapt this to show a menu of Linux commands and when a command is selected, the program should fork and use execl to run the commands as a child process and show its PID. I'm new to C so this I've been struggling so far :/

#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;
}

before calling

fork() 

the code should display the menu, probably via some calls to printf(),

then input the users selection

then use that selection, probably via a switch() statement, to place the appropriate text into an

char *args[] variable 

then use that args[] variable when calling execl()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM