first examples for myself
This commit is contained in:
parent
65df8ef927
commit
6f897672a2
37
fork1.cpp
Normal file
37
fork1.cpp
Normal 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
16
signal1.c
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user