Skip to content

Instantly share code, notes, and snippets.

@kkirby
Last active October 11, 2023 06:19
Show Gist options
  • Save kkirby/cde9ded430bc632381143014f8300aa2 to your computer and use it in GitHub Desktop.
Save kkirby/cde9ded430bc632381143014f8300aa2 to your computer and use it in GitHub Desktop.
Updated NonResponding
/**
* compile using this:
* gcc -framework Carbon -framework Foundation ./hung.m -o hung
*
* Referenced from https://apple.stackexchange.com/a/424763/260619
* Original source code https://github.com/jksoegaard/NonResponding
* Original Author: jksoegaard
**/
#import <Foundation/Foundation.h>
#import <Carbon/Carbon.h>
#include <ApplicationServices/ApplicationServices.h>
extern int _CGSDefaultConnection(void);
extern bool CGSEventIsAppUnresponsive(int connection, const ProcessSerialNumber *psn);
bool isNonResponding(pid_t pid)
{
ProcessSerialNumber psn;
if( GetProcessForPID(pid, &psn) )
return 0;
return CGSEventIsAppUnresponsive(_CGSDefaultConnection(), &psn);
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s <pid>\n", argv[0]);
return 1;
}
pid_t pid = atoi(argv[1]);
bool is_responding = !isNonResponding(pid);
if (is_responding) {
printf("Process with pid %d is responding.\n", pid);
} else {
printf("Process with pid %d is not responding.\n", pid);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment