“Obtén Pid C” Código de respuesta

Obtén Pid C

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

c consigue 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;
}
Powerful Partridge

Respuestas similares a “Obtén Pid C”

Preguntas similares a “Obtén Pid C”

Más respuestas relacionadas con “Obtén Pid C” en C

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código