⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fork1.forkall.c

📁 Solaris下进程编程源码,sun公司培训用例
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <pthread.h>void* thread_func(void*);void print_thread();int main(int argc, char** argv){        pid_t child_pid1, child_pid2;        pthread_t tid1, tid2;            // Create two threads        if (pthread_create(&tid1, NULL, thread_func, NULL) != 0 ||            pthread_create(&tid2, NULL, thread_func, NULL) != 0) {                printf("Can't create threads.\n");                return -1;        }            // Create the child process 1 using fork1()        if ((child_pid1 = fork1()) == -1){                printf("Can't create child process 1\n");                exit(-1);        } else if (child_pid1 == 0) { // Child 1's code                printf("Child %d is created by process %d using fork1\n",                       getpid(), getppid());                while (1) {                        sleep(3);                        print_thread();                }        } else { // Parent's code                // Create the child process 2 using forkall()                if (( child_pid2 = forkall()) == -1){                        printf("Can't create child process 2\n");                        exit(-1);                } else if (child_pid2 == 0) { // Child 2's code                        printf("Child %d is created by process %d using forkall\n",                               getpid(), getppid());                        while (1) {                                sleep(3);                                print_thread();                        }                } else { // Parent's code                        while (1) {                                sleep(2);                                print_thread();                        }                }        }}void* thread_func(void* arg){        while (1) {                sleep(3);                print_thread();        }}void print_thread(){        printf("Process %d: thread %d running\n", getpid(), pthread_self());  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -