
本文共 3905 字,大约阅读时间需要 13 分钟。
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������i������������������������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������i-node������������������������������i������������������������������������������������������������������������������������������������������
���������������������������������������������������������ID������������ID���������������������������������������������������������������������������������������������
���������������������**fork()**���������������������������������������������������������fork()
������������������������������������������������������������vfork()
������������������������������������������������������������������������������������������������vfork()
���������������������������������������������
���������������������������������������������������
main() { // ... fork(); // ��������������� fork(); // ��������������������� // ... }
������������������������������������������������������������������������������������������������������������������������������������
������������
#include#include int main() { printf("Parent PID: %d\n", getpid()); pid_t child = fork(); if (child < 0) { perror("fork failed"); exit(EXIT_FAILURE); } else if (child > 0) { printf("Parent running with child PID: %d\n", child); } else { printf("Child running with PID: %d\n", getpid()); } sleep(1); return EXIT_SUCCESS;}
���������������
Parent PID: 1234Parent running with child PID: 5678Child running with PID: 5678
- ������������������
- ���������������������
- ������������������
#include#include int main() { printf("Current PID: %d\n", getpid()); pid_t child = fork(); if (child < 0) { perror("fork failed"); exit(EXIT_FAILURE); } else if (child > 0) { printf("Parent running (PID: %d)\n", child); for(int i = 0; i < 5; i++){ printf("Parent: %d\n", i); sleep(1); } } else { printf("Child running (PID: %d)\n", child); for(int i = 0; i < 5; i++){ printf("Child: %d\n", i); sleep(1); } } return EXIT_SUCCESS;}
���������������
Current PID: 1234Parent running (PID: 5678)Parent: 0Parent: 1...
���������������������������IO������fprintf
���fgets
���������������������������������������������������IO������������write
���read
������������������������������������������������������������������������
#include#include #include int main() { int fd = open("file.txt", O_WRONLY); if(fd < 0){ perror("open failed"); return -1; } pid_t child = fork(); if(child < 0){ perror("fork failed"); return -1; } else if(child > 0){ //������������������������������������ long pos = lseek(fd, 0L, SEEK_END); if(pos < 0){ perror("lseek failed"); return -1; } } else { //��������������������� char* str = "\ndata appended\n"; ssize_t size = strlen(str) * sizeof(char); if(write(fd, str, size) != size){ perror("write failed"); return -1; } sleep(3); //������������������������ } return EXIT_SUCCESS;}
���������������
parent PID: 1234, ������������child PID: 5678, ������������
发表评论
最新留言
关于作者
