-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ac9fb2
commit cc28f4b
Showing
5 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,10 @@ | |
*.exe | ||
ashwathama | ||
bash_gitignore.sh | ||
endian | ||
garibsh | ||
ior | ||
myps | ||
pid | ||
sync | ||
threads |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
|
||
int main(void) { | ||
|
||
int num = 0x0A0B0C0D; | ||
/* | ||
0A 0B 0C 0D | ||
high low | ||
*/ | ||
unsigned char *ptr = (unsigned char*)# | ||
|
||
if (*ptr == 0x0D) { | ||
printf("Little endian\n"); | ||
} else { | ||
printf("Big endian\n"); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/sysctl.h> | ||
#include <sys/types.h> | ||
#include <sys/param.h> | ||
#include <sys/user.h> | ||
#include <string.h> | ||
#include <errno.h> | ||
|
||
int main(void) { | ||
int mib[3]; | ||
struct kinfo_proc *procs = NULL; | ||
size_t size = 0; | ||
|
||
// MIB array: [CTL_KERN, KERN_PROC, KERN_PROC_ALL] | ||
// This will query all running processes. | ||
mib[0] = CTL_KERN; | ||
mib[1] = KERN_PROC; | ||
mib[2] = KERN_PROC_ALL; | ||
|
||
// First call with a NULL buffer to get the size needed. | ||
if (sysctl(mib, 3, NULL, &size, NULL, 0) == -1) { | ||
fprintf(stderr, "sysctl (get size) failed: %s\n", strerror(errno)); | ||
return 1; | ||
} | ||
|
||
// Allocate a buffer large enough to hold the process list. | ||
procs = (struct kinfo_proc *)malloc(size); | ||
if (!procs) { | ||
fprintf(stderr, "malloc failed\n"); | ||
return 1; | ||
} | ||
|
||
// Second call gets the actual process data. | ||
if (sysctl(mib, 3, procs, &size, NULL, 0) == -1) { | ||
fprintf(stderr, "sysctl (get data) failed: %s\n", strerror(errno)); | ||
free(procs); | ||
return 1; | ||
} | ||
|
||
// Number of processes returned | ||
int count = (int)(size / sizeof(struct kinfo_proc)); | ||
|
||
// Print a simple header | ||
printf("%5s %-16s %5s\n", "PID", "COMMAND", "STATE"); | ||
printf("------------------------------------------------\n"); | ||
|
||
for (int i = 0; i < count; i++) { | ||
// Extract PID and command name | ||
pid_t pid = procs[i].kp_proc.p_pid; | ||
char comm[MAXCOMLEN+1]; | ||
strncpy(comm, procs[i].kp_proc.p_comm, MAXCOMLEN); | ||
comm[MAXCOMLEN] = '\0'; | ||
|
||
// The process state is in p_stat (numeric on macOS). | ||
// Some typical BSD states (historical): | ||
// 1=SIDL (startup), 2=SRUN (running), 3=SSLEEP, 4=SSTOP, 5=SZOMB, etc. | ||
int state = procs[i].kp_proc.p_stat; | ||
|
||
printf("%5d %-16s %5d\n", pid, comm, state); | ||
} | ||
|
||
free(procs); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pid.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <pthread.h> | ||
#include <signal.h> | ||
#include <unistd.h> | ||
|
||
volatile sig_atomic_t stop_thread = 0; | ||
|
||
void handle_signal(int signal) { | ||
stop_thread = 1; | ||
} | ||
|
||
void* increment_counter(void* arg) { | ||
int* counter = (int*)arg; | ||
while (!stop_thread) { | ||
(*counter)++; | ||
printf("Counter: %d\n", *counter); | ||
sleep(1); // Sleep for 1 second | ||
} | ||
return NULL; | ||
} | ||
|
||
int main() { | ||
int counter = 0; | ||
pthread_t thread_id; | ||
|
||
signal(SIGINT, handle_signal); | ||
|
||
if (pthread_create(&thread_id, NULL, increment_counter, &counter) != 0) { | ||
fprintf(stderr, "Error creating thread\n"); | ||
return 1; | ||
} | ||
|
||
for (int i = 0; i < 10; i++) { | ||
if (stop_thread) { | ||
break; // Exit if signal received | ||
} | ||
sleep(1); | ||
} | ||
|
||
stop_thread = 1; | ||
|
||
pthread_join(thread_id, NULL); | ||
|
||
printf("Final Counter Value: %d\n", counter); | ||
return 0; | ||
} |