testsleep_bak.cpp

来自「在Linux测试sleep函数是否对其它进程阻塞」· C++ 代码 · 共 67 行

CPP
67
字号
#include <stdio.h>#include <stdlib.h>#include <time.h>#include <string.h>#include <fcntl.h>#include <termios.h>#include <unistd.h>#include <sys/io.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/wait.h>#include <sys/file.h>#include <pthread.h>#include <semaphore.h>#include <sys/ioctl.h>/*********************** main ***********************************/int main(void){	static int j=0;	pid_t pid;	j++;	printf("======== Time=%ld,PID=%d,Main process j=%d =====\n",time(NULL),getpid(),j);//======================= First Create process...Ready ==============================	//Process 1	pid=fork();	if(pid==0){		printf("...... Time=%ld,PID=%d,Process 1# is Running j=%d ......\n",time(NULL),getpid(),j);		sleep(5);		printf("------- Time=%ld,PID=%d,Process 1# Exit count=%d-------\n",time(NULL),getpid(),j);	}	else if(pid==-1){		printf("...... fork() process 1# error......\n");		return -1;	}	//process 2	pid=fork();	if (pid==0){		printf("------- Time=%ld,PID=%d,Process 2# Exit j=%d-------\n",time(NULL),getpid(),j);		sleep(15);	}else if (pid==-1){		printf("...... forking process 2# error ......\n");		return -1;	}	//process 3	pid=fork();	if (pid==0){		printf("------- Time=%ld,PID=%d,Process 3# Exit j=%d-------\n",time(NULL),getpid(),j);		sleep(25);	}else if (pid==-1){		printf("...... forking process 3# error ......\n");		return -1;	}	waitpid(pid,NULL,0);	//printf("PID=%d,Main Process EXIT....\n",getpid());	return 0;}

⌨️ 快捷键说明

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