LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Fork and Commands (https://www.linuxquestions.org/questions/programming-9/fork-and-commands-4175472965/)

Sud_paddy 08-11-2013 04:55 AM

Fork and Commands
 
Write the application or program to open applications of Linux by creating new processes using fork system call. Comment on how various application’s/command’s process get created in linux.



#include<stdio.h>
#include<stdlib.h>

int main(){

int val,ret,proc;
char app1[10],app2[10];
printf("\nThis is the program to illustrate the use of fork and exec system call family-->>");

printf("\nEnter name and command of any two applications you want to run on this linux!");

scanf("%s%s",&app1,app2);

printf(app1);
printf(app2);

proc = fork();

if(proc == 0)
{
printf("\n Forked process executing this app ..btw");
ret = execlp (app1, app1, "-l", (char *)0);

}
if(proc)
{
printf("\n main process executing this app ..btw");
ret = execlp (app2, app2, "-l", (char *)0);

}

return;

Plaese Comment

NevemTeve 08-11-2013 05:28 AM

Okay, here is a comment: your code would be more readable is you used [code] and [/code] tags.

Chanur 08-14-2013 06:51 PM

} // This to terminate your main ;)

- the second argument of scanf is wrong. It should be app1, not &app1. And good compilers signal that as a warning ...
- You could have written "else" instead of "if (fork)"
- You never use the value of ret. You should because execlp does NOT return unless there is a problem in which case the value of ret may be useful to understand the error
- Why do you add a "-l" argument to your commands ?
- the printf in each case could expose the value of app1 (app2)
- I don't like scanf
- what will happen if you enter commands greater than 9 characters ? :tisk:

sundialsvcs 08-15-2013 08:50 AM

You should rewrite your original post in this way:

Quote:

Originally Posted by A L0Z3r:
Ask "linuxquestions.org" to do your homework for you.

Ask yourself this question: "Do you, really, want to learn this stuff?" Seriously. (If the answer is "no," then, trust me on this, there are plenty of bags that need to be filled at grocery-store checkouts; plenty of boxes in the back rooms of retail stores that need to be schlepped to the sales-floor, opened up and their contents stacked on the shelves at 3:00 in the morning.)

If the answer is "yes," then y-o-u are going to have to be the one to do it. There is no other way.


All times are GMT -5. The time now is 06:38 AM.