LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-11-2013, 04:55 AM   #1
Sud_paddy
LQ Newbie
 
Registered: Aug 2013
Posts: 12

Rep: Reputation: Disabled
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
 
Old 08-11-2013, 05:28 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Okay, here is a comment: your code would be more readable is you used [code] and [/code] tags.
 
Old 08-14-2013, 06:51 PM   #3
Chanur
LQ Newbie
 
Registered: Mar 2012
Distribution: Ununtu 11.10
Posts: 2

Rep: Reputation: Disabled
} // 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 ?
 
Old 08-15-2013, 08:50 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
USB Low Level commands (embedded Linux, terminal commands) JonCombat Linux - Newbie 2 01-05-2013 02:34 PM
LXer: HTML5: To Fork or Not to Fork LXer Syndicated Linux News 1 07-25-2012 07:58 PM
what is the similarity between linux commands and dos commands kamalkirat1 Linux - Software 6 11-21-2008 08:54 PM
Need help for Windows cmd commands into Linux terminal commands. windowsNilo Linux - Software 2 07-02-2008 06:26 PM
Need help for Windows cmd commands into Linux terminal commands. windowsNilo Linux - General 2 07-01-2008 06:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration