The child process will continue to execute the same code as the parent
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/wait.h>int main() { int num_children = 3; pid_t pid; for (int i = 0; i < num_children; i++) { pid = fork(); if (pid < 0) { perror("fork"); exit(1); } else if (pid == 0) { // the child sees this printf("Child process %d, PID: %d\n", i + 1, getpid()); exit(0); } else { // parent sees this printf("Parent created child %d, PID: %d\n", i + 1, pid); } } printf("Parent process complete\n"); return 0;}
void exit(int __status)
Terminate execution with status
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/wait.h>int main() { pid_t pid; int status; pid = fork(); if (pid < 0) { perror("fork failed"); exit(1); } else if (pid == 0) { printf("Child process PID: %d\n", getpid()); sleep(3); printf("Child process is exiting...\n"); exit(5); } else { printf("Parent process PID: %d waiting for child to terminate...\n", getpid()); // pass by ref wait(&status); if (WIFEXITED(status)) { printf("Child terminated normally with exit status: %d\n", WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) { printf("Child was terminated by a signal: %d\n", WTERMSIG(status)); } printf("Parent process finished.\n"); } return 0;}
void abort()
kill()
Shared memory
key_t ftok(const char *path, int id)
Generate an interprocess communication (IPC) key
returns a key based on path and id that is usable in subsequent calls to msgget(), semget(), and shmget()
int shmget(key_t __key, size_t __size, int __shmflg)
SHared Memory GET
Creates or retrieves a shared memory segment with the given key, size, and permissions.
void *shmat(int __shmid, const void *__shmaddr, int __shmflg)
SHared Memory ATtach
Attach shared memory segment to current process’s address space
int shmdt(const void *__shmaddr)
SHared Memory DeTach
Detach shared memory segment from current process’s address space
int shmctl(int __shmid, int __cmd, struct shmid_ds *__buf)
SHared Memory ConTroL
Control the shared memory, eg: remove it
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/ipc.h>#include <sys/shm.h>#include <sys/types.h>#include <unistd.h>#include <sys/wait.h>#define SHM_SIZE 1024int main() { pid_t pid; key_t key = ftok("shmfile", 65); // Generates a unique key for the shared memory segment int shmid; char *shared_memory; shmid = shmget(key, SHM_SIZE, 0666 | IPC_CREAT); if (shmid == -1) { perror("shmget failed"); exit(1); } pid = fork(); if (pid < 0) { perror("fork failed"); exit(1); } if (pid == 0) { printf("Child process writing to shared memory...\n"); // Attaches the shared memory segment to the child process's address space. shared_memory = (char *)shmat(shmid, NULL, 0); if (shared_memory == (char *)-1) { perror("shmat failed"); exit(1); } // copy n or less string to memory strncpy(shared_memory, "Hello from child process!", SHM_SIZE); shmdt(shared_memory); printf("Child process finished writing.\n"); } else { wait(NULL); printf("Parent process reading from shared memory...\n"); shared_memory = (char *)shmat(shmid, NULL, 0); if (shared_memory == (char *)-1) { perror("shmat failed"); exit(1); } printf("Data read from shared memory: %s\n", shared_memory); shmdt(shared_memory); // remove the segment shmctl(shmid, IPC_RMID, NULL); printf("Parent process finished reading and cleaned up shared memory.\n"); } return 0;}