LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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, 08:54 AM   #1
jegadezz
Member
 
Registered: Oct 2015
Location: Bangalore,India
Distribution: Fedora,Ubuntu
Posts: 32

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, 10:47 AM   #2
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,049
Blog Entries: 17

Rep: Reputation: 4222Reputation: 4222Reputation: 4222Reputation: 4222Reputation: 4222Reputation: 4222Reputation: 4222Reputation: 4222Reputation: 4222Reputation: 4222Reputation: 4222
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, 12:09 PM   #3
jegadezz
Member
 
Registered: Oct 2015
Location: Bangalore,India
Distribution: Fedora,Ubuntu
Posts: 32

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, 12:59 PM   #4
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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 01:07 PM.
 
Old 05-25-2016, 03:12 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,359
Blog Entries: 4

Rep: Reputation: 3822Reputation: 3822Reputation: 3822Reputation: 3822Reputation: 3822Reputation: 3822Reputation: 3822Reputation: 3822Reputation: 3822Reputation: 3822Reputation: 3822
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 06:16 PM
Script to check PID from file and check whether process is running or not rajkiran183 Linux - Newbie 5 10-19-2012 11:28 AM
[SOLVED] How to save the pid of a process at the time of process startup jhonnappier2007 Linux - Newbie 13 11-20-2011 11:54 PM
program to fetch pid of a running process smitha kulkarni Linux - Server 9 03-05-2010 12: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
Open Source Consulting | Domain Registration