sock_test

#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);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值