first examples for myself

This commit is contained in:
left_adjoint 2024-01-16 13:44:41 -08:00
parent 65df8ef927
commit 6f897672a2
2 changed files with 53 additions and 0 deletions

37
fork1.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
void forkAndPrint(int n){
// pid_t pid = fork();
// if(n>0){
// print("Child process, PID = %d\n", getpid());
// }
// else{
// wait(NULL);
// print("Exiting parent process
// }
pid_t pid;
if (n > 0){
pid = fork();
if(pid != 0){
printf("Parent process, PID = %d waiting to finish\n",getpid());
wait(NULL);
printf("Parent process, PID = %d finishing\n",getpid());
}
else {
forkAndPrint(n-1);
}
}
else{
printf("Bottom process!\n");
}
}
int main() {
forkAndPrint(10);
return 0;
}

16
signal1.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
void handle_sigint(int sig) {
printf("Caught signal %d\n", sig);
}
int main() {
signal(SIGINT, handle_sigint); // Register signal handler
while (1) {
sleep(1); // Sleep for 1 second
printf("Process ID %d running...\n", getpid());
}
return 0;
}