Search
 
SCRIPT & CODE EXAMPLE
 

C

get pid c

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int pid;
    printf("1) before the fork
");
    pid = fork();
    printf("2) after the fork
");
    if (pid == 0)
    {
        printf("3) i am the child process, my pid is %d
", getpid());
        printf("my parent has the pid %d
", getppid());
        exit(1);
    }
    else
    {
        printf("i am the parent process, my pid is %d
", getpid());
        printf("my parent has the pid %d
", getppid());
        exit(0); //the father of the father is the terminal
    }
}
// THIS ONLY WORKS ON LINUX
Comment

c get pid

/* Windows only */
#include <stdio.h>
#include <Windows.h>
#include <TlHelp32.h>

int
main(void)
{
	const WCHAR *processname = L"name_of_your_process.exe";
	DWORD pid = 0;

	HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	PROCESSENTRY32 process;
	ZeroMemory(&process, sizeof(process));
	process.dwSize = sizeof(process);

	if (Process32First(snapshot, &process)) {
		do {
			if (!wcscmp(process.szExeFile, processname)) {
				pid = process.th32ProcessID;
				break;
			}
		} while (Process32Next(snapshot, &process));
	}

	CloseHandle(snapshot);

    /* rest of code */
    /* pid of our target is stored in the "pid" DWORD */

	return 0;
}
Comment

PREVIOUS NEXT
Code Example
C :: conio.h linux 
C :: print an array in c 
C :: docker container give usb access 
C :: wireless app debug android 
C :: how to use gets after scanf 
C :: transpose of matrix using c program 
C :: how to map one value to another in C 
C :: print boolean value in c 
C :: come creare variabili casuali in c 
C :: get chunks of a mp4 in ffmpeg 
C :: c printf to string 
C :: c static 
C :: how to genrate a random number in C 
C :: program execution time calculate in c 
C :: exclamation mark in c 
C :: string if statements c 
C :: armstrong number in c 
C :: mutex c 
C :: c program for swapping of two numbers using temporary variable 
C :: c pass int by reference 
C :: copy string in c 
C :: C Passing Pointers to Functions 
C :: mpi example 
C :: How to convert string to int without using library functions in c 
C :: print hello world in c 
C :: gcd and lcd in c 
C :: c change value of const 
C :: imprimir matriz 
C :: getchar c 
C :: vifm preview images 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =