LinuxQuestions.org
Help answer threads with 0 replies.
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 02-28-2003, 07:16 AM   #1
Makaveli.2003
LQ Newbie
 
Registered: May 2002
Location: England, UK
Posts: 5

Rep: Reputation: 0
C Programming Question


Im new to C Programming and have been workin on afew examples off the internet, but I cant seem to work out what these 2 programs do, could some1 please tell me what these programs do, and help me understand what each line does? plz, thx in advance.

Code:
main()
{
 	 printf("My pid = %d.  My parent's pid = %d\n", getpid(), getppid());
}
Code:
fatal(s)
char *s;
{
	perror(s);
	exit(1);
}

main()
{
	int pid;

	pid = fork();
	printf("The PID is %d\n",pid);
	if(pid > 0)
	{
		wait((int *)0);
		printf("ls completed\n");
		exit(0);
	}

	if (pid == 0)
	{
		execl("/bin/ls", "ls", "-l", (char *)0);
		fatal("execl failed");
	}
	fatal("fork failed");
}
 
Old 02-28-2003, 08:20 AM   #2
loke137
Member
 
Registered: Feb 2003
Location: Brasil
Distribution: Debian Etch
Posts: 147

Rep: Reputation: 15
did you include the libraries in the beginning of the proguram ?

Code:
#include <stdio.h>
Also good programming practice is
Code:
int main()
{
.
code
.
return 0;
}
Hope it helps
 
Old 02-28-2003, 08:41 AM   #3
Makaveli.2003
LQ Newbie
 
Registered: May 2002
Location: England, UK
Posts: 5

Original Poster
Rep: Reputation: 0
Yea, I did, sorry I didnt cut n paste it properly. Can ne1 help?
 
Old 02-28-2003, 12:06 PM   #4
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Most of the standard functions will come with manual pages. Type:

man printf
man getpid
man getppid
man fork

In brief, though: printf is used for formatted output, usually directed to the screen. It's the most commonly-used method of printing out text in C.

getpid is used on *NIX systems to retrieve the process ID number (i.e., if you type 'ps -a' you get a listing of running processes, and their ID numbers - getpid retrieves that number).

Similarly, getppid retrieves the process parent's ID (the process that started that process running).

fork causes a running process to split off into two processes, both of which continue running at the fork point.
 
Old 03-03-2003, 06:34 AM   #5
aditya
Member
 
Registered: Aug 2002
Distribution: RH 7.x
Posts: 71

Rep: Reputation: 15
Hi,
the programs are pretty simple. Have you read process management.
read some material on process creation.
A parent process creates a child process by forking. So in the first program the main functionis giving it's identifier as well as the process identifier of the process that created it.

The 2nd program creates a new process. read the details of fork. It's easy.

hope that helps.

bye

aditya
 
  


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
a little programming question haora Programming 3 06-24-2005 08:19 PM
Question about programming jcramer Mandriva 2 02-01-2004 02:22 PM
Programming question pk21 Linux - Newbie 6 12-02-2002 01:17 PM
Programming question LinuxQuest01 Linux - Software 3 08-09-2002 11:42 AM
newbie programming question aizkorri Programming 4 03-22-2002 03:39 PM

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

All times are GMT -5. The time now is 04:58 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