Skip to content

Commit

Permalink
Add C programs from last 2 weeks
Browse files Browse the repository at this point in the history
  • Loading branch information
unmeshvrije committed Jan 19, 2025
1 parent 4ac9fb2 commit cc28f4b
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
*.exe
ashwathama
bash_gitignore.sh
endian
garibsh
ior
myps
pid
sync
threads
19 changes: 19 additions & 0 deletions endianness.c
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*)&num;

if (*ptr == 0x0D) {
printf("Little endian\n");
} else {
printf("Big endian\n");
}
return 0;
}
65 changes: 65 additions & 0 deletions myps.c
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;
}
1 change: 1 addition & 0 deletions symlink_to_pid.c
47 changes: 47 additions & 0 deletions threads.c
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;
}

0 comments on commit cc28f4b

Please sign in to comment.