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 :: Symmetrical matrix in C 
C :: hostbuilder add environment variables 
C :: two way communication between child and parent processes in C using pipes 
C :: C Accessing Union Members 
C :: Returns numbers between i and 0 
C :: type cast in c 
C :: Command to compile and execute a c file program consecutively 
C :: C/c drop mime 
C :: String to Integer (atoi) 
C :: router solicitation and advertisement magic is used by 
C :: leer string en c 
C :: how to define pi in c 
C :: c create array 
C :: convert c to python online 
C :: pointeur de pointeur en language c 
C :: reading arrays from stdin c 
C :: c++ to assembly language converter online 
C :: Integer Output 
C :: type conversion 
C :: esp rainmaker led 
C :: send array to child process c 
C :: obby übersetzung 
C :: or gmode inline image 
C :: bit wise operation 
C :: create a buffer in c 
C :: tableau c 
C :: visual studio code 
Dart :: decode, encode base64 dart 
Dart :: how to get whatsapp groups in app flutter programmatically 
Dart :: flutter textbutton autofocus 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =