Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

0 I need some help with Shell Programming using C Language. I have a homework-like project that I need to write a simple shell but

0

I need some help with Shell Programming using C Language. I have a homework-like project that I need to write a simple shell but I'm really unfamiliar with C Language, Linux and these OS related topics.

I did some resarch and tried to learn but I couldn't find a lot of information about this topic and I have some questions. Please help me with my two questions. They are all theoratical, I don't request you to write exact codes that needed for this project but please give me some example codes that can help me understand how things are working.

1- In project I have a template .c file to work with. It has two functions which are main() and setup(). main() function is an ordinary main function that prints "my shell:" and waits for input. After taking input, it sends these inputs to setup() function. setup() function takes input arguments and parse them, take length etc.

One of this project's instructions is this

It will take the command as input and will execute that in a new process. When your program gets the program name, it will create a new process using fork() system call, and the new process (child) will execute the program. The child will use the execv() function in the below to execute a new program.

Use execv() instead of execvp(), which means that you will have to read the PATH environment variable, then search each directory in the PATH for the command file name that appears on the command line.

I think that I need to do something like this in main() function

 pid = fork(); if(pid == 0){ execv(path?,arguments?); 

But I don't know how to use execv() function. I know that first argument should be absolute path but I don't understand what should second argument be. Can you give me an example for this? How can I use execv() function?

2- Instructions:

  1. Using the system() function is not allowed for part A!
  2. In the project, you need to handle foreground and background processes. When a process run in foreground, your shell should wait for the task to complete, then immediately prompt the user for another command.

myshell: gedit

A background process is indicated by placing an ampersand (&) character at the end of an input line. When a process run in background, your shell should not wait for the task to complete, but immediately prompt the user for another command.

myshell: gedit &

With background processes, you will need to modify your use of the wait() system call so that you check the process id that it returns

I did a lot of research and couldn't understand anything about background/foreground processes. How can I implement this feature in my shell? What differs background and foreground? Are codes working differently while backgroud?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions