LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-12-2005, 10:01 AM   #1
jagman
LQ Newbie
 
Registered: Feb 2005
Posts: 17

Rep: Reputation: 0
Question c program that executes during a certain amount of time


Good day to all of you.
How I make a C program to executes for a certain amount of time?
For example:
My C program capture some data from the output of tcpdump. But I want that this program capture only the first 10 minutes of data from the output of tcpdump. How can i do that?
 
Old 04-12-2005, 10:04 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Check out the man page for the alarm() function. You could set the timer to go off in 600 seconds. Make a signal handler for SIGALRM that simply exits the program.
 
Old 04-12-2005, 06:50 PM   #3
jagman
LQ Newbie
 
Registered: Feb 2005
Posts: 17

Original Poster
Rep: Reputation: 0
Question

Good day to all of you.
I`m doing a C program that creates and exec another process( with fork and exec). Now I want that the child process executes a a system command (shell function) but that shell command ( tcpdump) finish only when the CTRL+c is enter.
How can I make this child process to stop exec? How can I send interuption signals to the child process?
 
Old 04-13-2005, 12:37 AM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
fork() returns the child's PID to the parent. Store it somewhere and then send a signal to it using kill().
 
Old 04-13-2005, 08:33 AM   #5
jagman
LQ Newbie
 
Registered: Feb 2005
Posts: 17

Original Poster
Rep: Reputation: 0
Question

I tried to get the pid of the chil process but i get the number PID= 0 , and when i used that in the kill option the kill option interrupt all the process.
 
Old 04-13-2005, 08:44 AM   #6
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Quote:
Originally posted by jagman
I tried to get the pid of the chil process but i get the number PID= 0 , and when i used that in the kill option the kill option interrupt all the process.
When you use fork() the program will be duplicated and a child process will be created that is exactly the same as the father. The only difference is the return value of fork. The pid of the child will be returned to the father and 0 will be returned to the child. SO only the child will get 0.

Use an "if" to distinguish the father from the child.

Code:
if ( PID=fork() )
{
   /*I'm the father*/
.......................................
......................................
..................................
}
/*the child continues from here*/
 
Old 04-13-2005, 09:22 AM   #7
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You missed a key part of my reply:
Quote:
fork() returns the child's PID to the parent.
You appear to be capturing the child's PID from the child process.
 
  


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
Make most amount of Linux users in least amount of time studpenguin General 24 02-02-2007 03:42 PM
Killing a process after x amount of time... virid Linux - Newbie 3 03-17-2006 06:13 AM
Program to track amount of network usage jpc82 Linux - Software 3 02-10-2006 07:29 AM
an open source program that only gives the user a certain amount of time to input? dr_zayus69 Programming 4 05-22-2005 10:56 PM
Ping times grow HUGE after X amount of time Shade General 2 05-11-2005 11:03 PM

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

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