LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 12-10-2006, 03:23 AM   #1
noir911
Member
 
Registered: Apr 2004
Posts: 682

Rep: Reputation: Disabled
[C] pid_t confusion


I'm trying to understand the significance of pid_t in C. The glibc reference manual says pid_t is a data type which is capable of representing a process ID. So, I'm guessing pid_t is data type in a sense that float, int, string, char are data types.

In some code I've seen

pid_t my_pid = getpid();

which gets the pid.

But I can also do

printf ("my pid is %d\n", getpid());

So why would I use pid_t to get my PID when I can just use getpid() in the printf statement?
 
Old 12-10-2006, 08:00 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
pid_t is created by a typedef statement. This is because on some systems a pid value is an unsigned short, other systems it is an int. This way you let the compiler figure out the datatype

In the header file you might see (I'm making this up to make it easy to understand):
Code:
/* is this HPUX? */
#ifdef _HPUX_SOURCE
#define PID_DEF
typedef unsigned int pid_t;
#endif
/* is this SYSTEMV *.
#ifdef _SYSTEMV
#define PID_DEF
typedef unsigned short pid_t;
#endif
/* if none of these then use this as a default */
#ifndef PID_DEF
#define PID_DEF
typedef int pid_t;
#endif
 
Old 12-10-2006, 02:31 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
pid_t may be different type in differnet systems. It may be 8, 16, 32 or 64 bit, but using a separate type allows an easy change to any type the situation/future feature request may lead it to. Printf with %d is not a very portable way, it may not work in all possible environment, especially when the type used for pid_t is much longer than int.
 
Old 12-10-2006, 03:10 PM   #4
BiThian
Member
 
Registered: Aug 2006
Location: Romania
Distribution: NetBSD 3.1
Posts: 118

Rep: Reputation: 15
If I still want to print it in a portable way, how do you suggest to do it?
LE: Never mind, I guess a (f)write or a library function who outputs strings would do.

Last edited by BiThian; 12-10-2006 at 03:40 PM.
 
Old 12-11-2006, 06:00 AM   #5
noir911
Member
 
Registered: Apr 2004
Posts: 682

Original Poster
Rep: Reputation: Disabled
Thanks for all the response. I understand it better now. So if I were to get my PID and wrote the following code would that be correct? Please notice I used "%d" to print my PID. Should I use anything else?

Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

main() {

    pid_t my_pid = getpid();
    printf ("my pid is %d \n", my_pid);

}
 
Old 12-11-2006, 04:03 PM   #6
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
It will work in most cases. In fact, most implementations use int. It means that it'll print correctly. When it comes to implementations with different type used - you have a good chance to get a warning (if not a compiler error) if you try to compile such thing in a system it's not correct - so you'll notice this.
 
Old 12-11-2006, 04:25 PM   #7
BiThian
Member
 
Registered: Aug 2006
Location: Romania
Distribution: NetBSD 3.1
Posts: 118

Rep: Reputation: 15
@noir911: You could try something with sizeof to determine what type is it and then print it in the right format.

Last edited by BiThian; 12-11-2006 at 04:31 PM.
 
  


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
confusion carl440 Linux - Newbie 4 04-20-2006 03:10 PM
a little confusion..... b0nd Linux - Newbie 1 02-17-2005 06:45 AM
pid_t, key_t? blackzone Programming 1 12-12-2004 11:03 PM
WM Confusion phoeniXflame Slackware 3 02-16-2003 05:19 AM
Some confusion about RH 7.3 psyklops Linux - Distributions 2 05-08-2002 03:08 PM

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

All times are GMT -5. The time now is 06:13 AM.

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