Home » C programming language

Process Identification (pid_t) data type in C language



C programming in Linux pid_t data type: In this tutorial, we are going to learn about pid_t data type which can be used in C programming language in Linux.
Submitted by IncludeHelp, on May 30, 2018

pid_t data type in C

pid_t data type stands for process identification and it is used to represent process ids. Whenever, we want to declare a variable that is going to be deal with the process ids we can use pid_t data type.

The type of pid_t data is a signed integer type (signed int or we can say int).

Header file:

The header file which is required to include in the program to use pid_t is sys/types.h

There are basically two functions, which returns the process ids and if we check their return type, it is pid_t, the functions are...

  1. getpid() – this function returns the process id of the calling process, its syntax is, pid_t getpid()
  2. getppid() – this function returns the parent process id in which the function is calling, its syntax is, pid_t getppid()

Example: program to get process id and parent process id and the data type will be pid_t

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

int main(void)
{
	pid_t process_id;
	pid_t p_process_id;

	process_id = getpid();
	p_process_id = getppid();

	printf("The process id: %d\n",process_id);
	printf("The process id of parent function: %d\n",p_process_id);

	return 0;
}

Output

    The process id: 3614
    The process id of parent function: 3613

Testing the 'pid_t' data type

Here, we will test the pid_t data type by its size and by assigning values in it

Test cases:

  1. If size of pid_t and signed int or int is same.
  2. Since unsigned int and signed int have the same size, so we will test the pid_t data type by assigning negative value (as it is a signed int data type and it can be able to store and retrieve it).

Example to implement test cases:

#include <stdio.h>
#include <sys/types.h>

int main(void)
{
	pid_t var;

	var = -200;

	printf("size of int: %d\n",sizeof(int));
	printf("size if pid_t: %d\n",sizeof(pid_t));
	printf("value of var: %d\n",var);

	return 0;
}

Output

    size of int: 4
    size if pid_t: 4
    value of var: -200

Explanation:

Size of int and size of pid_t are same and the assigned value is -200, which is assigned and printed correct. Thus, we can say pid_t is a signed int data type. We can also check its definitions in the header file.






Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions





© https://www.includehelp.com (2015-2018), Some rights reserved.




close Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.