The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Difference grep, egrep and fgrep ravind27 UNIX Desktop for Dummies Questions & Answers 2 06-14-2009 07:37 AM
monitoring running processes nhatch Shell Programming and Scripting 2 12-21-2007 09:03 AM
Can we grep a list of all running PIDs in a file !!?? marconi Shell Programming and Scripting 1 12-20-2007 04:48 PM
how to find all processes that are running speedieB UNIX for Dummies Questions & Answers 9 07-12-2006 05:04 AM
grep/awk/egrep? whugo UNIX for Dummies Questions & Answers 3 01-20-2006 12:51 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-23-2005
jimmynath jimmynath is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 11
need to grep or egrep the running processes in C file

Hi,

I want to find if specific process are running at any given moment in C? please tell me how do I do this. Currently I am trying to save these process ids in a file and check it. But this is not a foolproof method.

thanks in advance.


Jimmy
  #2 (permalink)  
Old 08-23-2005
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
Try man getutent

This function reads any file in "utmp" format -- the /etc/utmp file has the recent entries. The utmp struct has information about pids currently running.

You can periodically open, read /etc/utmp, then close it to find what processes are currently running.
  #3 (permalink)  
Old 08-24-2005
jimmynath jimmynath is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 11
I did not find the getutent function and also i did not find the /etc/ump file. i am using FreeBSD 5.4 version.ll
  #4 (permalink)  
Old 08-24-2005
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
try man getutxent - this should be on your system. It automatically opens a file:
/etc/utmp
/etc/utmpx or
/var/adm/wtmp

/etc/utmp may not exist depending on how you set up the system. If you disabled any accounting, then it may not exist.

Also try using popen and a shell script - I just made up a function foo() - you get to change it and the command it uses:
Code:
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
pid_t foo(char *pid_to_find)
{
    char cmd[296]={0x0}; /* 40 + 256 */
    char tmp[256]={0x0};
    FILE *in=NULL;
    int retval=0;
    pid_t the_pid_I_found=0;

    snprintf(cmd,sizeof(cmd)-40,
       "ps -ef | grep  %s | awk '{print $2}'",pid_to_find);
    in=popen(cmd, "r");
    if(in==NULL)
    {
         perror("Shell execution error");
         exit(EXIT_FAILURE);
    }
    while(fgets(tmp,4096,in)!=NULL)
    {
         the_pid_I_found=atol(tmp);
         break;
    }
    retval=pclose(in);
    return the_pid_I_found;
}
  #5 (permalink)  
Old 08-24-2005
jimmynath jimmynath is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 11
Thanks for the help. yes I have not enabled the accounting and also thanks for the sript. I will try it.


Thanks once again for the help.

Jimmy
  #6 (permalink)  
Old 09-08-2005
jimmynath jimmynath is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 11
Experts,

I found out that using popen and awk command introduces a racing condition.
Does anyone have any idea why?

Thanks in advance.

Jim
Sponsored Links
Closed Thread

Bookmarks

Tags
grep or

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 09:16 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0