more projects

This commit is contained in:
Austin
2026-02-03 09:00:30 -06:00
parent 2451448b8a
commit 4f40466733
250 changed files with 993861 additions and 0 deletions
Binary file not shown.
+92
View File
@@ -0,0 +1,92 @@
/**
* Records the amount of time to run a command.
* Uses shared memory.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
const char *name = "v1";
const int SIZE = 24;
struct timeval start;
struct timeval end;
pid_t pid;
int shm_fd;
char *ptr;
if (argc == 1)
return 0;
shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
if (shm_fd == -1) {
printf("shared memory creation failed\n");
return -1;
}
ftruncate(shm_fd, SIZE);
ptr = (char *)mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (ptr == MAP_FAILED) {
printf("memory map failed\n");
return -1;
}
pid = fork();
if (pid == 0)
{
// child
if (gettimeofday(&start, NULL) != 0)
return 0;
// Write the starting time to shared memory
sprintf(ptr, "%ld %ld", start.tv_sec, start.tv_usec);
// Execute the command
if (execvp(argv[1], &argv[1]) == -1) {
perror("execvp");
exit(1);
}
}
else {
// Parent
wait(NULL);
if (gettimeofday(&end, NULL) != 0)
return -1;
printf("\nParent ending time = %ld sec, %ld microsec\n",end.tv_sec, end.tv_usec);
// Read the starting time from shared memory
long start_sec, start_usec;
sscanf(ptr, "%ld %ld", &start_sec, &start_usec);
// Determine elapsed time
long elapsed_sec = end.tv_sec - start_sec;
long elapsed_usec = end.tv_usec - start_usec;
if (elapsed_usec < 0) {
elapsed_sec--;
elapsed_usec += 1000000;
}
printf("Elapsed time = %ld sec, %ld microsec\n", elapsed_sec, elapsed_usec);
shm_unlink(name);
}
return 0;
}
+53
View File
@@ -0,0 +1,53 @@
/*
* ZombieCode.c
* This small program generates a zombie process.
*
* A zombie process is created when a process terminates,
* but the parent does not invoke wait(), but the parent
* continues to run. By not invoking wait(), the child
* process maintains an entry in the process entry table
* as well as maintaining its pid.
*
* Pretty simple to do:
*
* (1) Create a child process
* (2) Have the parent run for a specified amount of time.
*
* If this program is run in the background, the state of the child
* process illustrates it is a zombie process.
* (Z underneath the S field using ps)
*/
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
int main(void)
{
pid_t pid;
pid = fork();
if (pid < 0) {
fprintf(stderr,"Unable to create child process\n");
return -1;
}
else if (pid == 0) {
/**
* Just have the child exit
* and turn into a zombie.
*/
return 0;
}
else {
// [FILL UP] Have the parent sleep for 15 seconds
sleep(15);
printf("Parent exiting\n");
return 0;
}
}