Using C or C write a program which implements the command pi

Using C or C++, write a program which implements the command pipeline ”ls -la | tr [a-zA-Z0-9] a”. Use any combination of fork, exec, wait, read, and write necessary to create this functionality. Note: the parent process must be the one to output the data to the console.

Solution

Framework call fork() is utilized to make forms. It takes no contentions and returns a procedure ID. The reason for fork() is to make another procedure, which turns into the kid procedure of the guest. After another youngster procedure is made, both procedures will execute the following direction taking after the fork() framework call. Along these lines, we need to recognize the parent from the kid. This should be possible by testing the returned estimation of fork():

• If fork() gives back a negative esteem, the production of a youngster procedure was unsuccessful.

• fork() gives back a zero to the recently made tyke handle.

• fork() gives back a positive esteem, the procedure ID of the tyke procedure, to the parent. The returned procedure ID is of sort pid_t characterized in sys/types.h. Ordinarily, the procedure ID is a whole number. Besides, a procedure can utilize work getpid() to recover the procedure ID alloted to this procedure.

In this way, after the framework call to fork(), a basic test can tell which process is the youngster. If you don\'t mind take note of that Unix will make a precise of the parent\'s address space and offer it to the youngster. Consequently, the parent and tyke forms have isolate address spaces.

Example:

#include <stdio.h>

#include <string.h>

#include <sys/types.h>

#define MAX_COUNT 200

#define BUF_SIZE 100

void main(void)

{

pid_t pid;

int i;

burn buf[BUF_SIZE];

fork();

pid = getpid();

for (i = 1; i <= MAX_COUNT; i++) {

sprintf(buf, \"This line is from pid %d, esteem = %d\ \", pid, i);

write(1, buf, strlen(buf));

}

}

Programm:-

#include <stdio.h>

#include <sys/types.h>

void parse(char *line, burn **argv)

{

while (*line != \'\\0\') {/* if not the finish of line

while (*line == \" || *line == \"\\t\" || *line == \'\ \')

*line++ = \'\\0\';/* supplant white spaces with 0

*argv++ = line;/* spare the contention position

while (*line != \"\\0\" && *line != \" &&

*line != \"\\t\" && *line != \'\ \')

line++;/* skirt the contention until ...

*argv = \'\\0\';/* stamp the finish of contention rundown

}

void execute(char **argv)

{

pid_t pid;

int status;

in the event that ((pid = fork()) < 0) {/* fork a tyke procedure

printf(\"*** ERROR: forking kid prepare failed\ \");

exit(1);

}

else if (pid == 0) {/* for the youngster procedure:

in the event that (execvp(*argv, argv) < 0) {/* execute the charge

printf(\"*** ERROR: executive failed\ \");

exit(1);

}

}

else {/* for the parent:

while (wait(&status) != pid)/* sit tight for fruition

;

}

}

void main(void)

{

singe line[1024];/* the info line

singe *argv[64];/* the charge line contention

while (1) {/* rehash until done ....

printf(\"Shell - > \");/* show a provoke

gets(line);/* read in the charge line

printf(\"\ \")

parse(line, argv);/* parse the line

in the event that (strcmp(argv[0], \"exit\") == 0)/* is it an \"exit\"?

exit(0);/* exit in the event that it is

execute(argv);/* something else, execute the summon..

Using C or C++, write a program which implements the command pipeline ”ls -la | tr [a-zA-Z0-9] a”. Use any combination of fork, exec, wait, read, and write nece
Using C or C++, write a program which implements the command pipeline ”ls -la | tr [a-zA-Z0-9] a”. Use any combination of fork, exec, wait, read, and write nece
Using C or C++, write a program which implements the command pipeline ”ls -la | tr [a-zA-Z0-9] a”. Use any combination of fork, exec, wait, read, and write nece

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site