LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-25-2016, 09:54 AM   #1
jegadezz
Member
 
Registered: Oct 2015
Location: Bangalore,India
Distribution: Fedora,Ubuntu
Posts: 30

Rep: Reputation: Disabled
PID of a running process in C++


I am writing a code and I want to set CPU affinity of a process for which I need its PID . How can I read the pid of running processes in C++ code.

Any help is appreciable.
 
Old 05-25-2016, 11:47 AM   #2
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: Debian, Crux, LFS, AntiX
Posts: 2,463
Blog Entries: 6

Rep: Reputation: 1177Reputation: 1177Reputation: 1177Reputation: 1177Reputation: 1177Reputation: 1177Reputation: 1177Reputation: 1177Reputation: 1177
There's a C-library getpid() function that returns the PID of the current process. You can use getppid() to get the PID of the parent.
 
1 members found this post helpful.
Old 05-25-2016, 01:09 PM   #3
jegadezz
Member
 
Registered: Oct 2015
Location: Bangalore,India
Distribution: Fedora,Ubuntu
Posts: 30

Original Poster
Rep: Reputation: Disabled
Hi Hazel , thanks for your reply . Actually I am looking for another independent process's id whose I know only the name . Its neither the child or parent process of my program.
 
Old 05-25-2016, 01:59 PM   #4
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 368Reputation: 368Reputation: 368Reputation: 368
How about something like this?
Code:
#include <stdio.h>
#include <stdlib.h>
#define LEN 10

int main()
{
	char line[LEN];
	FILE *cmd = popen("pidof -s cbatticon", "r");
	long pid = 0;

	fgets(line, LEN, cmd);
	pid = strtoul(line, NULL, 10);
	
	printf("%ld\n", pid);
	
	pclose(cmd);
	
	return 0;
}
My output:
Code:
$ ./myPidof 
365
My output from terminal:
Code:
pidof -s cbatticon
365
ps aux | grep cbatti | grep -v grep
HMW     365  0.0  1.1  63368 24276 tty1     Sl   18:51   0:02 cbatticon
Probably bug prone as hell, no expert at C (read: amateur!), mostly stolen from here:
http://stackoverflow.com/questions/8...x-in-c#8166467

Best regards,
HMW

Last edited by HMW; 05-25-2016 at 02:07 PM.
 
Old 05-25-2016, 04:12 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171Reputation: 3171
Bear in mind, also, that "CPU affinity" is customarily set by rules, or heuristics, which do not presume prior knowledge of a particular PID. The exceptions are low-level system calls which presume that the PID is already known, e.g. by its parent.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Getting PID of running process in C/C++ laikos Programming 16 10-30-2015 07:16 PM
Script to check PID from file and check whether process is running or not rajkiran183 Linux - Newbie 5 10-19-2012 12:28 PM
[SOLVED] How to save the pid of a process at the time of process startup jhonnappier2007 Linux - Newbie 13 11-21-2011 12:54 AM
program to fetch pid of a running process smitha kulkarni Linux - Server 9 03-05-2010 01:51 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:11 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration