Skip to content

Why we used fork and execvp in the following program? please explain me

0 points3 comments
Open on Reddit
About this post
Posted
Archived
Score
0 · 33% up
Kind
Text
ID
zuwdhs

Author

1
posts
1
comments
0
media
View profile

3 comments

Probably to make the command entered runs in a separate process. When you fork(), you have two processes running the same line of code, but the parent will have the child PID returned while the child will receive 0 as the return value, so the if pid > 0 branch is for the parent and the other branch is for the child process. exec family replaces the process with another one, in this case it's the command to be executed.

Tl;dr: Here the parent process spawn a new process and wait for it to be terminated, while the child process is replaced with the command to be executed.

0 points

ok , so in following program if i dont want to make a copy of that program , if i have to just return to the user that command he entered is wrong it will run.

This is one of question in my assignment where they asked for creation of child process. So my question is it would work just fine even if i didn't create a child process

0 points

no. processes just exit when done, so if you don't use a child then there won't be anything running to return to ( no more shell).

what would happen when the program gets done is exactly the same as "exit" in normal shells

1 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.