Edit the code (shell.c) given to do the tasks asked! will rate
for correct answer! Also, include a screen shot of the output and
terminal window of each command you used. Read carefully to do this
task please.
shell.c code given below.
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> #include <string.h> int main() { int PID; char lineGot[256]; char *cmd; while (1){ printf("cmd: "); fgets(lineGot, 256, stdin); // Get a string from user (includes \n) cmd = strtok(lineGot, "\n"); // Get the string without the \n if( strcmp(cmd, "e") == 0 ) // loop terminates when "e" is typed exit (0); // creates a new process. Parent gets the child's process ID. Child gets 0. if ( (PID=fork()) > 0) { wait(NULL); } else if (PID == 0) /* child process */ { execlp (cmd, cmd, NULL); /* exec cannot return. If so do the following */ fprintf (stderr, "Cannot execute %s\n", cmd); exit(1); /* exec failed */ } else if ( PID == -1) { fprintf (stderr, "Cannot create a new process\n"); exit (2); } } }
The solution for the above question is given below with the
screenshot of output.
---------------------------------------------------------------------------------------------------------------
I have kept the logic simple and output as per the question.
I have update the program to use execvp and strtok .
If there is anything else do let me know in comments
---------------------------------------------------------------------------------------------------------------
--------------- CODE TO COPY -----------------------------------------------------------------------
shell.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> #include <unistd.h> int main() { int PID; char lineGot[256]; char *cmd; while (1) { // Array of pointers for arguments char *args[16] = {0}; printf("cmd: "); fgets(lineGot, 256, stdin); // Get a string from user (includes \n) cmd = strtok(lineGot, "\n"); // Get the string without the \n if (strcmp(cmd, "e") == 0) // loop terminates when "e" is typed exit(0); // creates a new process. Parent gets the child's process ID. Child gets // 0. int i = 0; // Returns first cmd args[0] = strtok(lineGot, " "); args[1] = strtok(NULL, " "); args[2] = strtok(NULL, " "); if ((PID = fork()) > 0) { wait(NULL); } else if (PID == 0) /* child process */ { execvp(cmd, args); /* exec cannot return. If so do the following */ fprintf(stderr, "Cannot execute %s\n", cmd); exit(1); /* exec failed */ } else if (PID == -1) { fprintf(stderr, "Cannot create a new process\n"); exit(2); } } }
---------------------------------------------------------------------------------------------------------------
Output :
Edit the code (shell.c) given to do the tasks asked! will rate for correct answer! Also,...
Hello, how can i compile this C code using option -std=c99 or -std=gnu99 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/stat.h> #include <fcntl.h> static char* args[512]; pid_t pid; int command_pipe[2]; static void waiting(int n); static int command(int input, int first, int last) { int fd[2]; int flag=0; pipe( fd ); pid = fork(); if (pid == 0) { for(int i=0;args[i]!=0;i++) { if(args[i][0]=='>') { fd[1]=open(args[i+1], O_CREAT|O_TRUNC|O_WRONLY, 0644); flag=1; } if(args[i]=='>>' ) { fd[1]=open(args[i+1],...
The original code using the gets() function is written below. You need to do (a) change the provided code so that you now use fgets() function to obtain input from the user instead of gets(), (b) make any other necessary changes in the code because of using fgets() function, and (c) fill in the code for the execute() function so that the whole program works as expected (a simple shell program). Note: part c is already done, and the execute...
C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...
Source code to modify:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_t pid;
/*fork a child process*/
pid = fork();
if (pid<0){ /*error occured*/
fprintf(stderr,"Fork failed\n");
return 1;
}
else if (pid == 0) { /*child process */
printf("I am the child %d\n",pid);
execlp("/bin/ls","ls",NULL);
}
else { /*parent process */
/*parent will wait for the child to complete*/
printf("I am the parent %d\n",pid);
wait(NULL);
printf("Child Complete\n");
}
return 0;
}
1. Write program codes for 3.21 a and...
Modify When executing on the command line having only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with your new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the command line to be tested...
need this in c programming and you can edit the code below. also
give me screenshot of the output
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define LINE_SIZE 1024
void my_error(char *s)
{
fprintf(stderr, "Error: %s\n", s);
perror("errno");
exit(-1);
}
// This funciton prints lines (in a file) that contain string
s.
// assume all lines has at most (LINE_SIZE - 2) ASCII
characters.
//
// Functions that may be called in this function:
// fopen(), fclose(), fgets(), fputs(),...
I am supposed to write documentation and report for the code below but I am new to operating system concepts I will appreciate if someone can help make a detailed comment on each line of code for better understanding. Thanks #include <pthread.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <ctype.h> #define handle_error_en(en, msg) \ do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) struct thread_info...
This is for a Unix class.
Please help me out.
I am attaching a skeletal code of the program below, it just
needs ti be filled in.
Below is a skeletal code of the program. Fork a child process and then use the
parent for reading and the child for writing. This is
just a way of sending and receiving messages
asynchronously.
/*
*************************************************************
* Utility functions
* **************************************************************
*/
static void
usageError(const char * progName, const char *msg)
{...
This is the given code:
/**
* This program uses a Taylor Series to compute a value
* of sine.
*
*/
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
/**
* A function to compute the factorial function, n!.
*/
long factorial(int n) {
long result = 1, i;
for(i=2; i<=n; i++) {
result *= i;
}
return result;
}
int main(int argc, char **argv) {
if(argc != 3) {
fprintf(stderr, "Usage: %s x n ", argv[0]);
exit(1);
}
double x = atof(argv[1]);
int...
Overview Writing in the C language, implement a very basic shell, called smash. In this project, we will work on processing strings and using the appropriate system calls. Build System setup Before we can start writing code we need to get our build system all setup. This will involve writing a very simple makefile. You should leverage your Makefile project for this part of the assignment! Write a Makefile that provides the three targets listed below all - The default...