#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PIDNUM 100 //16K*100
int tprintf(const char *fmt,...);
int main(void)
{
int status, i, fd, sock;
pid_t p1,p2;
while(1){
for(i = 0; i < PIDNUM; i++)
{
status = fork();
if(status == -1){
tprintf("fork error!\n");
}
else if(status == 0){
// fd = open("a.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IROTH);
// fd = open("b.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IROTH);
// fd = open("c.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IROTH);
// fd = open("d.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IROTH);
// fd = open("e.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IROTH);
// fd = open("f.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IROTH);
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
tprintf("this is son %d, PID is %d;", i, getpid());
goto end;
}
else{
tprintf("parent PID:%d\n", getpid());
}
}
sleep(1);
}
end:
while(1){
sleep(5);
//printf("====>This PID:%d\n", getpid());
}
return 0;
}
/*
* 设置输出格式
*/
int tprintf(const char* fmt,...)
{
va_list args;
struct tm *tstruct;
time_t tsec;
tsec = time(NULL);
tstruct = localtime(&tsec);
printf("%02d:%02d:%02d:%5d|",tstruct->tm_hour,tstruct->tm_min,tstruct->tm_sec,getpid());
va_start(args,fmt);
return vprintf(fmt,args);
}